首页
编程语言
数据库
网络开发
Algorithm算法
移动开发
系统相关
金融统计
人工智能
其他
首页
>
> 详细
辅导CS 4323、讲解Operating Systems、R编程设计调试、辅导R 辅导Python编程|辅导Python编程
Programming Project 3
CS 4323 Operating Systems
Spring 2019
Instructor: Fred Wu Office: 215 e-mail: cnhengwu@gmail.com
Instructor Office Hours: 10:00 -12:00 PM on Monday and Wednesday
In this programming project, you are asked to implement a simplified version of the find?
utility on Linux system. This assignment assists you for better understanding of file
systems design, how to use the Linux's system calls, and enhancing programming skills
and experience with programming on a Unix-like environment.
Description
The find utility is used to locate files on a Unix or Linux system. find will search any set
of directories you specify for files that match the supplied search criteria. You can search
for files by name, owner, group, type, permissions, date, and other criteria. The search is
recursive in that it will search all subdirectories too. The syntax looks like this:
$find where-to-look criteria what-to-do
Requirements
You need to implement the following functionalities. You can compare the output of your
program with the output of the standard find utility provided on Linux.
1. find where-to-look
2. find where-to-look criteria
a. find where-to-look -name
b. find where-to-look -mmin
c. find where-to-look -inum
3. find where-to-look criteria -delete
Extra credit:
4. find where-to-look criteria -exec command
Details of Each Functionality
1. find where-to-look
This will display the pathnames of all files in the specified directory and all
subdirectories. e.g. (if no directory specified, the default is the current working
directory)
$ find Document
You will get the output like:2
Document/file1
Document/file2
Document/subfolder/file3
2. find where-to-look criteria
2.1 find where-to-look -name
This will search the specified directory (where-to-look) and all subdirectories for any
files named
and display their pathnames. e.g.
$ find Document –name foo
Here we are using the criterion -name with the argument foo to tell find to perform a
name search for the filename foo. The output might look like this:
Document/wpollock/foo
Document/ua02/foo
Document/foo
If find doesn't locate any matching files, it produces no output.
2.2 find where-to-look -mmin
This will find those files modified with the specified number of minutes ago
You can specify a number “n” to mean exactly n, “-n” to mean less than n, and “+n”
to mean more than n.
$ find Document -mmin -10
This is used to locate files modified less than 10 minutes ago
2.3 find where-to-look -inum
Find a file that has i-node number n.
$ find Document -inum n
3. find where-to-look criteria -delete
This is an example of usage "find where-to-look criteria what-to-do". This will find
files with specified criteria and delete them; e.g.
$ find Document -name foo -delete
$ find Document -mmin -10 -delete
Extra credit:
4. find where-to-look criteria -exec command
This will find files with specified criteria and execute the specified command; e.g.
$ find Document -name foo -exec cat (this should find the file with a name “foo” in
the specified directory and output the content of the file by executing the “cat”
command on the file; this should be equivalent to “$ find Document -name foo -exec
cat {} \; ” on the Oak machine)
$ find Document -name foo -exec rm (this should find the file with a name “foo” in
the specified directory and delete the file; this should be equivalent to “$ find
Document -name foo -exec rm {} \; ” on the Oak machine)3
$ find Document -name foo -exec mv
(this should find the file with a
name “foo” in the specified directory and rename to a new name; this should be
equivalent to “$ find Document -name foo -exec mv {}
\;” on the Oak
machine)
If you are able to implement and support the above three commands (cat, rm,
mv), you can score the extra credit.
Sample Codes and Hints
Before you start, you can create a testing directory under your home directory, this will
help you debugging your code and better understanding the routine of each function.
Follow the steps below to create a test directory:
Table 1, Create a Test Directory
After typing the above 18 commands in Table. 1, you will have a simple directory
structure.
Then, by typing:
you will get a tree of all the files, as shown in the below:
Figure 1. Tree structure of all the files in a directory
This is a typical directory structure in Linux file system. From the Fig.1, we can see that
there are files and subdirectory within a directory.Therefore you can imagine that the
find utility is probably a recursive routine.
But let?s see what we missed in a directory,
ls –al –R testdir
you will see something like:
jialin@jaln:~$ ls -al -R testdir/
testdir/:
total 24
drwxr-xr-x 4 jialin jialin 4096 2013-04-14 00:53 .
drwxr-xr-x 73 jialin jialin 12288 2013-04-14 01:50 ..
drwxr-xr-x 3 jialin jialin 4096 2013-04-14 00:52 dir1
drwxr-xr-x 2 jialin jialin 4096 2013-04-14 00:53 dir4
-rw-r--r-- 1 jialin jialin 0 2013-04-14 00:52 test1
-rw-r--r-- 1 jialin jialin 0 2013-04-14 00:52 test2
testdir/dir1:
total 12
drwxr-xr-x 3 jialin jialin 4096 2013-04-14 00:52 .
drwxr-xr-x 4 jialin jialin 4096 2013-04-14 00:53 ..
drwxr-xr-x 2 jialin jialin 4096 2013-04-14 00:53 dir2
-rw-r--r-- 1 jialin jialin 0 2013-04-14 00:52 test3
-rw-r--r-- 1 jialin jialin 0 2013-04-14 00:52 test4
testdir/dir1/dir2:
total 8
drwxr-xr-x 2 jialin jialin 4096 2013-04-14 00:53 .
drwxr-xr-x 3 jialin jialin 4096 2013-04-14 00:52 ..
-rw-r--r-- 1 jialin jialin 0 2013-04-14 00:53 test5
-rw-r--r-- 1 jialin jialin 0 2013-04-14 00:53 test6
testdir/dir4:
total 8
drwxr-xr-x 2 jialin jialin 4096 2013-04-14 00:53 .
drwxr-xr-x 4 jialin jialin 4096 2013-04-14 00:53 ..
-rw-r--r-- 1 jialin jialin 0 2013-04-14 00:53 test7
-rw-r--r-- 1 jialin jialin 0 2013-04-14 00:53 test8
Figure 2. All Contents in A Directory
Notice that there are some hidden files starting with . or ..
Be careful about that!
Studying the basic directory structure and the contents in a directory is a good start for
you to implement the find utility. The following code (Table 2) recursively prints all the
file names in a directory, you may need to learn and pick up some useful system calls
from the codes and then implement other find utilities.
0 /*
1 *A function that recursively print all file names
2 *Input: directory name, i.e., char * sub_dir5
3 *Output: all file names
4 */
5 void read_sub (char* sub_dir)
6 {
7 DIR *sub_dp=opendir(sub_dir);//open a directory stream
8 struct dirent * sub_dirp;//define
9 struct stat buf;//define a file status structure
10 char temp1[]=".";
11 char temp2[]="..";
12 char temp3[]="/";
13 if(sub_dp!=NULL)
14 //check whether the directory stream is opened successfully
15 {
16 // read one entry each time
17 while((sub_dirp=readdir(sub_dp))!=NULL)
18 {
19 //print the first entry, a file or a subdirectory
20 printf("%s\n",sub_dirp->d_name);
21
22 //check whether the first entry is a subdirectory
23 char * temp =sub_dirp->d_name;
24
25 //to avoid recursively searching . and .. in the directory.
26 if(strcmp(temp,temp1)!=0&&strcmp(temp,temp2)!=0)
27 {
28 char *temp_sub=temp3;
29 temp_sub=strcat(temp_sub,temp);
30 //now you add the / in front of the entry’s name
31 char* temp_full_path=malloc(sizeof(char)*2000);
32 temp_full_path=strcpy(temp_full_path,sub_dir);
33 strcat(temp_full_path,temp_sub);
34 //now you get a full path, e.g., testdir/dir1 or testdir/test1
35
36 // try to open
37 DIR * subsubdp=opendir(temp_full_path);
38 //if not null, means we find a subdirectory, otherwise, its just a file
39 if(subsubdp!=NULL){
40 //close the stream, because we will reopen it in the recursive call.
41 closedir(subsubdp);
42 read_sub(temp_full_path);//call the recursive function call.
43 }
44 }
45 }//end of while loop
46 closedir(sub_dp);//close the steam
47 }
48 else
49 {
50 printf("cannot open directory\n");
51 exit(2);
52 }
53 }
Table 2. Sample Codes for Printing All File Names
There are several system calls you need to know in your program.
1. DIR *opendir(const char *name) at line 7, 37
The opendir() function opens a directory stream corresponding to the directory name,
and returns a pointer to the directory stream. The stream is positioned at the first entry 6
in the directory.
2. struct dirent *readdir(DIR *dirp) at line 17
The readdir() function returns a pointer to a ?dirent? structure representing the next
directory entry in the directory stream pointed to by ?dirp?. It returns NULL on
reaching the end of the directory stream or if an error occurred.
3. The ?dirent? structure defines a file system independent directory entry, which
contains information common to directory entries in different file system types. The
dirent structure is shown in Figure 3 as follows:
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all file system types */
char d_name[256]; /* filename */
};
Figure 3. Strucutre of Dirent
Note that we have used the d_name in the sample codes at line 20 and 23.
4. Another system call you need is stat()
int stat(const char *path, struct stat *buf);
This system call returns information about a file. No permissions are required on the
file itself.Stat is also a struct in Linux system. The structure of stat is shown in the
Figure 4:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
Figure 4. Structure of Stat
Why do we need the stat() call and stat struct?Since you are required to implement a
find utility like
$ find where-to-look -inum
The stat contains the information of i-node and other needed stuff.
How to use it?Recallthat we have the path name in line 20, and we defined a file
status structure in line 9. Then to print the file size in bytes, we can use the following
codes:
struct stat buf;//define a file status structure
if(stat(sub_dirp->d_name,&buf)==0)
printf("%d ", (int)buf.st_size);7
Figure 5. Print File Status
5. Remove() will be needed
To implement the ?find where-to-look criteria –delete? function, you will need
remove():
remove(file_name);
6. For parsing options and arguments, you can directly manipulate argv, or you can
consider using getopt()/getopt_long()/argp_parse() supplied by the GNU C library. A
sample code getopt.c is provided as an example of showing how getopt() is used. You
can always Google to find more details of these command line options and arguments
parsing functions.
Expected Submission:
You should submit a single tarball/zipped file through the Blackboard containing the
following:
Source codes
Output files for your test cases
Grading Criteria:
Grade Criteria
10 Inline comments to briefly describe your code
20 Implement the find where-to-look?
30 Implement the find where-to-look criteria?
20 Implement the find where-to-look criteria -delete?
10 (extra
credit)
Implement the find where-to-look criteria -exec command?
20 Correctness of result. Source code can be compiled and executed.
Reference Materials:
Linux system programming:
Book: Linux System Programming
Online:
Tutorial for Beginners, http://www.ee.surrey.ac.uk/Teaching/Unix/
Advanced Linux Programming, http://www.advancedlinuxprogramming.com/alpfolder/advanced-linux-programming.pdf
Linux man pages, http://linux.die.net/man/
Stackoverflow, http://stackoverflow.com/
Codewiki, http://codewiki.wikidot.com/start8
联系我们
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-21:00
微信:codinghelp
热点文章
更多
辅导 cs1b spring 2024 tth hw...
2024-04-19
讲解 managing financial risk...
2024-04-19
辅导 cs 0449 – project 5: /...
2024-04-19
辅导 elec 2141 digital circu...
2024-04-19
讲解 csc171 — videogame pro...
2024-04-19
讲解 comp3411 artificial int...
2024-04-19
讲解 stat3061: random proces...
2024-04-19
辅导 accounting 452, spring ...
2024-04-19
辅导 finc5001 foundations in...
2024-04-19
辅导 7ssmm712 – topics in a...
2024-04-19
讲解 com 337 - film studies ...
2024-04-19
辅导 mes202tc - digital vlsi...
2024-04-19
辅导 geography 2041b distanc...
2024-04-19
辅导 ecos3006 international ...
2024-04-19
讲解 fit5225 2024 sm1 creati...
2024-04-19
讲解 cit 593: introduction t...
2024-04-19
讲解 math 4931: take home ex...
2024-04-19
辅导 csci 547|info 533: syst...
2024-04-19
辅导 cs536-s24 intro to pls ...
2024-04-19
讲解 fit5212 - assignment 1辅...
2024-04-19
热点标签
cs 161
swen20003
comp282
csc1002
comp27112
comm1190
elec9764
acfi3308
acct7101
fin6035
comp2048
geog0163
comp2013
coen 146
dts101tc
comp4880/8880
cs 455
07
comp10002
comp30023
sehh2042
stat0045.
fil-30023
celen085
psyc40005
math40082
are271
comp9311
ee5311
imse2113
comp 2322
acct2102
fnd109
int102
is3s664
is6153
data4000
accfin5034
fit5212
cs536-s24
fit5225
ecos3006
mes202tc
finc5001
stat3061
csc171
cs1b
7ssmm712
bu.450.760
comp3411
cs170
swen90004
cpt206
comp5313/comp4313—large
bl5611
kxo206
comp532
elec207
kxo151
cs 2820
cpt108
math2319
dts204tc
qm222
comp2511
ccs599
infs1001
mat2355
eeee4123
25721
ifn647
pols0010
hpm 573
comp9417
stat0023
csci 1100
qbus6860
comp2003j
cse340
cs 2550
cs 61b
cs360
fin 3080
ierg 4080
cs6238
cit 594
finm7406
hw6
elec9713
asb-2522
mso3610
lit301
mcd4540
geog0030
125.330
biol0006
125.320
cs3334
fit2093
acct1101
110.309
masy1-gc
cs314
elec0048
gds104
mg5637
fit2096
math5905
eel4837
sehs4515
cpt s 321
asb2522 investment
ma214
co2104
mgmt2015
32516
math32051
econ1012
mark2052
comp3310
econ0019
dsci 525
abmf3184
aps106
antc27
finm7401
itp122
tech2300
math3026
comp9024
cao107
36318
is2022
cs 211
fit1047
ics4u
2xc3
en.540.635
4qqmn506
finn3081
phys10362
sta601
ec481e
math5165
csi 2120
el1205
comp7250
ecos3013
beam065
info1113
comp2051
csc325
mne 6130
ai6126
ecs150
is61x6
cse115
seng6110
bus265
cpts260
mphy0009
csc306
eco2011
ee3004
inu1111
st332
idepg001
info6001
cpt106
finm7409
fit3152
fins5516
qbus2820
isom3028
eece 6083
ceg5304
mcd4700
eecs 493
eg25h4
38173
elc5216
infs6071
lubs5996m
7ssmm803
glbh0031
phys1120
comp52715
eeb240
math3836
cmns3490
iy5610/4610
cpt304
ac6105
psyc3241
fin570
218.323
lng310
rim3352
bio206
comp3334
联系我们
- QQ: 99515681 微信:codinghelp
© 2024
www.7daixie.com
站长地图
程序辅导网!