Start system call

 #include <stdio.h>

#include <sys/stat.h>

#include <stdlib.h>

#include <time.h>


int main(int argc, char* argv[]) {

    struct stat file;

    int n;


    if (argc != 2) {

        printf("Usage: ./a.out <filename>\n");

        exit(-1);

    }


    // Checking if file statistics can be obtained

    if ((n = stat(argv[1], &file)) == -1) {

        perror(argv[1]);

        exit(-1);

    }


    // Printing file information

    printf("User id: %d\n", file.st_uid);

    printf("Group id: %d\n", file.st_gid);

    printf("Block size: %ld\n", file.st_blksize);

    printf("Blocks allocated: %ld\n", file.st_blocks);

    printf("Inode no.: %ld\n", file.st_ino);

    printf("Last accessed: %s", ctime(&(file.st_atime)));

    printf("Last modified: %s", ctime(&(file.st_mtime)));

    printf("File size: %ld bytes\n", file.st_size);

    printf("No. of links: %ld\n", file.st_nlink);


    // Displaying file permissions

    printf("Permissions: ");

    printf((S_ISDIR(file.st_mode)) ? "d" : "-");

    printf((file.st_mode & S_IRUSR) ? "r" : "-");

    printf((file.st_mode & S_IWUSR) ? "w" : "-");

    printf((file.st_mode & S_IXUSR) ? "x" : "-");

    printf((file.st_mode & S_IRGRP) ? "r" : "-");

    printf((file.st_mode & S_IWGRP) ? "w" : "-");

    printf((file.st_mode & S_IXGRP) ? "x" : "-");

    printf((file.st_mode & S_IROTH) ? "r" : "-");

    printf((file.st_mode & S_IWOTH) ? "w" : "-");

    printf((file.st_mode & S_IXOTH) ? "x" : "-");

    printf("\n");


    // Determining file type

    if (S_ISREG(file.st_mode)) {

        printf("File type: Regular file\n");

    }

    if (S_ISDIR(file.st_mode)) {

        printf("File type: Directory\n");

    }


}

Comments

Popular posts from this blog

Implementation