Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do i do this in C ? Create a program filestat that takes as a command line p

ID: 3797457 • Letter: H

Question

How do i do this in C ?

Create a program filestat that takes as a command line parameter thename of a directory, and for every file or directory in the passed directory print outthe contents of the stat structure associated in the following sample format:

...var

permissions: 0755

file type: directory -> private/var

major device: 1

minor device: 1

number of links: 24

user ID: 0

group ID: 0 size (bytes): 816

last access: Sat Dec 6 07:05:17 2014

last modification: Sun Nov 10 13:55:43 2013

last inode change: Sun Nov 10 13:55:43 2013

block size: 4096

number disk blocks: 0

Volumes permissions: 0777

file type: directory

major device: 1

minor device: 1

number of links: 4

user ID: 0

group ID: 80 size (bytes): 136

last access: Fri Dec 5 11:50:45 2014

last modification: Fri Dec 5 08:04:25 2014

last inode change: Fri Dec 5 08:04:25 2014

block size: 4096    

number disk blocks: 0...

You may want to make use of sprintf(3), strcpy(3), and ctime(3). Note thatdisplaying symbolic links correctly is a little tricky. Print all output to thestdout -- it is not necessary to open an output file as the shell makes it easy toredirect the output to a file:$ filestat /dir > stat.out

Explanation / Answer

The below program displays all the file information in a directory.


#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <err.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>

int main (int argc, char **argv)
{
   // create a pointer to DIR structure.
DIR *midir;
  
   // create a pointer to dirent
struct dirent* info_archivo;
  
   // create a type of stat defined in <sys/stat.h>
struct stat fileStat;
  
   // fullpath to hold the filename
char fullpath[256];

   // folder name should be given from command line.
if (argc != 2)
{
perror("Input folder name !! ");
exit(-1);
}

if ((midir=opendir(argv[1])) == NULL)
{
perror("Error in opendir");
exit(-1);
}

while ((info_archivo = readdir(midir)) != 0)
{
printf ("%s ", info_archivo->d_name);
strcpy (fullpath, argv[1]);
strcat (fullpath, "/");
strcat (fullpath, info_archivo->d_name);
      
       // this will print the permissions of the file in the given folder.
if (!stat(fullpath, &fileStat))
{
printf((S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf((fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf((fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf((fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf((fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf((fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf((fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf((fileStat.st_mode & S_IROTH) ? "r" : "-");
printf((fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf((fileStat.st_mode & S_IXOTH) ? "x" : "-");
} else
{
perror("Error in stat");
}
printf(" ");
      
       // below things are all self explanatory.
       // all the required elements are extracted from stat structure.
       printf("Last status change: %s", ctime(&fileStat.st_ctime));
       printf("Last file access: %s", ctime(&fileStat.st_atime));
       printf("Last file modification: %s", ctime(&fileStat.st_mtime));
       printf("Blocks allocated: %lld ", (long long) fileStat.st_blocks);
       printf("Number of (hard) links: %ld ", (long) fileStat.st_nlink);
       printf("Device containing i-node: major=%ld minor=%ld ", (long) major(fileStat.st_dev), (long) minor(fileStat.st_dev));
       printf("Ownership: UID=%ld GID=%ld ", (long) fileStat.st_uid, (long) fileStat.st_gid);
          
       printf(" ");
}
closedir(midir);
}

OUTPUT:
The above program will also print the information of all the files in a given folder.
All files includes hidden files also.

Note: all files including hidden files can be seen using the command ls -a folder_name

Below is the output.
~$ gcc file.c
~$ ./a.out te       // here te is the folder I am using
test.txt -rw-rw-r--
Last status change: Fri Feb 24 04:27:16 2017
Last file access: Fri Feb 24 04:27:16 2017
Last file modification: Fri Feb 24 04:27:16 2017

Blocks allocated: 8
Number of (hard) links: 1
Device containing i-node: major=8 minor=1
Ownership: UID=1000 GID=1000
. drwxrwxr-x
Last status change: Fri Feb 24 04:27:16 2017

Last file access: Fri Feb 24 04:27:23 2017

Last file modification: Fri Feb 24 04:27:16 2017

Blocks allocated: 8
Number of (hard) links: 2
Device containing i-node: major=8 minor=1
Ownership: UID=1000 GID=1000
.. drwxr-xr-x
Last status change: Fri Feb 24 04:26:53 2017

Last file access: Fri Feb 24 04:26:13 2017

Last file modification: Fri Feb 24 04:26:53 2017

Blocks allocated: 8
Number of (hard) links: 18
Device containing i-node: major=8 minor=1
Ownership: UID=1000 GID=1000
niranjan@ubuntu:~$ gcc sys.c
niranjan@ubuntu:~$ ./a.out te
test.txt -rw-rw-r--
Last status change: Fri Feb 24 04:27:16 2017

Last file access: Fri Feb 24 04:27:16 2017

Last file modification: Fri Feb 24 04:27:16 2017

Blocks allocated: 8
Number of (hard) links: 1
Device containing i-node: major=8 minor=1
Ownership: UID=1000 GID=1000
. drwxrwxr-x
Last status change: Fri Feb 24 04:27:16 2017

Last file access: Fri Feb 24 04:27:23 2017

Last file modification: Fri Feb 24 04:27:16 2017

Blocks allocated: 8
Number of (hard) links: 2
Device containing i-node: major=8 minor=1
Ownership: UID=1000 GID=1000
.. drwxr-xr-x
Last status change: Fri Feb 24 04:31:27 2017

Last file access: Fri Feb 24 04:31:27 2017

Last file modification: Fri Feb 24 04:31:27 2017

Blocks allocated: 8
Number of (hard) links: 18
Device containing i-node: major=8 minor=1
Ownership: UID=1000 GID=1000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote