'c'에 해당되는 글 4건

  1. 2013.03.12 C/C++ link
  2. 2012.05.22 [C언어] Directory Scan / File or Directory
  3. 2012.05.22 [C언어] 설정파일 읽기
  4. 2012.05.15 C, C++ reference

C/C++ link

ITWeb/개발일반 2013.03.12 13:01

http://publib.boulder.ibm.com/infocenter/zos/v1r13/index.jsp?topic=%2Fcom.ibm.zos.r13.cbc%2Fcbc.htm

신고
크리에이티브 커먼즈 라이선스
Creative Commons License
tags : c, c++
Trackback 0 : Comment 0

[C언어] Directory Scan / File or Directory

ITWeb/개발일반 2012.05.22 23:33
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

int main (int argc, char *argv[]) {

    DIR* dir;
    struct dirent* entry = NULL;
    struct stat statBuff;

    char *path = "/home/jjeong/dist/simscan-1.4.0";
    char buff[256]={'\0'};

    if ( ( dir = opendir(path) ) == NULL) {
        printf("Cannot Open Directory\n");
        return -1;
    }


    while( ( entry = readdir( dir ) ) != NULL ) {
        lstat(entry->d_name, &statBuff);

        snprintf(buff, sizeof(buff), "%s/%s", path, entry->d_name);
        //printf("Full Path :%s\n", buff);

        if ( chdir(buff) == 0 ) {
            printf("1.Directory : %s\n", entry->d_name);
        } else {
            printf("2.File : %s\n", entry->d_name);
        }
        /*
        if ( S_ISDIR(statBuff.st_mode) ) {
            if ( chdir(buff) == 0 ) {
                printf("1.Directory : %s\n", entry->d_name);
            } else {
                printf("2.File : %s\n", entry->d_name);
            }
        } else if ( S_ISREG(statBuff.st_mode) ) {
            printf("File : %s\n", entry->d_name);
        }
        */

    }

    closedir(dir);

    return 1;
}


원래 문서에 있는 데로 라면,

S_ISDIR 와 S_ISREG 에서 directory 와 file 이 구분되어져야 하는데.. 잘 되지 않았습니다.

그래서 workaround 로 chdir 의 result 값으로 directory 인지 file 인지 구분해 보았습니다. ㅡ.ㅡ;;

신고
크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 : Comment 0

[C언어] 설정파일 읽기

ITWeb/개발일반 2012.05.22 23:17

[Build & Run]

gcc -c config.c; gcc config.o -o config

./config /home/jjeong/workspace/config/config.property

[config.property]

# UserAuthUrl
UserAuthUrl^http://192.168.1.2:8080/rcpt_auth?email=%s
# UserAuthCompareStr
UserAuthCompareStr^verified
# DlAuthUrl
DlAuthUrl^http://192.168.1.2:8080/dlCheck?q={filterSearch:'%s'}
# DlAuthCompareStr
DlAuthCompareStr^SUCCESS
# AhfServer
AhfServer^192.168.1.2
# AhfPort
AhfPort^33000
#AhfUser
AhfUser^jjeong
#AhfPass
AhfPass^1234
#AhfDb
AhfDb^demodb
#AhfQuery
AhfQuery^SELECT * FROM event WHERE id='%s'
#SkipDomain
SkipDomain^jjeong.com

[config.c]

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct config {
        char userAuthUrl[256];
        char userAuthCompareStr[128];
        char dlAuthUrl[256];
        char dlAuthCompareStr[128];
        char ahfServer[128];
        int ahfPort;
        char ahfUser[128];
        char ahfPass[128];
        char ahfDb[128];
        char ahfQuery[128];
        char skipDomain[128];
} CONFIG;

int isBlank(char ch) {
    return ( (ch==0x20) || (ch=='\t') || (ch=='\r') || (ch=='\n') );
}

char *trim( char *s ) {
    char *f=s, *e=0, *c=s;

    /* 뒤쪽 공백 제거 */
    e=s +(strlen(s)) -1;
    while( isBlank(*e) && s<=e) e--;
    *(e+1)=0;

    /* 앞쪽 공백 제거 */
    while( isBlank(*f) && f<=e) f++;

    /* 공백 없는 부분 복사 */
    if (s!=f) {
        while(f<=e) *(c++) = *(f++);
        *c=0;
    }

    return s;
}

