LS - Unix Command
Supported Flags: -R -t -u -l -f -d -G -r -a
- -R : Recursive function for reading all files and repos within the repo passed
- -t : Sort files by time last modified using st_mtime , st_mtimespec.tv_nsec , or by lexicographic order
- -u : Sort files by time last accessed using st_atime , st_atimespec.tv_nsec , or by lexicographic order
- -l : Long Format prints out metadata consisting of Type , Permissions , Number Of Links, User Owner , Group Owner , File Size , Time Last Modified.
- Type of file uses: with a stat struct's st_mode as the parameter.
- S_ISDIR() - Directory
- S_ISSOCK() - Socket File
- S_ISFIFO() - FIFO pipe file
- S_ISCHR() - character special device.
- S_ISBLK() - Block Devcies
- S_ISREG() - Regular Files
- S_ISLNK() - Symbolic Link
- Permissions: Bitwise check using st_mode and macro value (st_mode & S_IRUSR)
- S_IRUSR - User Read : 0400
- S_IWUSR - User Write : 0200
- S_ISUID - User Set-ID : 04000
- S_IXUSR - User Execute: 0100
- S_IRGRP - Group Read: 040
- S_IWGRP - Group Write: 020
- S_ISGID - Group Set-ID: 02000
- S_IXGRP - Group Execute: 010
- S_IROTH - Other Read: 04
- S_IWOTH - Other Write: 02
- S_ISVTX - Sticky Bit: 01000
- S_IXOTH - Other Excecute: 01
- Number of Links: Uses the st_nlink value from the stat struct
- User Owner: Uses getpwuid() with the stat structs value of st_uid for the parameter
- Group Owner: Uses getgrgid() with the stat structs value of st_gid for the parameter
- File Size
- Normal File: Uses the st_size value from the stat struct
- Character Special Devices or Block Devices: Use makedev() function with major() and minor() return values as a parameters st_rdev value is passed as a paramter for major() and minor() functions used.
- Example: size = makedev(major(statstruct.st_rdev), minor(statstruct.st_rdev))
- Time Last Modified: uses the time(NULL) to get current time. Then uses ctime() with the stat structs st_mtime vlaue as a parameter.
- Not modifed longer than 6 months: Month, Date, Year
- Modifed within past 6 motnhs: Month, Day, Time
- -f : Do not sort files. Overrides any sort passed.
- -d : Print directories name, do not read files from it.
- -G : Print color depending on file type. Uses ANSI Escape Codes to set color using write
- Normal Files: No color
- Directory: BLUE \033[34m
- Excuctable: RED \033[31m
- Symbolic Link: MAGENTA \033[35m
- Socket File: GREEN \033[32m
- FIFO pipe: YELLOW \033[33m
- Character Device: BLUE YELLOW \033[34;43m
- Block Device: BLUE CYAN \033[34;46m
- Sticky Bit: BLACK GREEN \033[30;42m
- Is User Set-ID & Excuctable: BLACK RED \033[30;41m
- Is Group Set-ID & Excuctable: BLACK CYAN \033[30;46m
- -r : Reverse sort
- -a : List hidden files
Reading Directories
This program uses a struct called DIR , a struct called dirent , opendir() , readdir() , and closedir() in the dirent.h library to obtain file names within a directory
Things Covered
Using readdir() system call
Calling stat struct values using lstat() within the sys/stat.h standard C library's POSIX API
Using lstat(path, &statbuf) to call meta data
Different types of files and repos within the unix filesystem
Bubble sort and Merge sort
Makdev() system call to create hex values for device drivers
Extracting permissions using lstat()
Accessing time last modified and time last accessed using system calls for file
Recursive functionality reading through the entire repo
Sorting files times using tv_msec and tv_nsec
Much more!