chapter 2

31
Chapter 2 Users, Files, and the Manual

Upload: lopjuan

Post on 25-May-2015

291 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 2

Chapter 2

Users, Files, and the Manual

Page 2: Chapter 2

Who?Is using the system?

Page 3: Chapter 2
Page 4: Chapter 2
Page 5: Chapter 2
Page 6: Chapter 2
Page 7: Chapter 2
Page 8: Chapter 2
Page 9: Chapter 2
Page 10: Chapter 2
Page 11: Chapter 2
Page 12: Chapter 2

Low Level Read

ssize_t read(int fd, void *buf, size_t count);• read() attempts to read up to count bytes from file descriptor fd

into the buffer starting at buf.• On success, the number of bytes read is returned (zero indicates

end of file), and the file position is advanced by this number. • It is not an error if this number is smaller than the number of

bytes requested; – 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.

• On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.

Page 13: Chapter 2

Low Level Open

• open and possibly create a file or device

#include <fcntl.h>

int open(const char *pathname, mode_t mode);

• 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.).

Page 14: Chapter 2
Page 15: Chapter 2
Page 16: Chapter 2

Suppressing Blank Records

• Remove unused terminal lines blank user name

• Locate the file /usr/include/bits/utmp.h– Only print out our LOGIN process – USER_PROCESS 7

Page 17: Chapter 2

Time format

• Unix stores times as the number of seconds since midnight 1/1/1970, G.M.T.

• time_t data type is an integer.• ctime

– #include <time.h>– char *ctime(const time_t *timep);– The call ctime(t) is equivalent to asctime(localtime(t)).

It converts the calendar time t into a null-terminated string of the form

"Wed Jun 30 21:49:08 1993\n"

Page 18: Chapter 2

Time format

• “Wed Jun 30 21:49:08 1993\n”

• Printf(“12.12s”, ctime(&t) + 4)

Position 4

Page 19: Chapter 2

perror

• perror - print a system error message• #include <stdio.h>• Void perror(const char *s);• DESCRIPTION

– 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.

– 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.

– Then the message and a new-line.

Page 20: Chapter 2
Page 21: Chapter 2
Page 22: Chapter 2
Page 23: Chapter 2
Page 24: Chapter 2
Page 25: Chapter 2
Page 26: Chapter 2
Page 27: Chapter 2
Page 28: Chapter 2
Page 29: Chapter 2
Page 30: Chapter 2

Leek()

Base: – SEEK_SET start of file– SEEK_CUR current position– SEEK_END end of file

Example:Lseek(fd, 0 SEEK_END);Write(fd, “hello”, strlen(“hello”);

Sets the current position to the end of file and writes a string there.

Page 31: Chapter 2

Leek()

Example:Lseek(fd, -(sizeof(struct utmp)), SEEK_CUR);

Moves to the current position a distance of sizeof(struct utmp) bytes before the current position.