Untitled

 avatar
unknown
plain_text
a year ago
1.4 kB
16
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>

void print_file_type(mode_t mode) {
    if (S_ISREG(mode))
        printf("Type : regular file\n");
    else if (S_ISDIR(mode))
        printf("Type : directory\n");
    else if (S_ISLNK(mode))
        printf("Type : symbolic link\n");
    else if (S_ISCHR(mode))
        printf("Type : character device\n");
    else if (S_ISBLK(mode))
        printf("Type : block device\n");
    else if (S_ISFIFO(mode))
        printf("Type : FIFO/pipe\n");
    else if (S_ISSOCK(mode))
        printf("Type : socket\n");
    else
        printf("Type : unknown\n");
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    struct stat sb;

    // utilisation de stat()
    if (stat(argv[1], &sb) == -1) {
        perror("stat");
        exit(EXIT_FAILURE);
    }

    printf("Inode number : %ld\n", (long) sb.st_ino);
    printf("Number of links : %ld\n", (long) sb.st_nlink);

    struct passwd *pw = getpwuid(sb.st_uid);
    struct group  *gr = getgrgid(sb.st_gid);

    printf("Owner : %s\n", pw ? pw->pw_name : "unknown");
    printf("Group : %s\n", gr ? gr->gr_name : "unknown");
    printf("Size : %ld bytes\n", (long) sb.st_size);

    print_file_type(sb.st_mode);

    return 0;
}
Editor is loading...
Leave a Comment