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 35 static 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 42 static char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93"; 43 #endif 44 static const char rcsid[] = 45 "$Id: wall.c,v 1.8 1997/08/25 06:43:22 charnier Exp $"; 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/uio.h> 56 57 #include <ctype.h> 58 #include <err.h> 59 #include <locale.h> 60 #include <paths.h> 61 #include <pwd.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <time.h> 66 #include <unistd.h> 67 #include <utmp.h> 68 69 void makemsg __P((char *)); 70 static void usage __P((void)); 71 72 #define IGNOREUSER "sleeper" 73 74 int nobanner; 75 int mbufsize; 76 char *mbuf; 77 78 /* ARGSUSED */ 79 int 80 main(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 128 static void 129 usage() 130 { 131 (void)fprintf(stderr, "usage: wall [file]\n"); 132 exit(1); 133 } 134 135 void 136 makemsg(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; 145 FILE *fp; 146 int fd; 147 char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15]; 148 149 (void)strcpy(tmpname, _PATH_TMP); 150 (void)strcat(tmpname, "/wall.XXXXXX"); 151 if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) 152 errx(1, "can't open temporary file"); 153 (void)unlink(tmpname); 154 155 if (!nobanner) { 156 if (!(whom = getlogin())) 157 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 158 (void)gethostname(hostname, sizeof(hostname)); 159 (void)time(&now); 160 lt = localtime(&now); 161 162 /* 163 * all this stuff is to blank out a square for the message; 164 * we wrap message lines at column 79, not 80, because some 165 * terminals wrap after 79, some do not, and we can't tell. 166 * Which means that we may leave a non-blank character 167 * in column 80, but that can't be helped. 168 */ 169 (void)fprintf(fp, "\r%79s\r\n", " "); 170 (void)sprintf(lbuf, "Broadcast Message from %s@%s", 171 whom, hostname); 172 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 173 (void)sprintf(lbuf, " (%s) at %d:%02d ...", ttyname(2), 174 lt->tm_hour, lt->tm_min); 175 (void)fprintf(fp, "%-79.79s\r\n", lbuf); 176 } 177 (void)fprintf(fp, "%79s\r\n", " "); 178 179 if (fname && !(freopen(fname, "r", stdin))) 180 errx(1, "can't read %s", fname); 181 while (fgets(lbuf, sizeof(lbuf), stdin)) 182 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { 183 if (cnt == 79 || ch == '\n') { 184 for (; cnt < 79; ++cnt) 185 putc(' ', fp); 186 putc('\r', fp); 187 putc('\n', fp); 188 cnt = 0; 189 } else if (((ch & 0x80) && ch < 0xA0) || 190 /* disable upper controls */ 191 (!isprint(ch) && !isspace(ch) && 192 ch != '\a' && ch != '\b') 193 ) { 194 if (ch & 0x80) { 195 ch &= 0x7F; 196 putc('M', fp); 197 if (++cnt == 79) { 198 putc('\r', fp); 199 putc('\n', fp); 200 cnt = 0; 201 } 202 putc('-', fp); 203 if (++cnt == 79) { 204 putc('\r', fp); 205 putc('\n', fp); 206 cnt = 0; 207 } 208 } 209 if (iscntrl(ch)) { 210 ch ^= 040; 211 putc('^', fp); 212 if (++cnt == 79) { 213 putc('\r', fp); 214 putc('\n', fp); 215 cnt = 0; 216 } 217 } 218 } 219 putc(ch, fp); 220 } 221 (void)fprintf(fp, "%79s\r\n", " "); 222 rewind(fp); 223 224 if (fstat(fd, &sbuf)) 225 errx(1, "can't stat temporary file"); 226 mbufsize = sbuf.st_size; 227 if (!(mbuf = malloc((u_int)mbufsize))) 228 errx(1, "out of memory"); 229 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) 230 errx(1, "can't read temporary file"); 231 (void)close(fd); 232 } 233