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.

The watch program you write must meet the following specifications:

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

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

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

The utmplib.c file is:

#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 */ }

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

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions