Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will write a program called watch that takes as command line arguments a list of users you want the program to watch for. Every

You will write a program called watch that takes as command line arguments a list of users you want the program to watch for. Every five minutes the program wakes up and checks the utmp file. It compares the list of active users it finds there to the list of active users it found last time. If it finds a user is not logged on but had been logged on before, it should tell you. If it finds a user is logged on now and had not been logged on before, it should tell you. The interval of checking defaults to five minutes. If you prefer a different interval, you should be able to specify the interval on the command line. Under Unix, a user may login to several terminals at the same time, so the program should only report when a user goes from no logins to one or more logins or when a user goes from one or more logins to no logins. If you do not make this restriction, each time a user opens a new terminal window on a graphics workstation, you will be notified. Specifications The watch program you write must meet the following specifications:

in c++ [a] It takes one or more lognames as command line arguments. It watches for the comings and goings of the lognames specified on the command line. It does not report on users you do not specify. [b] When the program starts, it prints the lognames of users on the given list who are currently logged in. [c] It checks the utmp file every 300 seconds to see if anyone on the list of lognames has logged in or logged out. If the first command line argument is an integer, it uses that number as the number of seconds to sleep between checking the utmp file.

2 [d] The program reports when a user on the list logs in. A user is considered to have logged in if, when the utmp file was last checked, that user was not logged in anywhere, but now the user is logged in at one or more terminals. [e] The program reports when a user on the list logs out. A user is considered to have logged out if, when the utmp file was last checked, that user was logged in, but now the user is not logged in at any terminals. The program should produce output of the form (the ... indicates five-minute pauses): % watch Alice happy Bob fido king susie Alice happy are currently logged in ... happy logged out susie logged in ... happy king logged in ... susie logged out fido Alice logged in ... If you invoke the program with % watch 120 Alice happy Bob fido king Susie then it checks for changes every 120 seconds. In practice, one would run the program in the background by typing a command with a trailing ampersand: % watch 120 betsy happy maya fido king susie & [f] The program buffers disk access to the utmp file by using the functions in the file utmplib.c. [g] The program exits when the person who launched the program is no longer logged in. (There are several ways of doing this part.)

the utmplib.c is as follows

#include

#include

#include

#include

/*

* library of functions to do buffered reads from utmp file

*

* reads NRECS per read and then doles them out from the

* buffer

*/

#define NRECS 16

#define NULLUT ((struct utmp *)NULL)

#define UTSIZE (sizeof(struct utmp))

static char utmpbuf[NRECS * UTSIZE]; /* storage */

static int num_recs; /* num stored */

static int cur_rec; /* next to go */

static int fd_utmp = -1; /* read from */

/*

* buffered reads from /etc/utmp

* functions are

* utmp_open( filename ) - open file

* returns -1 on error

* utmp_next( ) - return pointer to next struct

* returns NULL on eof

* utmp_close() - close file

*/

utmp_open( filename )

char *filename;

{

if ( filename == NULL )

filename = UTMP_FILE;

fd_utmp = open( filename, O_RDONLY ); /* open it */

cur_rec = num_recs = 0; /* no recs yet */

return fd_utmp; /* report */

}

struct utmp *

utmp_next()

{

struct utmp *recp;

if ( fd_utmp == -1 ) /* error ? */

return NULLUT;

if ( cur_rec==num_recs && utmp_reload()==0 ) /* any more ? */

return NULLUT;

/* get address of next record */

recp = ( struct utmp *) &utmpbuf[cur_rec * UTSIZE];

cur_rec++;

return recp;

}

utmp_reload()

/*

* read next bunch of records into buffer

*/

{

int amt_read;

/* read them in */

amt_read = read( fd_utmp , utmpbuf, NRECS * UTSIZE );

/* how many did we get? */

num_recs = amt_read/UTSIZE;

/* reset pointer */

cur_rec = 0;

return num_recs;

}

utmp_close()

{

if ( fd_utmp != -1 ) /* don't close if not */

close( fd_utmp ); /* open */

}

while the dumputmp.c is

#include

#include

#include

main(ac,av)

char **av;

{

if ( ac == 1 )

dumpfile( UTMP_FILE );

else

dumpfile( av[1] );

}

dumpfile( fn )

char *fn;

/*

* open file and dump records

*/

{

struct utmp utrec;

int fd;

fd = open( fn, 0 );

if ( fd == -1 )

{

perror( fn );

return ;

}

while( read( fd, &utrec, sizeof(utrec) ) == sizeof(utrec) )

show_utrec(&utrec);

close( fd );

}

show_utrec( rp )

struct utmp *rp;

{

char *typename();

printf("%-8.8s ", rp->ut_user );

/* printf("%-14.14s ", rp->ut_id ); /* used it for hp-ux */

printf("%-12.12s ", rp->ut_line );

printf("%6d ", rp->ut_pid );

printf("%4d %-12.12s ", rp->ut_type , typename(rp->ut_type) );

printf("%12d ", rp->ut_time );

printf("%s ", rp->ut_host );

putchar(' ');

}

char *uttypes[] = { "EMPTY", "RUN_LVL", "BOOT_TIME", "OLD_TIME",

"NEW_TIME", "INIT_PROCESS", "LOGIN_PROCESS",

"USER_PROCESS", "DEAD_PROCESS", "ACCOUNTING"

};

char *

typename( typenum )

{

return uttypes[typenum];

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

c. What were you expected to do when you grew up?

Answered: 1 week ago

Question

4. Describe how cultural values influence communication.

Answered: 1 week ago

Question

3. Identify and describe nine cultural value orientations.

Answered: 1 week ago