As you are aware, we have lab assignments due every week. We also will have several homework assignments this semester, and this is one of those. While the labs are directed, the homework assignments present larger problems that you should break down into smaller problems. You should spend some time thinking about and planning your homework solutions before coding them.
ls
.
See the "processDirectory" function from Glass and Ables' Unix book, in their "monitor.c" example (starting on page 453). The "processDirectory" uses the "readdir" function to read a directory's contents, where the directory is specified by a char pointer. Also see this reference for stat, the file status function. It is recommended that you start with the "processDirectory" function, and make a program around it to understand how it works. Then add in elements of the "stat" structure and command, as needed. (Note: use the "stat" command, not one of the variants like "lstat". You can experiment with the other forms, but the work that you turn in should use "stat".) Make sure that you clearly indicate which lines are from the examples.
If the user passes "-h" to your program, print out some help information and exit. The help information should include the name of your program, your name, what the program does, and what the other arguments do. It should give an example on how to use it. It is possible for the user to pass "-h" among other arguments. If this happens, treat it as if only "-h" were passed.
If the user passes "-s" to your program, print out a short amount of information about the files/directory.
If a name is passed as an argument to your program, use that as the name to list. If it is a filename, indicate that, and print the associated information. If it is a directory name, indicate that, and print the associated information of the files in that directory. If there is no name passed, then assume that it should list the files in the current directory.
By default, your program should print the name of the file, the user ID number, the group ID number, its type, the size in bytes, and the last modification timestamp.
Here is an example, where "pictures" is a directory.
$ ./hmwk4_mweeks pictures pretzel.JPG, user 501, group 20, regular file, 57343 bytes, Sun Aug 27 18:16:28 2024 IMG_8211.jpg, user 501, group 20, regular file, 1976059 bytes, Thu Jun 15 17:03:21 2024 IMG_8212.mov, user 501, group 20, regular file, 30388195 bytes, Thu Jun 15 17:03:25 2024
Here is another example.
$ ./hmwk4_mweeks hmwk4_mweeks.c hmwk4_mweeks.c, user 501, group 20, regular file, 4015 bytes, Sun Sep 29 10:52:01 2024
If the "-s" option is used, print only the name of the file(s), its type, and the size in bytes. Here is an example.
$ ./hmwk4_mweeks -s pictures pretzel.JPG, regular file, 57343 bytes IMG_8211.jpg, regular file, 1976059 bytes IMG_8212.mov, regular file, 30388195 bytes
It is possible for the user to give an argument that is not a valid file or directory. In that case, print an error indicating the name of the file with double quotes around it, such as "file "name" not found" and exit with an appropriate code.
Use several functions and keep the code in "main" to a minimum. The "main" function should process the arguments, and print the help information if needed. Then it should call other functions to handle the work.
Notes: