← 返回题目列表
UNSW COMP1521 · 困难

COMP1521 2022T2 Q7 · 目录遍历

COMP1521 2022T2 Q7 · 10 marks · C 知识点: 目录遍历(DIR) · 次: 递归/name+depth过滤 · 难度: 进阶 Topic: Filesystem / directories · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

一句话 / Summary: 递归查找符合name/深度条件的文件


You have been given 22t2final_q7.c : a C program with an empty implementation of the function 22t2final_q7 .

void _22t2final_q7 ( char * directory , char * name , int min_depth , int max_depth ) { // TODO }

Add code to the function 22t2final_q7 such that it recursively looks through the provided directory for files and directories that match the given criteria. If a file or directory is found that matches the given criteria, the path to that file should be printed out.

Note that if you find a directory that does not match the given criteria, you should still recursively search inside of it; just don't print it out.

The possible criteria are:

char *name :

If name is NULL , then this restriction does not apply.

Otherwise, before printing out any found file or directory, you must first check that the file or directory's name exactly matches the provided name.

You can find the name of a found file or directory through the d_name field in the struct dirent * returned by readdir .

int min_depth :

If min_depth is -1 , then this restriction does not apply.

Otherwise, before printing out any found file or directory, you must first check if the current search is at least min_depth directories deep.

You should keep track of your current depth through a recursive parameter.

The initial depth of the files directly inside the provided directory is 0 .

int max_depth :

If max_depth is -1 , then this restriction does not apply.

Otherwise, before printing out any found file or directory, you must first check if the current search is at most max_depth directories deep.

You should keep track of your current depth through a recursive parameter.

The initial depth of the files directly inside the provided directory is 0 .

Note that the order in which you print out found files and directories does not matter; your output is alphabetically sorted before autotest checks for correctness. All that matters is that you print the correct files and directories.

For example:

make 22t2final_q7 unzip 22t2final_q7_dirs.zip [lots of output] ./22t2final_q7 22t2final_q7_dirs 'a' 1 3 | sort 22t2final_q7_dirs/dir1/a 22t2final_q7_dirs/dir1/a/a 22t2final_q7_dirs/dir1/b/a 22t2final_q7_dirs/dir1/c/a 22t2final_q7_dirs/dir2/a 22t2final_q7_dirs/dir4/a ./22t2final_q7 22t2final_q7_dirs 'a' -1 1 | sort 22t2final_q7_dirs/a 22t2final_q7_dirs/dir1/a 22t2final_q7_dirs/dir2/a 22t2final_q7_dirs/dir4/a ./22t2final_q7 22t2final_q7_dirs 'a' 5 -1 | sort 22t2final_q7_dirs/dir2/deep/deep/deep/deep/deep/deep/deep/deep/deep/deep/a ./22t2final_q7 22t2final_q7_dirs 'e' | sort 22t2final_q7_dirs/dir1/a/e 22t2final_q7_dirs/dir1/b/e 22t2final_q7_dirs/dir1/c/e 22t2final_q7_dirs/dir4/e 22t2final_q7_dirs/e ./22t2final_q7 22t2final_q7_dirs '' 2 2 | sort 22t2final_q7_dirs/dir1/a/. 22t2final_q7_dirs/dir1/a/.. 22t2final_q7_dirs/dir1/a/a 22t2final_q7_dirs/dir1/a/b 22t2final_q7_dirs/dir1/a/c 22t2final_q7_dirs/dir1/a/d 22t2final_q7_dirs/dir1/a/e 22t2final_q7_dirs/dir1/b/. 22t2final_q7_dirs/dir1/b/.. 22t2final_q7_dirs/dir1/b/a 22t2final_q7_dirs/dir1/b/b 22t2final_q7_dirs/dir1/b/c 22t2final_q7_dirs/dir1/b/d 22t2final_q7_dirs/dir1/b/e 22t2final_q7_dirs/dir1/c/. 22t2final_q7_dirs/dir1/c/.. 22t2final_q7_dirs/dir1/c/a 22t2final_q7_dirs/dir1/c/b 22t2final_q7_dirs/dir1/c/c 22t2final_q7_dirs/dir1/c/d 22t2final_q7_dirs/dir1/c/e 22t2final_q7_dirs/dir2/deep/. 22t2final_q7_dirs/dir2/deep/.. 22t2final_q7_dirs/dir2/deep/deep

You may find the functions: opendir(3) , readdir(3) , closedir(3) useful.

You may assume that you have read+execute permissions on the provided directory and all directories found within it.

You may assume that you have read permission on any files found within it the provided directory.

22t2final_q7_main.c includes a main function to help you test your code.

You may not change the provided 22t2final_q7_main.c file.

You may not change 22t2final_q7 's prototype.

i.e. : its return type and its argument types.

You may define and call your own functions if you wish.

Your solution must be in C only.

Your solution must not be in MIPS , C++ , Python , or any other language.

Your program cannot call external programs.

Your program cannot use any of: fork() / exec() , posix_spawn() , system() , or any other similar functions.

No error checking is necessary or required.


🤖 Adapted: write a COMPLETE C program. Args <directory> <name> <min_depth> <max_depth>. name=- means any name; depth -1 means no bound. Entries directly inside directory are depth 0; recurse into every sub-directory regardless of match. Print each matching file/dir path (as directory/.../entry), one per line, sorted.

Verified on the sandbox AND cross-checked against an independent oracle.