Announcements
- Scripting homework was due yesterday
- Reminder of scripting project due next Thursday
- CSE study break today in PL416
- Today's schedule
- Short quiz #7
- Return HW#3 and Project #4
- Finish who.c
- Discuss copy (cp) implementation
Our who is close, but still needs work
- How do we get who to look good?
- Suppress old entries (done)
- Format time (and combine to single line of output)
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...