Question: Write a new version of write.c * that accepts a username as argument and prints on the screen of that user you request to *

Write a new version of write.c * that accepts a username as argument and prints on the screen of that user you request to * communicate. This should be a modification of the following code:

#include #include #include

/* * write1.c * * purpose: send messages to another terminal * method: open the other terminal for output then * copy from stdin to that terminal * usage: write1 username */

main( int ac, char *av[] ) { int fd; char buf[BUFSIZ]; char *get_tty(), *tty_for_user;

if ( ac != 2 ){ /* check args */ fprintf(stderr,"usage: write0 logname "); exit(1); }

tty_for_user = get_tty( av[1] ); /* find user */ if ( tty_for_user == NULL ) return 1;

sprintf(buf, "/dev/%s", tty_for_user); /* open device */ fd = open( buf, O_WRONLY ); if ( fd == -1 ){ perror(buf); exit(1); }

while( fgets(buf, BUFSIZ, stdin) != NULL ) /* write to user */ if ( write(fd, buf, strlen(buf)) == -1 ) break; close( fd ); }

char * get_tty( char *logname ) /* * purpose: find the tty at which 'logname' is logged in * returns: a string or NULL if not logged in * errors: does not handle multiple logins */ { static struct utmp utrec; int utfd; int namelen = sizeof( utrec.ut_name ); char *retval = NULL ;

if ( (utfd = open( UTMP_FILE, O_RDONLY )) == -1 ) /* open utmp */ return NULL;

/* look for a line where the user is logged in */ while( read( utfd, &utrec, sizeof(utrec)) == sizeof(utrec) ) if ( strncmp(logname, utrec.ut_name, namelen ) == 0 ) { retval = utrec.ut_line ; break; }

close(utfd); /* close & go */ return retval; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!