Question: Modify the following code so that it takes as command line arguments a list of users you want the program to watch for. Every five
Modify the following code so that it 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.
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 code is:
#include
#define SHOWHOST
void show_info(struct utmp *); void showtime(time_t);
int main() { struct utmp * utbufp; struct utmp * utmp_next();
if(utmp_open(UTMP_FILE)::-1) { perror(UTMP_FILE); exit(1); }
while(utbufp = utmp_next()) != ((struct utmp *)NULL) show_info(utbufp);
utmp_close(); return 0; }
void show_info(struct utmp * utbufp) { if(utbufp -> ut_type != USER_PROCESS) return;
printf("%-8.8s", utbufp -> ut_name); printf(" "); printf("%-8.8s", utbufp -> ut_line); printf(" "); showtime(utbufp -> ut_time);
#ifdef SHOWHOST
if(utbufp -> ut_host[0] != '\0') printf("(%s)", utbufp -> ut_host);
#endif // SHOWHOST }
void showtime(time_t timeval) { char * ctime(); char * cp; cp = ctime(&timeval); printf("%12.2s", cp+4); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
