11159Sbill /* 240223Skarels * Copyright (c) 1988, 1990 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*44590Smarc static char sccsid[] = "@(#)wall.c 5.12 (Berkeley) 06/29/90"; 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> 3439423Sbostic #include <sys/stat.h> 3535531Sbostic #include <sys/time.h> 3639423Sbostic #include <sys/uio.h> 371159Sbill #include <utmp.h> 3835531Sbostic #include <pwd.h> 3935531Sbostic #include <stdio.h> 4039124Sbostic #include <paths.h> 4116256Sralph 4235531Sbostic #define IGNOREUSER "sleeper" 431159Sbill 4440223Skarels int nobanner; 4539089Smarc int mbufsize; 4639089Smarc char *mbuf; 471159Sbill 4835531Sbostic /* ARGSUSED */ 491159Sbill main(argc, argv) 5035531Sbostic int argc; 5135531Sbostic char **argv; 521159Sbill { 5340223Skarels extern int optind; 5440223Skarels int ch; 5539423Sbostic struct iovec iov; 5635531Sbostic struct utmp utmp; 5735531Sbostic FILE *fp; 5839423Sbostic char *p, *ttymsg(); 591159Sbill 6040223Skarels while ((ch = getopt(argc, argv, "n")) != EOF) 6140223Skarels switch (ch) { 6240223Skarels case 'n': 6340223Skarels /* undoc option for shutdown: suppress banner */ 6440223Skarels if (geteuid() == 0) 6540223Skarels nobanner = 1; 6640223Skarels break; 6740223Skarels case '?': 6840223Skarels default: 6940223Skarels usage: 7040223Skarels (void)fprintf(stderr, "usage: wall [file]\n"); 7140223Skarels exit(1); 7240223Skarels } 7340223Skarels argc -= optind; 7440223Skarels argv += optind; 7540223Skarels if (argc > 1) 7640223Skarels goto usage; 7735531Sbostic 7840223Skarels makemsg(*argv); 7939423Sbostic 8039089Smarc if (!(fp = fopen(_PATH_UTMP, "r"))) { 8139423Sbostic (void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP); 8235531Sbostic exit(1); 831159Sbill } 8439423Sbostic iov.iov_base = mbuf; 8539423Sbostic iov.iov_len = mbufsize; 8635531Sbostic /* NOSTRICT */ 8739423Sbostic while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) { 8839423Sbostic if (!utmp.ut_name[0] || 8939423Sbostic !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name))) 9039423Sbostic continue; 91*44590Smarc if (p = ttymsg(&iov, 1, utmp.ut_line)) 9239423Sbostic (void)fprintf(stderr, "wall: %s\n", p); 9339423Sbostic } 941159Sbill exit(0); 951159Sbill } 961159Sbill 9739423Sbostic makemsg(fname) 9839423Sbostic char *fname; 991159Sbill { 10035531Sbostic register int ch, cnt; 10135531Sbostic struct tm *lt; 10235531Sbostic struct passwd *pw, *getpwuid(); 10335531Sbostic struct stat sbuf; 10435531Sbostic time_t now, time(); 10535531Sbostic FILE *fp; 10635531Sbostic int fd; 10735531Sbostic char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15]; 10835531Sbostic char *getlogin(), *malloc(), *strcpy(), *ttyname(); 1091159Sbill 11039089Smarc (void)strcpy(tmpname, _PATH_TMP); 11139124Sbostic (void)strcat(tmpname, "/wall.XXXXXX"); 11235531Sbostic if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) { 11339423Sbostic (void)fprintf(stderr, "wall: can't open temporary file.\n"); 11435531Sbostic exit(1); 11535531Sbostic } 11635531Sbostic (void)unlink(tmpname); 11716256Sralph 11840223Skarels if (!nobanner) { 11940223Skarels if (!(whom = getlogin())) 12040223Skarels whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 12140223Skarels (void)gethostname(hostname, sizeof(hostname)); 12240223Skarels (void)time(&now); 12340223Skarels lt = localtime(&now); 12435531Sbostic 12540223Skarels /* 12640223Skarels * all this stuff is to blank out a square for the message; 12740223Skarels * we wrap message lines at column 79, not 80, because some 12840223Skarels * terminals wrap after 79, some do not, and we can't tell. 12940223Skarels * Which means that we may leave a non-blank character 13040223Skarels * in column 80, but that can't be helped. 13140223Skarels */ 13240223Skarels (void)fprintf(fp, "\r%79s\r\n", " "); 13340223Skarels (void)sprintf(lbuf, "Broadcast Message from %s@%s", 13440223Skarels whom, hostname); 13540223Skarels (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 13640223Skarels (void)sprintf(lbuf, " (%s) at %d:%02d ...", ttyname(2), 13740223Skarels lt->tm_hour, lt->tm_min); 13840223Skarels (void)fprintf(fp, "%-79.79s\r\n", lbuf); 13940223Skarels } 14039423Sbostic (void)fprintf(fp, "%79s\r\n", " "); 14135531Sbostic 14239423Sbostic if (*fname && !(freopen(fname, "r", stdin))) { 14339423Sbostic (void)fprintf(stderr, "wall: can't read %s.\n", fname); 14435531Sbostic exit(1); 14516256Sralph } 14635531Sbostic while (fgets(lbuf, sizeof(lbuf), stdin)) 14739089Smarc for (cnt = 0, p = lbuf; ch = *p; ++p, ++cnt) { 14839089Smarc if (cnt == 79 || ch == '\n') { 14939423Sbostic for (; cnt < 79; ++cnt) 15039423Sbostic putc(' ', fp); 15135531Sbostic putc('\r', fp); 15235531Sbostic putc('\n', fp); 15339089Smarc cnt = 0; 15435531Sbostic } else 15535531Sbostic putc(ch, fp); 15639089Smarc } 15739423Sbostic (void)fprintf(fp, "%79s\r\n", " "); 15835531Sbostic rewind(fp); 15935531Sbostic 16035531Sbostic if (fstat(fd, &sbuf)) { 16139423Sbostic (void)fprintf(stderr, "wall: can't stat temporary file.\n"); 16235531Sbostic exit(1); 16316256Sralph } 16435531Sbostic mbufsize = sbuf.st_size; 16535531Sbostic if (!(mbuf = malloc((u_int)mbufsize))) { 16639423Sbostic (void)fprintf(stderr, "wall: out of memory.\n"); 16735531Sbostic exit(1); 16816256Sralph } 16935776Sbostic if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) { 17039423Sbostic (void)fprintf(stderr, "wall: can't read temporary file.\n"); 17135776Sbostic exit(1); 17235776Sbostic } 17335531Sbostic (void)close(fd); 17435531Sbostic } 175