'C'에 해당되는 글 2건

  1. 2011.05.23 [C] "rm -rf" 명령 수행 함수 1
  2. 2011.05.23 [C] "mkdir -p" 명령 수행 함수

가끔 필요하지만 막상구현하려면 귀찮는 함수 ^^

파일이 존재하는 디렉토리 지우는 C코드입니다.

출처 : http://kldp.org/node/108793
위 출처에서 clique 님이 작성하신 코드를 조금 수정했습니다.

ftw(), nftw()라는 file tree walking(traversing) 함수가 있습니다. SUS에 속해 있죠
http://www.opengroup.org/onlinepubs/9699919799/
기본적으로는 pre-order traverse입니다만, 옵션을 줘서 post-order로 traverse할 수 있습니다.


#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>

static int rmdir_helper(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf)
{
    switch ( tflag )
    {
    case FTW_D:
    case FTW_DP:
        if ( rmdir(fpath) == -1 )
            perror("unlink");
        break;
    case FTW_F:
    case FTW_SL:
        if ( unlink(fpath) == -1 )
            perror("unlink");
        break;
    default:
        puts("do nothing");
    }
    return 0;
}

int rmdir_rf(const char * dir_to_remove)
{
    int flags = 0;

    if ( dir_to_remove == NULL )
        return 1;

    flags |= FTW_DEPTH; // post-order traverse

    if (nftw(dir_to_remove, rmdir_helper, 10, flags) == -1)
        perror("nftw");
        exit(EXIT_FAILURE);
    

    return 0;
}


int main(int argc, char *argv[])
{
    if ( argc == 2 )
        rmdir_rf(argv[1]);
    return 0;
}

Posted by kabangkle
가끔 필요할때까 있는데 구현하기는 귀찮은 함수

출처 : 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'  */

int
mkpath(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);
}

Posted by kabangkle