chapter 2

Post on 25-May-2015

292 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 2

Users, Files, and the Manual

Who?Is using the system?

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.

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

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

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"

Time format

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

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

Position 4

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.

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.

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.

top related