wall.c revision 28695
1/* 2 * Copyright (c) 1988, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1988, 1990, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93"; 43#endif 44static const char rcsid[] = 45 "$Id$"; 46#endif /* not lint */ 47 48/* 49 * This program is not related to David Wall, whose Stanford Ph.D. thesis 50 * is entitled "Mechanisms for Broadcast and Selective Broadcast". 51 */ 52 53#include <sys/param.h> 54#include <sys/stat.h> 55#include <sys/time.h> 56#include <sys/uio.h> 57 58#include <ctype.h> 59#include <err.h> 60#include <locale.h> 61#include <paths.h> 62#include <pwd.h> 63#include <stdio.h> 64#include <stdlib.h> 65#include <string.h> 66#include <unistd.h> 67#include <utmp.h> 68 69void makemsg __P((char *)); 70static void usage __P((void)); 71 72#define IGNOREUSER "sleeper" 73 74int nobanner; 75int mbufsize; 76char *mbuf; 77 78/* ARGSUSED */ 79int 80main(argc, argv) 81 int argc; 82 char **argv; 83{ 84 int ch; 85 struct iovec iov; 86 struct utmp utmp; 87 FILE *fp; 88 char *p, *ttymsg(); 89 char line[sizeof(utmp.ut_line) + 1]; 90 91 (void)setlocale(LC_CTYPE, ""); 92 93 while ((ch = getopt(argc, argv, "n")) != -1) 94 switch (ch) { 95 case 'n': 96 /* undoc option for shutdown: suppress banner */ 97 if (geteuid() == 0) 98 nobanner = 1; 99 break; 100 case '?': 101 default: 102 usage(); 103 } 104 argc -= optind; 105 argv += optind; 106 if (argc > 1) 107 usage(); 108 109 makemsg(*argv); 110 111 if (!(fp = fopen(_PATH_UTMP, "r"))) 112 errx(1, "cannot read %s", _PATH_UTMP); 113 iov.iov_base = mbuf; 114 iov.iov_len = mbufsize; 115 /* NOSTRICT */ 116 while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) { 117 if (!utmp.ut_name[0] || 118 !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name))) 119 continue; 120 strncpy(line, utmp.ut_line, sizeof(utmp.ut_line)); 121 line[sizeof(utmp.ut_line)] = '\0'; 122 if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL) 123 warnx("%s", p); 124 } 125 exit(0); 126} 127 128static void 129usage() 130{ 131 (void)fprintf(stderr, "usage: wall [file]\n"); 132 exit(1); 133} 134 135void 136makemsg(fname) 137 char *fname; 138{ 139 register int cnt; 140 register unsigned char ch; 141 struct tm *lt; 142 struct passwd *pw; 143 struct stat sbuf; 144 time_t now, time(); 145 FILE *fp; 146 int fd; 147 char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15]; 148 char *getlogin(), *strcpy(), *ttyname(); 149 150 (void)strcpy(tmpname, _PATH_TMP); 151 (void)strcat(tmpname, "/wall.XXXXXX"); 152 if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) 153 errx(1, "can't open temporary file"); 154 (void)unlink(tmpname); 155 156 if (!nobanner) { 157 if (!(whom = getlogin())) 158 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 159 (void)gethostname(hostname, sizeof(hostname)); 160 (void)time(&now); 161 lt = localtime(&now); 162 163 /* 164 * all this stuff is to blank out a square for the message; 165 * we wrap message lines at column 79, not 80, because some 166 * terminals wrap after 79, some do not, and we can't tell. 167 * Which means that we may leave a non-blank character 168 * in column 80, but that can't be helped. 169 */ 170 (void)fprintf(fp, "\r%79s\r\n", " "); 171 (void)sprintf(lbuf, "Broadcast Message from %s@%s", 172 whom, hostname); 173 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 174 (void)sprintf(lbuf, " (%s) at %d:%02d ...", ttyname(2), 175 lt->tm_hour, lt->tm_min); 176 (void)fprintf(fp, "%-79.79s\r\n", lbuf); 177 } 178 (void)fprintf(fp, "%79s\r\n", " "); 179 180 if (fname && !(freopen(fname, "r", stdin))) 181 errx(1, "can't read %s", fname); 182 while (fgets(lbuf, sizeof(lbuf), stdin)) 183 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { 184 again: 185 if (cnt == 79 || ch == '\n') { 186 for (; cnt < 79; ++cnt) 187 putc(' ', fp); 188 putc('\r', fp); 189 putc('\n', fp); 190 cnt = 0; 191 } else if ( ( (ch & 0x7F) < ' ' /* locale-independent */ 192 || (!isprint(ch) && !isspace(ch)) 193 ) 194 && strchr("\a\f\t\v\b\r\n", ch) == NULL 195 ) { 196 if (ch & 0x80) { 197 ch &= 0x7F; 198 (void)fprintf(fp, "M-"); 199 goto again; 200 } 201 putc('^', fp); 202 putc(ch^0x40, fp); /* DEL to ?, others to alpha */ 203 } else 204 putc(ch, fp); 205 } 206 (void)fprintf(fp, "%79s\r\n", " "); 207 rewind(fp); 208 209 if (fstat(fd, &sbuf)) 210 errx(1, "can't stat temporary file"); 211 mbufsize = sbuf.st_size; 212 if (!(mbuf = malloc((u_int)mbufsize))) 213 errx(1, "out of memory"); 214 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 215 errx(1, "can't read temporary file"); 216 (void)close(fd); 217} 218