😎 κ³΅λΆ€ν•˜λŠ” μ§•μ§•μ•ŒνŒŒμΉ΄λŠ” μ²˜μŒμ΄μ§€?

[디렉터리와 μ‹œκ°„ 처리] 디렉터리에 λŒ€ν•œ 정보 좜λ ₯ν•˜κΈ° ( = $ ls -RI) λ³Έλ¬Έ

πŸ‘©‍πŸ’» IoT (Embedded)/Raspberry Pi

[디렉터리와 μ‹œκ°„ 처리] 디렉터리에 λŒ€ν•œ 정보 좜λ ₯ν•˜κΈ° ( = $ ls -RI)

μ§•μ§•μ•ŒνŒŒμΉ΄ 2024. 1. 7. 23:59
728x90
λ°˜μ‘ν˜•

<μ„œμ˜μ§„ λ‹˜μ˜ 사물인터넷을 μœ„ν•œ λ¦¬λˆ…μŠ€ ν”„λ‘œκ·Έλž˜λ° with λΌμ¦ˆλ² λ¦¬νŒŒμ΄ μ„œμ μ„ μ°Έκ³ ν•΄μ„œ μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€ :-)>

 

⭐ 디렉터리

νŒŒμΌμ„ λ‹΄λŠ” 데 μ‚¬μš©ν•œλ‹€

μƒˆλ‘œμš΄ 디렉터리λ₯Ό μƒμ„±ν•˜λ©΄ ν˜„μž¬μ˜ 디렉터리λ₯Ό μ˜λ―Έν•œλ‹€

"." 파일과 λΆ€λͺ¨ 디렉터리λ₯Ό μ˜λ―Έν•˜λŠ” ".." 파일이 μžλ™μœΌλ‘œ μƒμ„±λœλ‹€

λ””λ ‰ν„°λ¦¬λŠ” 일반 파일 및 λ‹€λ₯Έ λ””λ ‰ν„°λ¦¬μ˜ μ΄λ¦„μœΌλ‘œ κ΅¬μ„±λœ 파일둜, 일반 파일 μ‘°μž‘μ— μ“°μ΄λŠ” ν•¨μˆ˜λ₯Ό λ™μΌν•˜κ²Œ μ‚¬μš©ν•œλ‹€

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>     // 디렉터리 μ‘°μž‘ ν•¨μˆ˜
#include <pwd.h>        // getpwuild() ν•¨μˆ˜: uid μ΄μš©ν•΄μ„œ μ‚¬μš©μžμ˜ 이름 κ΅¬ν•˜κΈ°
#include <grp.h>        // getgrgid() ν•¨μˆ˜: gid μ΄μš©ν•΄μ„œ 그룹의 이름 κ΅¬ν•˜κΈ°
#include <time.h>       // localtime() ν•¨μˆ˜
#include <sys/stat.h>
#include <sys/types.h>

