Exercise 8-5 (fsize, inode)

Chapter_8     Exercise_8-4     fsize malloc     Exercise_8-6







Exercise 8-5     K&R, p. 184


Exercise 8-5. Modify the fsize program to print the other information contained in the inode entry.




inode.c     K&R, p. 181-182         download


#include <stdio.h> // for printf(), fprintf(), sprintf(), stderr, NULL
#include <string.h> // for strcmp(), strlen()
#include <time.h> // for ctime()
#include <sys/stat.h> // for struct stat, stat(), mode_t, S_ISDIR(),
// S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP,
// S_IROTH, S_IWOTH, S_IXOTH
#include <sys/dir.h> // local directory structure, struct direct
#include <dirent.h> // for DIR, opendir(), readdir(), closedir()

void inode(char *); // print inode information

int main(int argc, char **argv) // print file sizes
{ // /usr/include/x86_64-linux-gnu/bits/stat.h struct stat
printf("Inode entries from 'struct stat':\n");
printf("Device, File no, File mode, Link count, User ID, Group ID,\n");
printf("Size, Block size, Blocks, Last accessed, modified, changed\n");
if (argc == 1)
{inode(".");} // default: current directory
else
{
while (--argc > 0) // all args but program name
{inode(*++argv);} // skip program name
}

return 0;
}

void dirwalk(char *, void (*fcn)(char *));
void printFlags(mode_t mode);

void inode(char *name) // print inode information for file "name"
{
struct stat stbuf;

if (stat(name, &stbuf) == -1)
{
fprintf(stderr, "inode(): can't access %s\n", name);
return;
}

if (S_ISDIR(stbuf.st_mode))
{dirwalk(name, inode);}

printf("\n\"%s\":\n", name);
printf("(%lu, %ld, %o (", stbuf.st_dev, stbuf.st_ino, stbuf.st_mode);
printFlags(stbuf.st_mode);
printf("), %lu, ", stbuf.st_nlink);
printf("%d, %d,\n", stbuf.st_uid, stbuf.st_gid);
printf(" %ld bytes, ", stbuf.st_size);
printf("%ld, %ld blocks)\n", stbuf.st_blksize, stbuf.st_blocks);
printf("Last accessed: %s", ctime(&stbuf.st_atime));
printf("Last modified: %s", ctime(&stbuf.st_mtime));
printf("Last status change: %s", ctime(&stbuf.st_ctime));
}

#define MAX_PATH 1024

// apply fcn to all files in dir
void dirwalk(char *dir, void (*fcn)(char *))
{
char name[MAX_PATH];
struct direct *dp; // directory pointer
DIR *dfd; // directory file descriptor

if ((dfd = opendir(dir)) == NULL)
{
fprintf(stderr, "dirwalk(): can't open %s\n", dir);
return;
}

while ((dp = readdir(dfd)) != NULL)
{
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
{continue;} // skip self and parent

if (strlen(dir)+strlen(dp->d_name)+2 > sizeof(name)) // MAX_PATH
{ // +2 for '/' and ending '\0'
fprintf(stderr, "dirwalk(): name %s/%s too long\n",
dir, dp->d_name);
return;
}
// else
sprintf(name, "%s/%s", dir, dp->d_name);
fcn(name); // (*fcn)(name);
}

closedir(dfd);
}

void printFlags(mode_t mode)
{
printf("%c", S_ISDIR(mode) ? 'd' : '-');
printf("%c", (mode & S_IRUSR) ? 'r' : '-');
printf("%c", (mode & S_IWUSR) ? 'w' : '-');
printf("%c", (mode & S_IXUSR) ? 'x' : '-');
printf("%c", (mode & S_IRGRP) ? 'r' : '-');
printf("%c", (mode & S_IWGRP) ? 'w' : '-');
printf("%c", (mode & S_IXGRP) ? 'x' : '-');
printf("%c", (mode & S_IROTH) ? 'r' : '-');
printf("%c", (mode & S_IWOTH) ? 'w' : '-');
printf("%c", (mode & S_IXOTH) ? 'x' : '-');
}

/*
gcc inode.c -o inode
./inode
Inode entries from 'struct stat':
Device, File no, File mode, Link count, User ID, Group ID,
Size, Block size, Blocks, Last accessed, modified, changed

"./fsize":
(2049, 26093828, 100775 (-rwxrwxr-x), 1, 1000, 1001,
17248 bytes, 4096, 40 blocks)
Last accessed: Wed Sep 6 13:13:48 2023
Last modified: Wed Sep 6 13:13:47 2023
Last status change: Wed Sep 6 13:13:47 2023

"./inode":
(2049, 26093831, 100775 (-rwxrwxr-x), 1, 1000, 1001,
17416 bytes, 4096, 40 blocks)
Last accessed: Wed Sep 6 21:24:02 2023
Last modified: Wed Sep 6 21:23:52 2023
Last status change: Wed Sep 6 21:23:52 2023

"./fsize.c":
(2049, 26093825, 100664 (-rw-rw-r--), 1, 1000, 1001,
1866 bytes, 4096, 8 blocks)
Last accessed: Wed Sep 6 20:40:57 2023
Last modified: Wed Sep 6 20:40:57 2023
Last status change: Wed Sep 6 20:40:57 2023

"./inode.c":
(2049, 26093830, 100664 (-rw-rw-r--), 1, 1000, 1001,
3582 bytes, 4096, 8 blocks)
Last accessed: Wed Sep 6 21:23:52 2023
Last modified: Wed Sep 6 21:23:48 2023
Last status change: Wed Sep 6 21:23:48 2023

".":
(2049, 26093811, 40775 (drwxrwxr-x), 2, 1000, 1001,
4096 bytes, 4096, 8 blocks)
Last accessed: Wed Sep 6 21:18:02 2023
Last modified: Wed Sep 6 21:23:52 2023
Last status change: Wed Sep 6 21:23:52 2023


ls -ails
total 60
26093811 4 drwxrwxr-x 2 user user 4096 Sep 6 21:24 .
26094441 4 drwxrwxr-x 6 user user 4096 Sep 4 14:37 ..
26093828 20 -rwxrwxr-x 1 user user 17248 Sep 6 13:13 fsize
26093825 4 -rw-rw-r-- 1 user user 1866 Sep 6 20:40 fsize.c
26093831 20 -rwxrwxr-x 1 user user 17416 Sep 6 21:23 inode
26093830 8 -rw-rw-r-- 1 user user 4829 Sep 6 21:24 inode.c


./inode .. // parent directory

ls -Rails ..
*/





Note:  See also GitHub-kr_(8.5) for printFlags() and clc-wiki-kr_(8.5).









Chapter_8     Exercise_8-4     fsize BACK_TO_TOP malloc     Exercise_8-6



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo