ls -- list directory contents
- How to read entry and show directory contents?
- How to get file properties and how to show them?
- Give you a name, how to tell whether it is a file or a directory?
Hint: The directory is a special kind of file. It contains a lot of records, each with a uniform standard format. The content of each record represents a file or directory. The directory can be opened by a standard file function. The directory is not an unformatted text but contains a certain data structure.
- man -k direct | grep read # learn how to read dir file
- read mannul of readdir and get other information.
- learn about opendir/readdir/closedir/dirent.h and so on.
- Write your own ls command!
open directory
+-> read entry - end of dir? -+
|_ display file info |
close directory <----------------+
see ls1.c
Some other file attributes is not stored in dirent struct.
- get file stat, see
filestat.c - change the integer file mode into string like -rw-rw-r--. Use bit masking.
- change user/group id into username and groupname.
You can do by these steps: +--> man 3 stat man 3 getpwuid man -k uid | grep get man getgrgid
see ls2.c and use ls2
ls2.c cannot use cannot use specified directory as argument. Try to fix this bug.
see ls3.c and ls4.c
- sort: store all filename in an array and qsort it.
- The standard ls is output in columns. The solution: read the file name into an array, and then calculate the column width and number of rows.
- ls -a: Make ls output hidden files only when accepting option -a.