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*39423Sbostic static char sccsid[] = "@(#)wall.c 5.10 (Berkeley) 10/28/89"; 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> 34*39423Sbostic #include <sys/stat.h> 3535531Sbostic #include <sys/time.h> 36*39423Sbostic #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 4439089Smarc int mbufsize; 4539089Smarc char *mbuf; 461159Sbill 4735531Sbostic /* ARGSUSED */ 481159Sbill main(argc, argv) 4935531Sbostic int argc; 5035531Sbostic char **argv; 511159Sbill { 52*39423Sbostic struct iovec iov; 5335531Sbostic struct utmp utmp; 5435531Sbostic FILE *fp; 55*39423Sbostic char *p, *ttymsg(); 561159Sbill 5735531Sbostic if (argc > 2) { 58*39423Sbostic (void)fprintf(stderr, "usage: wall [file]\n"); 591159Sbill exit(1); 601159Sbill } 6135531Sbostic 62*39423Sbostic makemsg(*++argv); 63*39423Sbostic 6439089Smarc if (!(fp = fopen(_PATH_UTMP, "r"))) { 65*39423Sbostic (void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP); 6635531Sbostic exit(1); 671159Sbill } 68*39423Sbostic iov.iov_base = mbuf; 69*39423Sbostic iov.iov_len = mbufsize; 7035531Sbostic /* NOSTRICT */ 71*39423Sbostic while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) { 72*39423Sbostic if (!utmp.ut_name[0] || 73*39423Sbostic !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name))) 74*39423Sbostic continue; 75*39423Sbostic if (p = ttymsg(&iov, 1, utmp.ut_line, 1)) 76*39423Sbostic (void)fprintf(stderr, "wall: %s\n", p); 77*39423Sbostic } 781159Sbill exit(0); 791159Sbill } 801159Sbill 81*39423Sbostic makemsg(fname) 82*39423Sbostic char *fname; 831159Sbill { 8435531Sbostic register int ch, cnt; 8535531Sbostic struct tm *lt; 8635531Sbostic struct passwd *pw, *getpwuid(); 8735531Sbostic struct stat sbuf; 8835531Sbostic time_t now, time(); 8935531Sbostic FILE *fp; 9035531Sbostic int fd; 9135531Sbostic char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15]; 9235531Sbostic char *getlogin(), *malloc(), *strcpy(), *ttyname(); 931159Sbill 9439089Smarc (void)strcpy(tmpname, _PATH_TMP); 9539124Sbostic (void)strcat(tmpname, "/wall.XXXXXX"); 9635531Sbostic if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) { 97*39423Sbostic (void)fprintf(stderr, "wall: can't open temporary file.\n"); 9835531Sbostic exit(1); 9935531Sbostic } 10035531Sbostic (void)unlink(tmpname); 10116256Sralph 10235531Sbostic if (!(whom = getlogin())) 10335531Sbostic whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 10435531Sbostic (void)gethostname(hostname, sizeof(hostname)); 10535531Sbostic (void)time(&now); 10635531Sbostic lt = localtime(&now); 10735531Sbostic 10835531Sbostic /* 109*39423Sbostic * all this stuff is to blank out a square for the message; we wrap 110*39423Sbostic * message lines at column 79, not 80, because some terminals wrap 111*39423Sbostic * after 79, some do not, and we can't tell. Which means that we 112*39423Sbostic * may leave a non-blank character in column 80, but that can't be 113*39423Sbostic * helped. 11435531Sbostic */ 115*39423Sbostic (void)fprintf(fp, "\r%79s\r\n", " "); 11635531Sbostic (void)sprintf(lbuf, "Broadcast Message from %s@%s", whom, hostname); 117*39423Sbostic (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 11835531Sbostic (void)sprintf(lbuf, " (%s) at %d:%02d ...", ttyname(2), 11935531Sbostic lt->tm_hour, lt->tm_min); 120*39423Sbostic (void)fprintf(fp, "%-79.79s\r\n", lbuf); 121*39423Sbostic (void)fprintf(fp, "%79s\r\n", " "); 12235531Sbostic 123*39423Sbostic if (*fname && !(freopen(fname, "r", stdin))) { 124*39423Sbostic (void)fprintf(stderr, "wall: can't read %s.\n", fname); 12535531Sbostic exit(1); 12616256Sralph } 12735531Sbostic while (fgets(lbuf, sizeof(lbuf), stdin)) 12839089Smarc for (cnt = 0, p = lbuf; ch = *p; ++p, ++cnt) { 12939089Smarc if (cnt == 79 || ch == '\n') { 130*39423Sbostic for (; cnt < 79; ++cnt) 131*39423Sbostic putc(' ', fp); 13235531Sbostic putc('\r', fp); 13335531Sbostic putc('\n', fp); 13439089Smarc cnt = 0; 13535531Sbostic } else 13635531Sbostic putc(ch, fp); 13739089Smarc } 138*39423Sbostic (void)fprintf(fp, "%79s\r\n", " "); 13935531Sbostic rewind(fp); 14035531Sbostic 14135531Sbostic if (fstat(fd, &sbuf)) { 142*39423Sbostic (void)fprintf(stderr, "wall: can't stat temporary file.\n"); 14335531Sbostic exit(1); 14416256Sralph } 14535531Sbostic mbufsize = sbuf.st_size; 14635531Sbostic if (!(mbuf = malloc((u_int)mbufsize))) { 147*39423Sbostic (void)fprintf(stderr, "wall: out of memory.\n"); 14835531Sbostic exit(1); 14916256Sralph } 15035776Sbostic if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) { 151*39423Sbostic (void)fprintf(stderr, "wall: can't read temporary file.\n"); 15235776Sbostic exit(1); 15335776Sbostic } 15435531Sbostic (void)close(fd); 15535531Sbostic } 156