Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

1. Let a, b R, a Answered: 1 week ago

Answered: 1 week ago