Question: I need help with this assignment. It is for operqating systems and it is in C. Assignment 4: Files in UNIX Objective: The objective of
I need help with this assignment. It is for operqating systems and it is in C.
Assignment 4: Files in UNIX
Objective: The objective of this assignment is to understand the UNIX file system in general, and the structure of i-node and the stat system call in particular. You will also have an opportunity to use system calls that deal with directories such as opendir, closedir and readdir.
Problem: You will write a program that, simulates the behavior of the UNIX command "ls -1". This UNIX command basically gives a long listing of the files in a specific directory. It will produce one line of output for each file/directory in the directory. This line will tell you the permissions that user, group and others have to read, write and execute the file, the number of hardlinks to the file, the login name of the owner, the group name to which the file belongs, the number of characters in the file, the last time the file's contents were modified and last but not least, the name of the file/directory. For example, some typical lines are shown below.
drwxr-xr-- 1 gopal staff 7783 Feb 19 09:57 junk
-rw-r--r-- 1 gopal staff 18806 Feb 2 1998 zero.tex
In UNIX systems, a directory is basically a file consisting of several lines, one per each file/directory present there. The information in there would simply be the name of the file/directory and its inode number. You can get additional information about the file from the actual inode. Fortunately, there is a system call by name stat that will do this job.
So, basically you have to open the specified directory using the opendir system call, read the entries in it one by one using the readdir system call, get additional information about each entry using the stat system call, format and print a line of output for each entry and finally close the directory specified using the closedir system call.
You can obtain almost all the information you need from the man pages for the system calls opendir, readdir, closedir and stat. The main structure one has to be concerned with when getting information about files is struct stat. struct stat is defined in the header file stat . h. You can get a pointer to stat structure by making the system call stat (there are two other variations of the stat system call, lstat and fstat. but the one we will use is stat. You can check out the others also using the man pages). Also, the library function ctime will come in handy to convert the time in seconds since the Epock (number of seconds since midnight Jan 1, 1970) to local time in the ordinary form. Look at the man pages for these system calls and the header files themselves for additional information.
Try to make you program myls work exactly like the UNIX commond ls l. In particular, the output should be sorted based on the name of the file. You can either sort them yourselves or you can use the sort command of unix to get them sorted without any work on your apart. You might want to worry about sorting after you have taken care of the rest of the detail. The time of modification should just be month, date and time. If myls is invoked with no arguments it should list the files in the current working directory. If myls is invoked with one argument and this argument is the name of a directory, then the files in that directory should be listed. If myls is invoked with one argument and this argument is a file, simply print information about this file. You can assume that more than one argument will not be given to the program (even though Is takes multiple arguments). Also, dont worry about wild care substitution features.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
