Menu

(Solved) : Write Program Called Watch Takes Command Line Arguments List Users Want Program Watch Eve Q44147324 . . .

You will write a program called watch thattakes as command line arguments a list of users you want theprogram to watch for. Every five minutes the program wakes up andchecks the utmp file. It compares the list of active users it findsthere to the list of active users it found last time. If it finds auser is not logged on but had been logged on before, it should tellyou. If it finds a user is logged on now and had not been logged onbefore, it should tell you. The interval of checking defaults tofive minutes. If you prefer a different interval, you should beable to specify the interval on the command line. Under Unix, auser may login to several terminals at the same time, so theprogram should only report when a user goes from no logins to oneor more logins or when a user goes from one or more logins to nologins.

The watch program you write must meet the followingspecifications:

[a] It takes one or more lognames as command line arguments. Itwatches for the comings and goings of the lognames specified on thecommand line. It does not report on users you do not specify.

[b] When the program starts, it prints the lognames of users onthe given list who are currently logged in.

[c] It checks the utmp file every 300 seconds to see if anyoneon the list of lognames has logged in or logged out. If the firstcommand line argument is an integer, it uses that number as thenumber of seconds to sleep between checking the utmp file.

[d] The program reports when a user on the list logs in. A useris considered to have logged in if, when the utmp file was lastchecked, that user was not logged in anywhere, but now the user islogged in at one or more terminals.

[e] The program reports when a user on the list logs out. A useris considered to have logged out if, when the utmp file was lastchecked, that user was logged in, but now the user is not logged inat any terminals.

[f] The program buffers disk access to the utmp file by usingthe functions in the file utmplib.c.

[g] The program exits when the person who launched the programis no longer logged in.

The utmplib.c file is:

#include <stdio.h>#include <fcntl.h>#include <sys/types.h>#include <utmp.h>/* * 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 */}

Expert Answer


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

OR