Welcome, guest | Sign In | My Account | Store | Cart

walking down the path, create the subpath when necessary

C, 31 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int create_file(char *fullpath, mode_t mode){
    struct stat stat_buf;
    int i = 1;
    int fd = -1;

    while( '\0' != fullpath[i] ){

        if('/' == fullpath[i] ){
            fullpath[i] = '\0';
            errno = 0;
            if( -1 == (stat(fullpath, &stat_buf) ) && ENOENT == errno    ){
                if(-1 == mkdir(fullpath, mode)){
                    perror("");
                }
            }
            fullpath[i] = '/';
        }
        i++;
    }

    errno = 0;
    if(-1 == (fd = creat(fullpath, mode)) ){
        printf("create file error %s : %s\n", fullpath, strerror(errno));
        return -1;
    }
    else{
        close(fd);
    }

    return 0;
}
Created by J Y on Wed, 20 May 2009 (MIT)
C recipes (32)
J Y's recipes (21)

Required Modules

  • (none specified)

Other Information and Tasks