흐르는 시간의 블로그...

C++ 빌더를 사용할때는 ForceDirectory()라는 좋은 함수가 있었다.
말 그대로... 파라미터만 주면 디렉토리를 다 만들어 준다.
리눅스 에서 코딩하는데 해당 기능이 없어서 자료를 찾다가...
지워진 링크에서 자료를 찾았다. ^^

----------------------------------------------------------------------------------------------------------------------------------------

출처 : http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/

Most people are probably familiar with the UNIX utility, mkdir(1). The mkdir utility makes directories (surprise surprise). There is a matching mkdir(2) system call available in the POSIX standard C library. The usage is pretty straightforward - how ever, the command-line executable, mkdir(1), supports a useful option -p to "create intermediate directories as required". Its very convenient to run `mkdir -p' on a long path before copying things or whatever, since you don't have to worry about the directory structure not existing.However, the mkdir(2) library function doesn't support an analogous mode. If you want to recursively create all the intermediate directories in a path in your program, you must implement this yourself. I've used this same function in at least three distinct projects now and so I decided to post the code:

/* Function with behaviour like `mkdir -p'  */intmkpath(const char *s, mode_t mode){        char *q, *r = NULL, *path = NULL, *up = NULL;        int rv;        rv = -1;        if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0)                return (0);        if ((path = strdup(s)) == NULL)                exit(1);             if ((q = strdup(s)) == NULL)                exit(1);        if ((r = dirname(q)) == NULL)                goto out;                if ((up = strdup(r)) == NULL)                exit(1);        if ((mkpath(up, mode) == -1) && (errno != EEXIST))                goto out;        if ((mkdir(path, mode) == -1) && (errno != EEXIST))                rv = -1;        else                rv = 0;out:        if (up != NULL)                free(up);        free(q);        free(path);        return (rv);}