Question
Identity crisis: The manual page in UNIX for who lists who am i as an acceptable *usage of * the command. It also lists whoami.
Identity crisis: The manual page in UNIX 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. Write a C program that works like whoami.
The who2.c file is:
#include #include #include #include #include
/* #define SHOWHOST */
void showtime(long); void show_info(struct utmp *);
int main() { struct utmp utbuf; /* read info into here */ int utmpfd; /* read from this descriptor */
if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){ perror(UTMP_FILE); exit(1); }
while( read(utmpfd, &utbuf, sizeof(utbuf)) == sizeof(utbuf) ) show_info( &utbuf ); close(utmpfd); 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( long 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 *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 */ }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started