Users and Files: who and cp
Announcements
- Project #6 is due next Monday.
- P4 returned on Wednesday: Low 30, high 100, mean 84
- Today's schedule
- Short quiz #6
- 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?