11159Sbill /* 235531Sbostic * Copyright (c) 1988 Regents of the University of California. 335531Sbostic * All rights reserved. 435531Sbostic * 535531Sbostic * Redistribution and use in source and binary forms are permitted 635531Sbostic * provided that the above copyright notice and this paragraph are 735531Sbostic * duplicated in all such forms and that any documentation, 835531Sbostic * advertising materials, and other materials related to such 935531Sbostic * distribution and use acknowledge that the software was developed 1035531Sbostic * by the University of California, Berkeley. The name of the 1135531Sbostic * University may not be used to endorse or promote products derived 1235531Sbostic * from this software without specific prior written permission. 1335531Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1435531Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1535531Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1619909Sdist */ 1719909Sdist 1819909Sdist #ifndef lint 1919909Sdist char copyright[] = 2035531Sbostic "@(#) Copyright (c) 1988 Regents of the University of California.\n\ 2119909Sdist All rights reserved.\n"; 2235531Sbostic #endif /* not lint */ 2319909Sdist 2419909Sdist #ifndef lint 25*35776Sbostic static char sccsid[] = "@(#)wall.c 5.6 (Berkeley) 10/10/88"; 2635531Sbostic #endif /* not lint */ 2719909Sdist 2819909Sdist /* 291159Sbill * This program is not related to David Wall, whose Stanford Ph.D. thesis 301159Sbill * is entitled "Mechanisms for Broadcast and Selective Broadcast". 311159Sbill */ 321159Sbill 3335531Sbostic #include <sys/param.h> 3435531Sbostic #include <sys/time.h> 3535531Sbostic #include <sys/signal.h> 3635531Sbostic #include <sys/stat.h> 3735531Sbostic #include <sys/dir.h> 3835531Sbostic #include <fcntl.h> 391159Sbill #include <utmp.h> 4035531Sbostic #include <pwd.h> 4116256Sralph #include <errno.h> 4235531Sbostic #include <stdio.h> 4316256Sralph 4435531Sbostic #define IGNOREUSER "sleeper" 4535531Sbostic #define UTMP "/etc/utmp" 461159Sbill 4735531Sbostic static int mbufsize; 4835531Sbostic static char *mbuf; 491159Sbill 5035531Sbostic /* ARGSUSED */ 511159Sbill main(argc, argv) 5235531Sbostic int argc; 5335531Sbostic char **argv; 541159Sbill { 5535531Sbostic struct utmp utmp; 5635531Sbostic FILE *fp; 571159Sbill 5835531Sbostic if (argc > 2) { 5935531Sbostic fprintf(stderr, "usage: wall [file]\n"); 601159Sbill exit(1); 611159Sbill } 6235531Sbostic makemsg(argv); 6335531Sbostic 6435531Sbostic if (!(fp = fopen(UTMP, "r"))) { 6535531Sbostic fprintf(stderr, "wall: cannot read /etc/utmp.\n"); 6635531Sbostic exit(1); 671159Sbill } 6835531Sbostic /* NOSTRICT */ 69*35776Sbostic while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) 70*35776Sbostic if (utmp.ut_name[0] && 71*35776Sbostic strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name))) 72*35776Sbostic sendmsg(utmp.ut_line); 731159Sbill exit(0); 741159Sbill } 751159Sbill 7635531Sbostic makemsg(argv) 7735531Sbostic char **argv; 781159Sbill { 7935531Sbostic register int ch, cnt; 8035531Sbostic struct tm *lt; 8135531Sbostic struct passwd *pw, *getpwuid(); 8235531Sbostic struct stat sbuf; 8335531Sbostic time_t now, time(); 8435531Sbostic FILE *fp; 8535531Sbostic int fd; 8635531Sbostic char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15]; 8735531Sbostic char *getlogin(), *malloc(), *strcpy(), *ttyname(); 881159Sbill 8935531Sbostic (void)strcpy(tmpname, "/tmp/wall.XXX"); 9035531Sbostic if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) { 9135531Sbostic fprintf(stderr, "wall: can't open temporary file.\n"); 9235531Sbostic exit(1); 9335531Sbostic } 9435531Sbostic (void)unlink(tmpname); 9516256Sralph 9635531Sbostic if (!(whom = getlogin())) 9735531Sbostic whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 9835531Sbostic (void)gethostname(hostname, sizeof(hostname)); 9935531Sbostic (void)time(&now); 10035531Sbostic lt = localtime(&now); 10135531Sbostic 10235531Sbostic /* 10335531Sbostic * all this stuff is to blank out a square for the message; we 10435531Sbostic * limit message lines to 75 characters, and blank out to 79. 10535531Sbostic * Not 80 'cause some terminals do weird stuff then. 10635531Sbostic */ 10735531Sbostic fprintf(fp, "\r%79s\r\n", " "); 10835531Sbostic (void)sprintf(lbuf, "Broadcast Message from %s@%s", whom, hostname); 10935531Sbostic fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 11035531Sbostic (void)sprintf(lbuf, " (%s) at %d:%02d ...", ttyname(2), 11135531Sbostic lt->tm_hour, lt->tm_min); 11235531Sbostic fprintf(fp, "%-79.79s\r\n", lbuf); 11335531Sbostic fprintf(fp, "%79s\r\n", " "); 11435531Sbostic 11535531Sbostic if (*++argv && !(freopen(*argv, "r", stdin))) { 11635531Sbostic fprintf(stderr, "wall: can't read %s.\n", *argv); 11735531Sbostic exit(1); 11816256Sralph } 11935531Sbostic while (fgets(lbuf, sizeof(lbuf), stdin)) 12035531Sbostic for (cnt = 0, p = lbuf; ch = *p; ++p, ++cnt) 12135531Sbostic if (cnt == 75 || ch == '\n') { 12235531Sbostic for (; cnt < 79; ++cnt) 12335531Sbostic putc(' ', fp); 12435531Sbostic putc('\r', fp); 12535531Sbostic putc('\n', fp); 12635531Sbostic cnt = 1; 12735531Sbostic } else 12835531Sbostic putc(ch, fp); 12935531Sbostic fprintf(fp, "%79s\r\n", " "); 13035531Sbostic rewind(fp); 13135531Sbostic 13235531Sbostic if (fstat(fd, &sbuf)) { 13335531Sbostic fprintf(stderr, "wall: can't stat temporary file.\n"); 13435531Sbostic exit(1); 13516256Sralph } 13635531Sbostic mbufsize = sbuf.st_size; 13735531Sbostic if (!(mbuf = malloc((u_int)mbufsize))) { 13835531Sbostic fprintf(stderr, "wall: out of memory.\n"); 13935531Sbostic exit(1); 14016256Sralph } 141*35776Sbostic if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) { 142*35776Sbostic fprintf(stderr, "wall: can't read temporary file.\n"); 143*35776Sbostic exit(1); 144*35776Sbostic } 14535531Sbostic (void)close(fd); 14635531Sbostic } 14735531Sbostic 14835531Sbostic sendmsg(line) 14935531Sbostic char *line; 15035531Sbostic { 15135531Sbostic extern int errno; 15235531Sbostic static char device[MAXNAMLEN] = "/dev/"; 153*35776Sbostic register int fd, flags, left, wret; 15435531Sbostic char *lp, *strcpy(); 15535531Sbostic 15635531Sbostic (void)strcpy(device + 5, line); 15735531Sbostic if ((fd = open(device, O_WRONLY, 0)) < 0) { 15835531Sbostic fprintf(stderr, "wall: %s: ", device); 15935531Sbostic perror((char *)NULL); 16016256Sralph } 16135531Sbostic flags = fcntl(fd, F_GETFL, 0); 16235531Sbostic if (!(flags & FNDELAY)) { 16335531Sbostic /* NDELAY bit not set; if can't set, fork instead */ 16435531Sbostic if (fcntl(fd, F_SETFL, flags|FNDELAY) == -1) { 16535531Sbostic flags = 0; 16635531Sbostic goto forkit; 1673176Swnj } 1681159Sbill } 16935531Sbostic else 17035531Sbostic flags = 0; 17135531Sbostic lp = mbuf; 172*35776Sbostic left = mbufsize; 173*35776Sbostic while ((wret = write(fd, lp, left)) != left) { 174*35776Sbostic if (wret >= 0) { 175*35776Sbostic lp += wret; 176*35776Sbostic left -= wret; 17735531Sbostic } else if (errno == EWOULDBLOCK) { 17835531Sbostic /* child resets FNDELAY if necessary; parent leaves */ 17935531Sbostic forkit: if (fork()) { 18035531Sbostic (void)close(fd); 18135531Sbostic return; 18235531Sbostic } 18335531Sbostic if (flags) 18435531Sbostic (void)fcntl(fd, F_SETFL, flags); 18535531Sbostic /* wait 5 minutes and then quit */ 18635531Sbostic (void)alarm((u_int)(60 * 5)); 18735531Sbostic (void)write(fd, mbuf, mbufsize); 18835531Sbostic exit(0); 18935531Sbostic } else { 19035531Sbostic fprintf(stderr, "wall: %s: ", device); 19135531Sbostic perror((char *)NULL); 19235531Sbostic break; 19335531Sbostic } 19435531Sbostic } 19535531Sbostic /* write was successful, or error != EWOULDBLOCK; cleanup */ 19635531Sbostic if (flags) 19735531Sbostic (void)fcntl(fd, F_SETFL, flags); 19835531Sbostic (void)close(fd); 1991159Sbill } 200