Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Need to write a function that works better than whoami in Unix. We were given a set of .txt files that we renamed to be

Need to write a function that works better than whoami in Unix. We were given a set of .txt files that we renamed to be .c files. The new function needs to written in who4.c file and it must work like whoami. Here are it's contents: /* who4.c - who with buffered reads * Identity crisis: The manual page for who lists who am i as an acceptable *usage of * the command. It also lists whoami. Modify who2.c so it supports the who am i *usage. * Experiment with the command whom and read the manage. How does it differ from who * am i ? write a program that works like whoami. */

/*Code from who2.c*/

/* who2.c - read /etc/utmp and list info therein * - suppresses empty records * - formats time nicely */ #include #include #include #include #include #include

#define SHOWHOST void showtime(time_t); void show_info(struct utmp *);

int main() { struct utmp *utbuf; /* read info into here */ *utmp_next(); /* read from this descriptor */

if (utmp_open(UTMP_FILE) == -1 ){ perror(UTMP_FILE); exit(1); } while((utbuf =utmp_next())) != ((struct utmp *) NULL)) show_info(utbufp)); utmp_close(); return 0; } /* * show info() * displays the contents of the utmp struct * in human readable form * * displays nothing if record has no user name */ void show_info( struct utmp *utbufp ) { if ( utbufp->ut_type != USER_PROCESS ) return;

printf("%-8.8s", utbufp->ut_name); /* the logname */ printf(" "); /* a space */ printf("%-8.8s", utbufp->ut_line); /* the tty */ printf(" "); /* a space */ showtime( utbufp->ut_time ); /* display time */ #ifdef SHOWHOST if ( utbufp->ut_host[0] != '\0' ) printf(" (%s)", utbufp->ut_host);/* the host */ #endif printf(" "); /* newline */ }

void showtime( time_t timeval ) /* * displays time in a format fit for human consumption * uses ctime to build a string then picks parts out of it * Note: %12.12s prints a string 12 chars wide and LIMITS * it to 12chars. */ { char *ctime(); char *cp; /* to hold address of time */

cp = ctime(&timeval); /* convert time to string */ /* string looks like */ /* Mon Feb 4 00:46:40 EST 1991 */ /* 0123456789012345. */ printf("%12.12s", cp+4 ); /* pick 12 chars from pos 4 */ }

/*Code from who3.c as provided*/

/* who3.c - who with buffered reads * - surpresses empty records * - formats time nicely * - buffers input (using utmplib) */ #include #include #include #include #include #include

#define SHOWHOST

void show_info(struct utmp *); void showtime(time_t);

int main() { struct utmp *utbufp, /* holds pointer to next rec */ *utmp_next(); /* returns pointer to 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; } /* * show info() * displays the contents of the utmp struct * in human readable form * * displays nothing if record has no user name */ void show_info( struct utmp *utbufp ) { if ( utbufp->ut_type != USER_PROCESS ) return;

printf("%-8.8s", utbufp->ut_name); /* the logname */ printf(" "); /* a space */ printf("%-8.8s", utbufp->ut_line); /* the tty */ printf(" "); /* a space */ showtime( utbufp->ut_time ); /* display time */ #ifdef SHOWHOST if ( utbufp->ut_host[0] != '\0' ) printf(" (%s)", utbufp->ut_host); /* the host */ #endif printf(" "); /* newline */ }

void showtime( time_t timeval ) /* * displays time in a format fit for human consumption * uses ctime to build a string then picks parts out of it * Note: %12.12s prints a string 12 chars wide and LIMITS * it to 12chars. */ { char *ctime(); /* convert long to ascii */ char *cp; /* to hold address of time */

cp = ctime( &timeval ); /* convert time to string */ /* string looks like */ /* Mon Feb 4 00:46:40 EST 1991 */ /* 0123456789012345. */ printf("%12.12s", cp+4 ); /* pick 12 chars from pos 4 */ }

/*Code from who0.c as provided*/

#include #include #include

/* * who version 0 * main outline but no substance */

int main() { int fd; /* for file des of utmp */ struct utmp current_record; /* hold info from file */ int reclen = sizeof(struct utmp);

fd = open(UTMP_FILE, O_RDONLY); if ( fd == -1 ){ perror( "who0" ); /* report a system error */ exit(1); /* and get out now */ }

while ( read(fd, ¤t_record, reclen) == reclen ) show_info( ¤t_record );

close (fd); return 0; }

/* Code for who1.c provided by instruct*/

/* who1.c - a first version of the who program * open, read UTMP file, and show results */ #include #include #include #include

#define SHOWHOST /* include remote machine on output */

int main() { struct utmp current_record; /* read info into here */ int utmpfd; /* read from this descriptor */ int reclen = sizeof(current_record);

if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){ perror( UTMP_FILE ); /* UTMP_FILE is in utmp.h */ exit(1); }

while ( read(utmpfd, ¤t_record, reclen) == reclen ) show_info(¤t_record); close(utmpfd); return 0; /* went ok */ } /* * show info() * displays contents of the utmp struct in human readable form * *note* these sizes should not be hardwired */ show_info( struct utmp *utbufp ) { printf("%-8.8s", utbufp->ut_name); /* the logname */ printf(" "); /* a space */ printf("%-8.8s", utbufp->ut_line); /* the tty */ printf(" "); /* a space */ printf("%10ld", utbufp->ut_time); /* login time */ printf(" "); /* a space */ #ifdef SHOWHOST printf("(%s)", utbufp->ut_host); /* the host */ #endif printf(" "); /* newline */ }

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_2

Step: 3

blur-text-image_3

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

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students explore these related Databases questions

Question

=+free to pirate employees from competitors?

Answered: 3 weeks ago