Reference no: EM132159787
Lab - File I/O Utility Programs
In this assignment you will create your own versions of:
# ls -l
# find <filename> <starting directory>
# grep <string> <filename>
Each of these programs will exercise different system and standard I/O functions but more importantly they will all work on different aspects of a file system and the differences in different types of files.
# ls -l
The "ls" command will require you to use directory functions to list out what the filenames are in the directory. Functions required to make a "ls" command work are included in the <dirent.h> header file ( man dirent ). You will need to open a directory and iterate through the directory until you hit the end and display the filenames in that current directory.
An example of what you get when you iterate through a directory is a structure like below:
struct dirent {
ino_t d_ino; /* file number of entry */
uint16_t d_reclen; /* length of this record */
uint8_t d_type; /* file type, see below */
uint8_t d_namlen; /* length of string in d_name */
char d_name[255 + 1]; /* name must be no longer than this */ };
NOTE: you have the inode and name of file here....
Now, the requirement for this piece of the lab requires an example of "ls -l" where you display most of the inode information on the file inclusive of:
UID of owner (this will be a number) GID of group (this will be a number) Time of last data modification File Size Blocks allocated for file
Permissions (this is in the mode or protection mode bits, so some bit fiddling is required)
To find this data you will need to use one of the "stat" functions (man 2 stat).. You will get a structure similar to:
struct stat {
dev_t st_dev; /* device inode resides on */
ino_t st_ino; /* inode's number */
mode_t st_mode; /* inode protection mode */
nlink_t st_nlink; /* number or hard links to the file */
uid_t st_uid; /* user-id of owner */
gid_t st_gid; /* group-id of owner */
dev_t st_rdev; /* device type, for special file inode */
struct timespec st_atimespec; /* time of last access */
struct timespec st_mtimespec; /* time of last data modification */
struct timespec st_ctimespec; /* time of last file status change */
off_t st_size; /* file size, in bytes */
quad_t st_blocks; /* blocks allocated for file */
u_long st_blksize; /* optimal file sys I/O ops blocksize */
u_long st_flags; /* user defined flags for file */
u_long st_gen; /* file generation number */ };
# find <filename> <starting directory>
The find command will use the directory (dirent) function calls, but will require you to start at a directory and then determine if a file is a directory or a file and if it is a directory you will need to walk down that directory as well. You will need a method/algorithm to note the new directories and build a logical graph or thread out every directory to find if the filename is in that directory and display the "absolute" path to that file.
NOTE: you will only need to find exact string matches with NO wildcards or regular expression (regex) matches (that is too much for this one ;-)
# grep <string> <filename>
The grep command will take an exact string and an exact filename as the 2 arguments and you will need to search through the filename for the given string and display the "line" and "line number" the string resides in.
Attachment:- Assignment File.rar