Users and Files: cp, ls, and tail
Announcements
- HW#4 is due Saturday.
- Project #6 is due Thursday.
- SQ6: Low 0, high 6, median 5, mean 4.5
- P5: Low 58, high 100, median 85, mean 82
- Today's schedule
- Short quiz #7
- Continue Understanding UNIX Programming
- Final version of who.c
- Discuss copy (cp) implementation
- implement cp
Let's now consider: cp (read and write)
% cp sourcefile copyfile
- What does cp do?
- How does it do so?
cp (read and write)
% cp sourcefile copyfile
- What does cp do?
- Answer: creates or truncates a file, then writes data into it
from source file
- How does it do so?
- Answer: let's find out by searching the manual pages
Creating/truncating a file
- Creating
- fd = creat(name, 0644)
- First creates or truncates a file
- Then opens the file for writing
- If create, set mode to 0644
- returns -1 on error, otherwise a file descriptor
- Writing
- n = write(fd, buffer, num)
- sends num bytes to the file
- returns actual number sent, or -1 on error
Writing copy.c
- Want (logically):
- open sourcefile
- creat copyfile
- read source to a buffer (until eof)
- write buffer to the copy, repeat
- close both files
- Let's do it... who is the first volunteer?