Construct a C program that accepts the path (absolute or relative) to a UNIX dir
ID: 3836545 • Letter: C
Question
Construct a C program that accepts the path (absolute or relative) to a UNIX directory and produces a listing of all files. The listing provides for each file, the size in 512-byte disk units allocated (see description below), the time of last access, and the name. The program also prints the total disk usage for the entire directory, including the space allocated to the directory itself. The output of the program is verified against the output of /bin/ls -ul and /usr/bin/du -a commands. Please provide output.
For simplicity, assume that there are no subdirectories under the one specified in the command line argument. For formatted printing, assume that the file size requires at most 7 digits. The date and time indicated correspond to the last access of the file (24 characters long). Directories . and .. are not printed. However, the size of the directory . must be included in the total disk usage. Assume that a filename is at most 20 characters long. Leave 2 blanks between each of the three fields printed -- size, time, and name, in that order. Thus, each line is 55 characters long. Print all items left aligned. See sample output below. Please provide output.
The program is invoked as follows: dulist directoryPath
SAMPLE OUTPUT:
[51][test@courses2017:~/class412/asgn6]$ ls -ul .
total 20
-rwx------ 1 test faculty 9237 Apr 15 18:59 dulist
-rw------- 1 test faculty 1913 Apr 15 18:59 dulist.c
-rw------- 1 test faculty 1 Apr 15 18:54 oneCharFile
-rw------- 1 test faculty 0 Apr 15 18:53 zeroCharFile [52][test@courses2017:~/class412/asgn6]$ du -a .
4 ./dulist.c
12 ./dulist
4 ./oneCharFile
0 ./zeroCharFile
24 .
[53][test@courses2017:~/class412/asgn6]$ ./dulist .
8 Fri Apr 15 18:59:34 2016 dulist.c
24 Fri Apr 15 18:59:59 2016 dulist
8 Fri Apr 15 18:54:35 2016 oneCharFile
0 Fri Apr 15 18:53:49 2016 zeroCharFile
Total disk usage = 48 512-byte disk units.
Thanks a lot for your help!!! Please provide output and PROOF that your code is working thank you!
Explanation / Answer
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <grp.h>
int main(int argc, char **argv)
{
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d){
printf(" output ls-ul ");
while ((dir = readdir(d)) != NULL){
struct stat fileStat;
int file=0;
if((file=open(dir->d_name,O_RDONLY)) < -1)
return 1;
if(fstat(file,&fileStat) < 0)
return 1;
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" : "-");
printf(" %d ",fileStat.st_ino);
printf(" ");
printf(" %d ",fileStat.st_size);
printf(" ");
printf(" %d ",fileStat.st_nlink);
printf(" ");
//printf(" %s ", dir->d_name);
// Last accessed:
printf(" %s %s ",ctime(&fileStat.st_atime),dir->d_name);
//printf("Last modified: %s",ctime(&fst.st_mtime));
//printf("Last changed: %s",ctime(&fst.st_ctime));
printf(" ");
}
DIR *di = opendir(".");
struct dirent *dir2;
printf("Ouput du-a ");
while ((dir2 = readdir(di)) != NULL){
struct stat fileStat;
int file=0;
if((file=open(dir2->d_name,O_RDONLY)) < -1)
return 1;
if(fstat(file,&fileStat) < 0)
return 1;
printf(" %d ",fileStat.st_size);
printf(" ");
printf(" %s ",dir2->d_name);
printf(" ");
}
closedir(d);
}
return 0;
}
output
---------
output ls-ul
drwxrwxrwx -939647188 45 1 Tue May 9 19:39:48 2017
.
drwxrwxrwx -939647189 17 1 Thu Jan 1 00:00:00 1970
..
-rw-r--r-- 22054464 2557 1 Tue May 9 19:39:43 2017
main.c
-rw-r--r-- 22054466 199 1 Tue May 9 18:58:03 2017
.cg_conf
-rwxr-xr-x 22055627 8984 1 Tue May 9 19:39:48 2017
main
Ouput du-a
45 .
17 ..
2557 main.c
199 .cg_conf
8984 main