Your task is to write the following C programs: •ls: list contents of a directory •find: find files/directories matching a pattern •tree: print files/directories as a tree Notes: •Please be careful to follow the I/O formats exactly. •You can assume that inputs will be correctly formatted and all file/directory names will consist only of letters, numbers, and periods. For the input/output examples given in this document, assume you have the following directory structure: "image" ls with no command-line options The ls command prints all files/directories in the current working directory. Running it in your hw2 directory, you might see this output: find.c ls.c Makefile tree.c The files/directories should be sorted in case-insensitive lexicographic order: .<0 <1 <···<9

COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
1st Edition
ISBN:9780357392676
Author:FREUND, Steven
Publisher:FREUND, Steven
Chapter7: Creating Templates, Importing Data, And Working With Smartart, Images, And Screenshots
Section: Chapter Questions
Problem 4EYK
icon
Related questions
Topic Video
Question

Your task is to write the following C programs:
•ls: list contents of a directory
•find: find files/directories matching a pattern
•tree: print files/directories as a tree
Notes:
•Please be careful to follow the I/O formats exactly.
•You can assume that inputs will be correctly formatted and all file/directory names will consist only of letters, numbers, and periods.
For the input/output examples given in this document, assume you have the following directory structure:

"image"

ls with no command-line options
The ls command prints all files/directories in the current working directory.
Running it in your hw2 directory, you might see this output:
find.c
ls.c
Makefile
tree.c
The files/directories should be sorted in case-insensitive lexicographic
order:
.<0 <1 <···<9 <a <b <···<z


ls with the -lcommand-line option
If the user invokes ls -l (lowercase “l” for “long”), you should print a “long
format” with extra information about each file.
-rw-rw-r-- bob users 1562 Sep 29 12:00 find.c
-rw-rw-r-- bob users 1024 Sep 29 12:05 ls.c
-rw-rw-r-- bob users 176 Sep 28 11:27 Makefile
-rw-rw-r-- bob users 2044 Sep 27 18:23 tree.c


The first column contains a 10-character permissions string.

•the first character is ‘-’ for files, ‘d’ for directories
•the next three are read, write, and execute permissions for the user
•the next three are read, write, and execute permissions for the group
•the next three are read, write, and execute permissions for others
Permissions should be denoted by a ‘r’, ‘w’, or ‘x’ if present, or ‘-’ otherwise.

The second and third columns show the file owner’s user name and group name (or user ID / group ID if a name can’t be found).
The next column shows the file size in bytes.
Next is the file’s modification time (mtime) formatted as in the example above.
Finally the filename is listed.
You may find strftime, getpwuid, and getgrgid useful.

find
The find command takes a pattern as a command-line argument and recursively searches through directories to find a filename matching that pattern. It should print a relative path starting with “./” for every file/directory that matches.
For example, running ./find ls.c from within the hw2 directory should print ./ls.c. If run from the parent directory, the output would be ./hw2/ls.c. There may be multiple matches. If we run ./find .c from within the hw2 directory, we should see:
./find.c
./ls.c
./tree.c
The output does not need to be sorted. If nothing matches, it shouldn’t print anything. Pattern matching should be case sensitive.

tree
The tree command prints all files/directories contained in the current directory as a tree. If we run ./tree from the cs214 directory, we should
see:
.
- hw1
     -factor.c
     - grep.c
     - monster.c
     - sort.c
     - uniq.c
- hw2
      - find.c
      - ls.c
      - Makefile
      - tree.c
The first line of output is always “.” to denote the current directory. tree recurses into directories to print all files/directories within them. For each subdirectory, two spaces of indentation are added. For example, if we had a file hw1/test/src/foo.c, tree would print:
 
.
- hw1
     - test
           - src
               - foo.c
Files/directories within each subdirectory should be sorted with the same lexicographic order as in ls.
cs214
hw1
factor.c
grep.c
monster.c
sort.c
uniq.c
hw2
find.c
.ls.c
Makefile
tree.c
Transcribed Image Text:cs214 hw1 factor.c grep.c monster.c sort.c uniq.c hw2 find.c .ls.c Makefile tree.c
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L