int setConfig(CONFIG *conf, char *confFile) {
        char readBuff[512];
        char varBuff[256];
        char *temp;

        FILE *fp;

        memset(varBuff, 0, 256);

        fp = fopen(confFile, "r");

        if ( fp ){
                while ( !feof(fp) ) {
                        memset(readBuff, 0, 512);

                        if ( fgets(readBuff, 512, fp) == NULL ) {
                                continue;
                        }

                        temp = trim(readBuff);

                        if ( *temp == '#' ) {
                                continue;
                        }

                        if ( strncasecmp(temp, "UserAuthUrl", strlen("UserAuthUrl")) == 0 ) {
                                strcpy(conf->userAuthUrl, strrchr(temp, '^')+1);
                                snprintf(varBuff, sizeof(varBuff), conf->userAuthUrl, "sadfasdfsafasfsdafsafsadf");
                                printf("UserAuthUrl : %s\n", varBuff);
                                memset(varBuff, 0, 256);
                        }

                        if ( strncasecmp(temp, "UserAuthCompareStr", strlen("UserAuthCompareStr")) == 0 ) {
                                strcpy(conf->userAuthCompareStr, strrchr(temp, '^')+1);
                                printf("UserAuthCompareStr : %s\n", conf->userAuthCompareStr);
                        }

                        if ( strncasecmp(temp, "DlAuthUrl", strlen("DlAuthUrl")) == 0 ) {
                                strcpy(conf->dlAuthUrl, strrchr(temp, '^')+1);
                                snprintf(varBuff, sizeof(varBuff), conf->dlAuthUrl, "mail@jjeong.com");
                                printf("DlAuthUrl : %s\n", varBuff);
                                memset(varBuff, 0, 256);

                        }

                        if ( strncasecmp(temp, "DlAuthCompareStr", strlen("DlAuthCompareStr")) == 0 ) {
                                strcpy(conf->dlAuthCompareStr, strrchr(temp, '^')+1);

                                if ( strstr("SUCCESS", conf->dlAuthCompareStr) ) {
                                        printf("DlAuthCompareStr SUCCESS\n");
                                }
                                printf("DlAuthCompareStr : %s\n", conf->dlAuthCompareStr);
                        }

                        if ( strncasecmp(temp, "AhfServer", strlen("AhfServer")) == 0 ) {
                                strcpy(conf->ahfServer, strrchr(temp, '^')+1);
                                printf("AhfServer : %s\n", conf->ahfServer);
                        }

                        if ( strncasecmp(temp, "AhfPort", strlen("AhfPort")) == 0 ) {
                                conf->ahfPort = atoi(strrchr(temp, '^')+1);
                                printf("AhfPort : %d\n", conf->ahfPort);
                        }

                        if ( strncasecmp(temp, "AhfUser", strlen("AhfUser")) == 0 ) {
                                strcpy(conf->ahfUser, strrchr(temp, '^')+1);
                                printf("AhfUser : %s\n", conf->ahfUser);
                        }

                        if ( strncasecmp(temp, "AhfPass", strlen("AhfPass")) == 0 ) {
                                strcpy(conf->ahfPass, strrchr(temp, '^')+1);
                                printf("AhfPass : %s\n", conf->ahfPass);
                        }

                        if ( strncasecmp(temp, "AhfDb", strlen("AhfDb")) == 0 ) {
                                strcpy(conf->ahfDb, strrchr(temp, '^')+1);
                                printf("AhfDb : %s\n", conf->ahfDb);
                        }

                        if ( strncasecmp(temp, "AhfQuery", strlen("AhfQuery")) == 0 ) {
                                strcpy(conf->ahfQuery, strrchr(temp, '^')+1);
                                snprintf(varBuff, sizeof(varBuff), conf->ahfQuery, "TEST");
                                printf("AhfQuery: %s\n", varBuff);
                                memset(varBuff, 0, 256);
                        }

                        if ( strncasecmp(temp, "SkipDomain", strlen("SkipDomain")) == 0 ) {
                                strcpy(conf->skipDomain, strrchr(temp, '^')+1);
                                printf("SkpDomain : %s\n", conf->skipDomain);
                        }

                }
        } else {
        }

        fclose(fp);

        return 1;
}


int main(int argc, char *argv[]) {
        CONFIG conf;

        printf("file full path : %s\n", argv[1]);

        setConfig(&conf, argv[1]);

        return 1;
}

일부 코드는 레퍼런스 코드 가져다 사용하였습니다.

뭐 이런 종류의 코드는 구글링 하면 많이 나오니까.. ^^;;

신고
크리에이티브 커먼즈 라이선스
Creative Commons License
tags : c, config, Property
Trackback 0 : Comment 0

C, C++ reference

ITWeb/개발일반 2012.05.15 17:34

[원본링크]


C, C++ API Doc 정리가 잘되어 있내요.

이제 C, C++ 도 해야 하는군..;;;

신고
크리에이티브 커먼즈 라이선스
Creative Commons License
tags : c, c++
Trackback 0 : Comment 0

티스토리 툴바