Chapter 2<br />Users, Files, and the Manual <br />
Who?Is using the system?<br />
Low Level Read<br />ssize_t read(intfd, void *buf, size_t count);<br /> read()  attempts to read up to count bytes from fi...
Low Level Open<br />open and possibly create a file or device<br /> #include <fcntl.h><br />int open(const char *pathname,...
Suppressing Blank Records<br />Remove unused terminal lines  blank user name <br />Locate the file /usr/include/bits/utmp...
Time format<br />Unix stores times as the number of seconds since midnight 1/1/1970, G.M.T.<br />time_t data type is an in...
Time format<br />“Wed Jun 30 21:49:08 1993n”<br />Printf(“12.12s”, ctime(&t) + 4)<br />Position 4<br />
perror<br />perror - print a system error message<br />#include <stdio.h><br />Void perror(const char *s);<br />DESCRIPTIO...
Leek()<br />Base: <br />SEEK_SET start of file<br />SEEK_CUR current position<br />SEEK_END end of file<br />Example:<b...
Leek()<br />Example:<br />Lseek(fd, -(sizeof(structutmp)), SEEK_CUR);<br />Moves to the current position a distance of siz...
Upcoming SlideShare
Loading in …5
×

Chapter 2

432 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
432
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 2

  1. 1. Chapter 2<br />Users, Files, and the Manual <br />
  2. 2. Who?Is using the system?<br />
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12. Low Level Read<br />ssize_t read(intfd, void *buf, size_t count);<br /> read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.<br />On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. <br />It is not an error if this number is smaller than the number of bytes requested; <br />this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. <br />On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.<br />
  13. 13. Low Level Open<br />open and possibly create a file or device<br /> #include <fcntl.h><br />int open(const char *pathname, mode_t mode);<br /> Given a pathname for a file, open() returns a file descriptor, a small, non-negative integer for use in subsequent system calls (read(2), write(2), lseek(2), fcntl(2), etc.). <br />
  14. 14.
  15. 15.
  16. 16. Suppressing Blank Records<br />Remove unused terminal lines  blank user name <br />Locate the file /usr/include/bits/utmp.h<br />Only print out our LOGIN process <br />USER_PROCESS 7<br />
  17. 17. Time format<br />Unix stores times as the number of seconds since midnight 1/1/1970, G.M.T.<br />time_t data type is an integer.<br />ctime<br /> #include <time.h><br /> char *ctime(const time_t*timep);<br /> The call ctime(t) is equivalent to asctime(localtime(t)). It converts the calendar time t into a null-terminated string of the form<br /> "Wed Jun 30 21:49:08 1993n"<br />
  18. 18. Time format<br />“Wed Jun 30 21:49:08 1993n”<br />Printf(“12.12s”, ctime(&t) + 4)<br />Position 4<br />
  19. 19. perror<br />perror - print a system error message<br />#include <stdio.h><br />Void perror(const char *s);<br />DESCRIPTION<br />The routine perror() produces a message on the standard error output, describing the last error encountered during a call to a system or library function. <br />First (if s is not NULL and *s is not a null byte ('0')) the argument string s is printed, followed by a colon and a blank. <br />Then the message and a new-line.<br />
  20. 20.
  21. 21.
  22. 22.
  23. 23.
  24. 24.
  25. 25.
  26. 26.
  27. 27.
  28. 28.
  29. 29.
  30. 30. Leek()<br />Base: <br />SEEK_SET start of file<br />SEEK_CUR current position<br />SEEK_END end of file<br />Example:<br />Lseek(fd, 0 SEEK_END);<br />Write(fd, “hello”, strlen(“hello”);<br />Sets the current position to the end of file and writes a string there.<br />
  31. 31. Leek()<br />Example:<br />Lseek(fd, -(sizeof(structutmp)), SEEK_CUR);<br />Moves to the current position a distance of sizeof(structutmp)bytes before the current position.<br />

×