Загрузка данных



#include <dirent.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

char *
get_perms(int p, char *buf)
{
    const char *rwx = "rwx";

    if (p < 0 || p > 0777) {
        return NULL;
    }
    if (buf == NULL) {
        buf = malloc(10);
        if (buf == NULL) {
            return NULL;
        }
    }
    for (int i = 0; i < 9; ++i) {
        if (p & (0400 >> i)) {
            buf[i] = rwx[i % 3];
        } else {
            buf[i] = '-';
        }
    }
    buf[9] = '\0';
    return buf;
}

const char *
get_type(mode_t mode)
{
    if (S_ISREG(mode)) {
        return "file";
    }
    if (S_ISDIR(mode)) {
        return "dir";
    }
    if (S_ISLNK(mode)) {
        return "link";
    }
    if (S_ISCHR(mode)) {
        return "char";
    }
    if (S_ISBLK(mode)) {
        return "block";
    }
    if (S_ISFIFO(mode)) {
        return "fifo";
    }
    if (S_ISSOCK(mode)) {
        return "socket";
    }
    return "unknown";
}

int
main(void)
{
    DIR *dir = opendir(".");
    if (dir == NULL) {
        perror("opendir");
        return 1;
    }

    struct dirent *ent;
    struct stat st;
    char perms[10];

    while ((ent = readdir(dir)) != NULL) {
        if (lstat(ent->d_name, &st) == -1) {
            perror(ent->d_name);
            continue;
        }

        get_perms(st.st_mode & 0777, perms);

        printf("%s %ju %s %s\n",
                ent->d_name,
                (uintmax_t) st.st_ino,
                get_type(st.st_mode),
                perms);
    }

    closedir(dir);
    return 0;
}