int listDir(char *arg) {
    // 디렉터리 μ‘°μž‘μ„ μœ„ν•œ 슀트림
    DIR *pdir;
    // λ””λ ‰ν„°λ¦¬μ˜ ν•­λͺ©μ„ μœ„ν•œ ꡬ쑰체
    struct dirent *dirt;
    // 파일의 정보λ₯Ό μœ„ν•œ ꡬ쑰체
    struct stat statBuf;
    // μ‚¬μš©μž 이름 좜λ ₯을 μœ„ν•œ
    struct passwd *username;
    // κ·Έλ£Ή 이름 좜λ ₯을 μœ„ν•œ λ³€μˆ˜
    struct group *groupname;
    // μ‹œκ°„ 좜λ ₯을 μœ„ν•œ λ³€μˆ˜
    struct tm *t;
    int i = 0, count = 0;
    char *dirName[255], buf[255], permission[11], mtime[20];

    memset(dirName, 0, sizeof(dirName));
    memset(&dirt, 0, sizeof(dirt));
    memset(&statBuf, 0, sizeof(statBuf));

    // ν•΄λ‹Ή λ””λ ‰ν„°λ¦¬μ˜ 슀트림 μ—΄κΈ°
    if ((pdir = opendir(arg)) <= 0) {
        perror("opendir");
        return -1;
    }
    // λ””λ ‰ν„°λ¦¬λ‘œ 이동
    chdir(arg);
    // ν˜„μž¬ λ””λ ‰ν„°λ¦¬μ˜ μ ˆλŒ€ 경둜 κ°€μ Έμ™€μ„œ ν‘œμ‹œ
    getcwd(buf, 255);
    printf("\n%s: Directory\n", arg);

    // ν˜„μž¬ 디렉터리λ₯Ό 읽을 수 있으면 μˆœν™˜
    while ((dirt = readdir(pdir)) != NULL) {
        // ν˜„μž¬ 디렉터리에 λŒ€ν•œ 정보 κ°€μ Έμ˜€κΈ°
        lstat(dirt->d_name, &statBuf);
        // 파일 μ’…λ₯˜ 검사
        if (S_ISDIR(statBuf.st_mode))
            permission[0] = 'd';            
        else if (S_ISLNK(statBuf.st_mode))
            permission[0] = 'l';
        else if (S_ISCHR(statBuf.st_mode))
            permission[0] = 'c';
        else if (S_ISBLK(statBuf.st_mode))
            permission[0] = 'b';
        else if (S_ISSOCK(statBuf.st_mode))
            permission[0] = 's';
        else if (S_ISFIFO(statBuf.st_mode))
            permission[0] = 'P';
        else
            permission[0] = '-';
        // ## νŒŒμΌμ— λŒ€ν•œ 정보λ₯Ό κ°€μ Έμ˜¨ ν›„ 파일의 μœ ν˜•κ³Ό νΌλ―Έμ…˜μ„ λΆ„μ„ν•΄μ„œ λ¬Έμžμ—΄ 배열에 μ €μž₯ν•œλ‹€
        // μ‚¬μš©μžμ— λŒ€ν•œ κΆŒν•œ 검사
        permission[1] = statBuf.st_mode&S_IRUSR? 'r' : '-';
        permission[2] = statBuf.st_mode&S_IWUSR? 'w' : '-';
        permission[3] = statBuf.st_mode&S_IXUSR? 'x' :
                        statBuf.st_mode&S_ISUID? 'S' : '-';
        // 그룹에 λŒ€ν•œ κΆŒν•œ 검사
        permission[4] = statBuf.st_mode&S_IRGRP? 'r' : '-';
        permission[5] = statBuf.st_mode&S_IWGRP? 'w' : '-';
        permission[6] = statBuf.st_mode&S_IXGRP? 'x' :
                        statBuf.st_mode&S_ISGID? 'S' : '-';
        // λ‹€λ₯Έ μ‚¬μš©μžμ— λŒ€ν•œ κΆŒν•œ 검사
        permission[7] = statBuf.st_mode&S_IROTH? 'r' : '-';
        permission[8] = statBuf.st_mode&S_IWOTH? 'w' : '-';
        permission[9] = statBuf.st_mode&S_IXOTH? 'x' : '-';

        // μŠ€ν‹°ν‚€ λΉ„νŠΈ μ„€μ •
        if (statBuf.st_mode & S_IXOTH) {
            permission[9] = statBuf.st_mode & S_ISVTX? 't' : 'x';
        } else {
            permission[9] = statBuf.st_mode & S_ISVTX? 'T' : 'x';
        }

        permission[10] = '\0';

        if (S_ISDIR(statBuf.st_mode) == 1) {
            if (strcmp(dirt->d_name, ".") && strcmp(dirt->d_name, "..")) {
                dirName[count] = dirt -> d_name;
                count = count + 1;
            }
        }
        // uid μ—μ„œ μ‚¬μš©μžμ˜ 이름 νšλ“
        username = getpwuid(statBuf.st_uid);
        // gid μ—μ„œ μ‚¬μš©μžμ˜ 이름 νšλ“
        groupname = getgrgid(statBuf.st_gid);
        // 졜근 μˆ˜μ •λœ μ‹œκ°„ 좜λ ₯
        t = localtime(&statBuf.st_mtime);
        // 좜λ ₯을 μœ„ν•œ μ„œμ‹ν™”: tm ꡬ쑰체
        sprintf(mtime, "%04d-%02d-%02d %02d:%02d:%02d",
            t->tm_year + 1900, t->tm_mon + 1, t -> tm_mday,
            t->tm_hour, t->tm_min, t->tm_sec);

        printf("%s %2d %s %s %9ld %s %s\n", permission, statBuf.st_nlink,
                username->pw_name, groupname->gr_name,
                statBuf.st_size, mtime, dirt->d_name);
    }

    for (i = 0; i < count; i++) {
        // λ‹€λ₯Έ 디렉터리에 λŒ€ν•œ μž¬κ·€ 호좜
        if (listDir(dirName[i]) == -1)
            break;
    }

    printf("\n");
    // μ—΄μ—ˆλ˜ λ””λ ‰ν„°λ¦¬μ˜ 슀트림 λ‹«λŠ”λ‹€
    closedir(pdir);
    // μ›λž˜ λ””λ ‰ν„°λ¦¬λ‘œ 이동
    chdir("..");
    return 0;
}

int main(int argc, char ** argv) {
    // μ‚¬μš©μž ID와 κ·Έλ£Ή ID μ΄μš©ν•΄μ„œ μ‚¬μš©μžμ˜ 이름과 그룹의 이름을 κ΅¬ν•œ ν›„ μ ‘κ·Ό κΆŒν•œκ³Ό μˆ˜μ • μ‹œκ°„ λ“±
    // νŒŒμΌμ— λŒ€ν•œ 정보λ₯Ό 화면에 좜λ ₯ν•œλ‹€
    // μ„œλΈŒ 디렉터리에 λŒ€ν•΄μ„œλ„ μž¬κ·€ 호좜 μˆ˜ν–‰ν•˜κ³ , κ΄€λ ¨ μ •λ³΄μ˜ 좜λ ₯이 λλ‚˜λ©΄ 디렉터리λ₯Ό λ‹«λŠ”λ‹€
    if (argc < 2) {
        fprintf(stderr, "Usage : %s directory_name.\n", argv[0]);
        return -1;
    }
    // μ‚¬μš©μžκ°€ μž…λ ₯ν•œ 디렉터리에 λŒ€ν•΄ ν˜ΈμΆœν•œλ‹€
    listDir(argv[1]);

    return 0;
}

/*
gani@gani:~/raspi/File $ gcc -o list list.c
gani@gani:~/raspi/File $ ./list . | head
*/
gcc -o list list.c
./list . | head

728x90
λ°˜μ‘ν˜•
Comments