From: utzoo!decvax!pur-ee!ks Newsgroups: net.sources Title: talk.c - unbuffered write.c - (nf) Article-I.D.: pur-ee.465 Posted: Wed Jul 28 01:26:25 1982 Received: Mon Aug 2 04:36:41 1982 #N:pur-ee:9200001:000:6017 pur-ee!ks Jul 27 17:19:00 1982 static char *sccsid = "@(#)talk.c 4.2 (aef & Berkeley) 10/1/80"; /* * TALK * write to another user * (from write.c) * Modified A E Feather for unbuffered writes * 29-Oct-80 * Modified to handle "newtty" driver ctlecho mode * aef 1-17-81 */ #include#include #include #include #include #include #include struct sgttyb stbuf; short cptr; #define NMAX sizeof(ubuf.ut_name) #define LMAX sizeof(ubuf.ut_line) char *strcat(); char *strcpy(); struct utmp ubuf; int signum[] = {SIGHUP, SIGINT, SIGQUIT, 0}; char me[10] = "???"; char *him; char *mytty; char histty[32]; char *histtya; char *ttyname(); char *rindex(); int logcnt; int eof(); int timout(); FILE *tf; char *getenv(); int ildisc; /* original line discipline */ int locmod; /* newtty local mode bits */ short lctlech = LCTLECH; main(argc, argv) char *argv[]; { struct stat stbuf; register ii, i; register FILE *uf; int c1, c2; long clock = time( 0 ); struct tm *localtime(); struct tm *localclock = localtime( &clock ); if(argc < 2) { printf("usage: talk user [ttyname]\n"); exit(1); } him = argv[1]; if(argc > 2) histtya = argv[2]; if ((uf = fopen("/etc/utmp", "r")) == NULL) { printf("cannot open /etc/utmp\n"); goto cont; } mytty = ttyname(2); if (mytty == NULL) { printf("Can't find your tty\n"); exit(1); } /* check if message permission on mytty is on */ if(stat(mytty, &stbuf) < 0){ printf("Can't stat your tty!\n"); exit(1); } if((stbuf.st_mode&02) == 0){ printf("Your message permission is OFF!\n"); exit(1); } mytty = rindex(mytty, '/') + 1; if (histtya) { strcpy(histty, "/dev/"); strcat(histty, histtya); } while (fread((char *)&ubuf, sizeof(ubuf), 1, uf) == 1) { if (strcmp(ubuf.ut_line, mytty)==0) { for(i=0; i 1){ printf(", %s", ubuf.ut_line); } if (histtya==0) { strcpy(histty, "/dev/"); strcat(histty, ubuf.ut_line); } nomat: ; } cont: if (logcnt==0 && histty[0]=='\0') { printf("%s not logged in.\n", him); exit(1); } if(histtya == 0 && logcnt > 1){ printf("\nTalk to which one: "); fflush(stdout); strcpy(histty, "/dev/"); i = read(0, histty+5, 8); if(i>8)i=8; histty[i+4] = '\0'; lseek(fileno(uf), 0L, 0); while (fread((char *)&ubuf, sizeof(ubuf), 1, uf) == 1) { for(i=0; i tm_hour , localclock -> tm_min ); fflush(tf); gtty(0,&stbuf); ioctl(0, TIOCGETD, &ildisc); /* check if new line discipline */ if(ildisc == NTTYDISC){ ioctl(0, TIOCLGET, &locmod); /* get local bits */ if(locmod & LCTLECH) ioctl(0, TIOCLBIC, &lctlech); /* clear ctlecho */ } stbuf.sg_flags |= CBREAK; stty(0,&stbuf); printf("\07"); fflush(stdout); cptr = 0; for(;;) { char buf; i = read(0, &buf, 1); if(i <= 0) eof(); if(buf == '\04') eof(); if(buf == stbuf.sg_erase){ write( fileno( tf ), "\b \b", 3); continue; } cptr++; if(buf == '!' && cptr == 1) { char buf1[128]; while( buf != '\n'){ read(0, &buf, 1); buf1[cptr++] = buf; if(buf == stbuf.sg_erase){ if(cptr)cptr -= 2; } } buf1[--cptr] = 0; ex(buf1); cptr = 0; continue; } write(fileno(tf), &buf, 1); if ( buf == '\n' ){ write( fileno( tf ) , "\r" , 1 ); cptr = 0; } } perm: printf("Permission denied\n"); exit(1); } timout() { printf("Timeout opening their tty\n"); exit(1); } eof() { fprintf(tf, "EOF\r\n"); gtty(0,&stbuf); stbuf.sg_flags &= ~ CBREAK; stty(0,&stbuf); if(ildisc == NTTYDISC){ if(locmod & LCTLECH) ioctl(0, TIOCLBIS, &lctlech); /* reset ctlecho */ } exit(0); } ex(bp) char *bp; { register i; sigs(SIG_IGN); i = fork(); if(i < 0) { printf("Try again\n"); goto out; } if(i == 0) { gtty(0,&stbuf); stbuf.sg_flags &= ~ CBREAK; stty(0,&stbuf); sigs((int (*)())0); execl(getenv("SHELL") ? getenv("SHELL") : "/bin/sh", "sh", "-c", bp+1, 0); exit(0); } while(wait((int *)NULL) != i) ; printf("!\n"); out: gtty(0,&stbuf); stbuf.sg_flags |= CBREAK; stty(0,&stbuf); sigs(eof); } sigs(sig) int (*sig)(); { register i; for(i=0;signum[i];i++) signal(signum[i],sig); }