Operating System代写:COMP3080-File-Description

Requirement

The program you will write will accept from 0 to an arbitrary number of command line file names and produce an output structure for each identified file (or all files in the working directory if no command line file names are listed) as shown below:

1
2
3
4
5
6
7
8
9
FILENAME
FILE_TYPE
PERMISSIONS
OWNER_NAME
GROUP_NAME
DATE_OF_LAST_MODIFICATION
LINK_COUNT
SIZE_IN_BYTES OR DEV INFO
INODE_NUMBER

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FILENAME:
alpha
FILE_TYPE:
ordinary
PERMISSIONS:
rw- r-- r--
OWNER_NAME:
jedwards
GROUP_NAM:
grad
DATE_OF_LAST_MODIFICATION:
Mar 30 08:11 2003
LINK_COUNT:
2
SIZE_IN_BYTES:
1345 (or 12, 6 dev info)
INODE_NUMBER:
347
*******< a blank line between entries >*******

System calls needed on a UNIX system include:

1
2
#include <dirent.h>
getdirentries(int fd, char *buf, int nbytes, long *basep)

which reads up to nbytes of data into buf in the form:

1
2
3
4
unsigned long   d_ino;
unsigned short d_reclen;
unsigned short d_namlen;
char d_name[MAXNAMLEN + 1];

see the man pages for more detail. This routine is difficult to use, so you may find the library routines opendir() and readdir() easier to use as shown in class.

1
2
#include <time.h>
char *ctime(long *clock)

see the man pages for more detail. This data structure may vary somewhat from platform to platform (see the stat.h header and its #includes), but the entry names shown above are common to all Unix/Linux type platforms

The getdirentries() call requires that you use the open() system call to open a directory, and you can then use getdirentries() to extract filenames from the directory. (You may want to check out the library routines opendir() and readdir(), which will do this for you in a more friendly way.) Your program will have to work in two basic modes:

  • if called with no arguments (as with ls) it must find the names of all the files in the current directory (including dotted files) and print information in the format shown above for each file object.
  • if called with a series of file names (from the command line as with ls abc xyz etc) it must print information in the format shown above for each named object in the argv [ ] vector (wildcard characters are not allowed).

File types include ordinary (-), directory (d), symbolic link (l), character device (c), and block device (b). You must show sample output with each of these types. (You do not have to worry about pipe (p) and UNIX domain socket (s) types, nor do you have to print resolution names for symbolic link (l) types.) There are several additional library routines and header file macros and defined constants that can help you get this done.

If you compile your code on mercury, you will need to define a compile time symbol to work properly with NFS mounted file objects:

1
Bash-$  gcc -D_FILE_OFFSET_BITS=64 -g -o stat stat.c

You won’t need this if you compile on cs but it will not hurt to include it either way (cs is a 64 bit Linux, while mercury is a 32 bit Linux)