파일인지 디렉터리인지 구별하는 방법좀 알려주세요.

puaxx의 이미지

리눅스 시스템에서 임의의 파일이 파일인지 디렉터리인지 구별하고 싶은데 방법을 모르겠습니다..

/usr/locarl/some

여기서 some이 디렉터리인지 파일인지 알고 싶은데요...방법이 있을까요? 구별해 주는 시스템 함수같은게 없는지요?

서지훈의 이미지

$ man 2 fstat

<어떠한 역경에도 굴하지 않는 '하양 지훈'>

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);

dlwornr99의 이미지

APU 책 부록에 있는 프로그램에 있는 내용입니다.

#include    <sys/types.h>
#include    <sys/stat.h>

int
main(int argc, char *argv[])
{
    int         i;
    struct stat buf;
    char        *ptr;

    for (i = 1; i < argc; i++) {
        printf("%s: ", argv[i]);
        if (lstat(argv[i], &buf) < 0) {
            err_ret("lstat error");
            continue;
        }

        if      (S_ISREG(buf.st_mode))  ptr = "regular";
        else if (S_ISDIR(buf.st_mode))  ptr = "directory";
        else if (S_ISCHR(buf.st_mode))  ptr = "character special";
        else if (S_ISBLK(buf.st_mode))  ptr = "block special";
        else if (S_ISFIFO(buf.st_mode)) ptr = "fifo";
#ifdef  S_ISLNK
        else if (S_ISLNK(buf.st_mode))  ptr = "symbolic link";
#endif
#ifdef  S_ISSOCK
        else if (S_ISSOCK(buf.st_mode)) ptr = "socket";
#endif
        else                ptr = "** unknown mode **";
        printf("%s\n", ptr);
    }
    exit(0);
}
puaxx의 이미지

감사합니다..참고 하겠습니다.

Necromancer의 이미지

man 2 stat
man 2 fstat

리턴후 struct stat 구조체 값에 채워지는 멤버 변수 중 st_mode가
파일타입을 알려줍니다. (퍼미션도 갖고 있습니다)

Written By the Black Knight of Destruction