Exercise 8.5 - inode entry

Question

Modify the fsize program to print the other information contained in the inode entry.

/*
   Modify the fsize program to print the other information contained in the inode entry.

   Solution by Akil Adeshwar
   https://clc-wiki.net/wiki/K%26R2_solutions:Chapter_8:Exercise_5
*/

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>
#include <pwd.h>


#define MAX_PATH 1024

#ifndef DIRSIZ
#define DIRSIZ 14
#endif


void dirwalk( char *dir,void (*fcn)(char *)){

    char name[MAX_PATH];
    struct dirent *dp;
    DIR *dfd;

    if((dfd = opendir(dir))==NULL){
        puts("Error: Cannot open Directory");
        return;
    }
    puts(dir);
    // Get each dir entry
    while((dp=readdir(dfd)) != NULL){
        // Skip . and .. is redundant.
        if(strcmp(dp->d_name,".") == 0
           || strcmp(dp->d_name,"..") ==0 )
            continue;
        if(strlen(dir)+strlen(dp->d_name)+2 > sizeof(name))
            puts("Error: Name too long!");
        else{
            sprintf(name,"%s/%s",dir,dp->d_name);
            // Call fsize
            (*fcn)(name);
        }
    }
    closedir(dfd);
}

void fsize(char *name){
    struct stat stbuf;

    if(stat(name,&stbuf) == -1){
        puts("Error: Cannot get file stats!");
        return;
    }

    if((stbuf.st_mode & S_IFMT) == S_IFDIR){
        dirwalk(name,fsize);
    }
    struct passwd *pwd = getpwuid(stbuf.st_uid);
    //print file name,size and owner
    printf("%81d %s Owner: %s\n",(int)stbuf.st_size,name,pwd->pw_name);
}




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

    if(argc==1)
        fsize(".");
    else
        while(--argc>0)
            fsize(*++argv);
    return 0;
}

Explanation

The main purpose of this program information about files and directories, similar to the ls command in Unix-like systems, but with more detailed information. It prints various file attributes like size, block information, and other metadata.

If a file argument is provided, it gets file statistics using stat to get the file inode and other information. If the program encounters a directory, it uses dirwalk to recursively traverse through it and on each file, it does a stat on the file.

$ ./ex_8.5_fsize source/_templates

    source/_templates
                                 101 source/_templates/index.html Owner: ec2-user
                                 902 source/_templates/layout.html Owner: ec2-user
                                 275 source/_templates/logo.html Owner: ec2-user
                                 60 source/_templates Owner: ec2-user