Files and Directories: ls and touch continued
Prof. Brian D. Davison
Computer Science & Engineering, Lehigh University
Announcements
- Friday is Exam #2. Topics include:
- Coding style
- Debugging tools and techniques
- Bash scripting
- UNIX programming
- ... bring questions to Wednesday's class!
- Today's schedule
- Collect homework #5
- Return Short Quiz #8: Range 3-10, Average 6.7
- Continue UNIX Programming with ls and
touch
Collaborative Programming
- On Friday, many students participated in helping to write versions
of ls and touch.
- We will continue that today, by finishing touch, and extending the
simple ls that we wrote Friday.
- Please feel free to work and discuss today's labwork in teams (of
any number of students)
First, recall the utility touch(1)
- What is touch? What does it do?
- How does it work?
- We started to implement it on Friday. The (incomplete) code that we
wrote is here but I
recommend starting over, as the current direction is incorrect.
- Your task: implement a correct version of touch(1)
Extending ls
- Now that touch is complete, let's revisit ls and extend it
to work like ls -l works.
- The (working) version of ls that was developed in class is
here.
- The following slides will help walk you through the necessary steps.
- Your goal, at the end, is to have an equivalent to ls -l.
How do we add -l to our ls?
- Q: What does -l display?
- A: modtime, size, owner, group, links, type, permissions
- Q: Where can we learn about these?
- A: As always, search the manual
- Result: the stat() system call
How does stat() work?
- int stat(char *name, struct stat *buf)
- struct contains
- st_mode
- st_uid
- st_gid
- st_size
- st_nlink
- st_mtime
- So we can modify ls to provide -l features using stat() on the filename
How well should your ls -l work?
- Perfect: filenames, filesizes
- We can use ctime() to convert the mod time
- Owner and group info are stored as numbers, so we need to map them to names (in a few slides)
- What about type? permission?
Where are type and permissions stored?
- st_mode is a 16 bit value. Within it, we have
- four bits for the file type, followed by
- three bits for suid, sgid, and sticky bit, followed by
- nine bits for owner, group and world rwx permissions
- So we will use subfield coding.
- Much like telephone numbers, IP addresses, etc.
UIDs and GIDs
- UIDs and GIDs from stat() are just numbers
- How can we convert them to names?
- Then we can finish our ls -l
- First, search manual pages
Converting UIDs
- From man pages, we learn about /etc/passwd and ypcat passwd
- The getpwuid() function provides access to user information.
- A similar function is available to convert gid information.
- Your ls should now be able to generate correct permissions, types, owner, group, etc. just like ls -l