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 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 static char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93"; 42 #endif /* not lint */ 43 44 /* 45 * This program is not related to David Wall, whose Stanford Ph.D. thesis 46 * is entitled "Mechanisms for Broadcast and Selective Broadcast". 47 */ 48 49 #include <sys/param.h> 50 #include <sys/stat.h> 51 #include <sys/time.h> 52 #include <sys/uio.h> 53 54 #include <locale.h> 55 #include <paths.h> 56 #include <pwd.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 #include <utmp.h> 62 63 void makemsg __P((char *)); 64 65 #define IGNOREUSER "sleeper" 66 67 int nobanner; 68 int mbufsize; 69 char *mbuf; 70 71 /* ARGSUSED */ 72 int 73 main(argc, argv) 74 int argc; 75 char **argv; 76 { 77 extern int optind; 78 int ch; 79 struct iovec iov; 80 struct utmp utmp; 81 FILE *fp; 82 char *p, *ttymsg(); 83 char line[sizeof(utmp.ut_line) + 1]; 84 85 (void)setlocale(LC_CTYPE, ""); 86 87 while ((ch = getopt(argc, argv, "n")) != -1) 88 switch (ch) { 89 case 'n': 90 /* undoc option for shutdown: suppress banner */ 91 if (geteuid() == 0) 92 nobanner = 1; 93 break; 94 case '?': 95 default: 96 usage: 97 (void)fprintf(stderr, "usage: wall [file]\n"); 98 exit(1); 99 } 100 argc -= optind; 101 argv += optind; 102 if (argc > 1) 103 goto usage; 104 105 makemsg(*argv); 106 107 if (!(fp = fopen(_PATH_UTMP, "r"))) { 108 (void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP); 109 exit(1); 110 } 111 iov.iov_base = mbuf; 112 iov.iov_len = mbufsize; 113 /* NOSTRICT */ 114 while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) { 115 if (!utmp.ut_name[0] || 116 !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name))) 117 continue; 118 strncpy(line, utmp.ut_line, sizeof(utmp.ut_line)); 119 line[sizeof(utmp.ut_line)] = '\0'; 120 if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL) 121 (void)fprintf(stderr, "wall: %s\n", p); 122 } 123 exit(0); 124 } 125 126 void 127 makemsg(fname) 128 char *fname; 129 { 130 register int cnt; 131 register unsigned char ch; 132 struct tm *lt; 133 struct passwd *pw; 134 struct stat sbuf; 135 time_t now, time(); 136 FILE *fp; 137 int fd; 138 char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15]; 139 char *getlogin(), *strcpy(), *ttyname(); 140 141 (void)strcpy(tmpname, _PATH_TMP); 142 (void)strcat(tmpname, "/wall.XXXXXX"); 143 if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) { 144 (void)fprintf(stderr, "wall: can't open temporary file.\n"); 145 exit(1); 146 } 147 (void)unlink(tmpname); 148 149 if (!nobanner) { 150 if (!(whom = getlogin())) 151 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 152 (void)gethostname(hostname, sizeof(hostname)); 153 (void)time(&now); 154 lt = localtime(&now); 155 156 /* 157 * all this stuff is to blank out a square for the message; 158 * we wrap message lines at column 79, not 80, because some 159 * terminals wrap after 79, some do not, and we can't tell. 160 * Which means that we may leave a non-blank character 161 * in column 80, but that can't be helped. 162 */ 163 (void)fprintf(fp, "\r%79s\r\n", " "); 164 (void)sprintf(lbuf, "Broadcast Message from %s@%s", 165 whom, hostname); 166 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 167 (void)sprintf(lbuf, " (%s) at %d:%02d ...", ttyname(2), 168 lt->tm_hour, lt->tm_min); 169 (void)fprintf(fp, "%-79.79s\r\n", lbuf); 170 } 171 (void)fprintf(fp, "%79s\r\n", " "); 172 173 if (fname && !(freopen(fname, "r", stdin))) { 174 (void)fprintf(stderr, "wall: can't read %s.\n", fname); 175 exit(1); 176 } 177 while (fgets(lbuf, sizeof(lbuf), stdin)) 178 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { 179 again: 180 if (cnt == 79 || ch == '\n') { 181 for (; cnt < 79; ++cnt) 182 putc(' ', fp); 183 putc('\r', fp); 184 putc('\n', fp); 185 cnt = 0; 186 } else if ( ( (ch & 0x7F) < ' ' /* locale-independent */ 187 || (!isprint(ch) && !isspace(ch)) 188 ) 189 && strchr("\a\f\t\v\b\r\n", ch) == NULL 190 ) { 191 if (ch & 0x80) { 192 ch &= 0x7F; 193 (void)fprintf(fp, "M-"); 194 goto again; 195 } 196 putc('^', fp); 197 putc(ch^0x40, fp); /* DEL to ?, others to alpha */ 198 } else 199 putc(ch, fp); 200 } 201 (void)fprintf(fp, "%79s\r\n", " "); 202 rewind(fp); 203 204 if (fstat(fd, &sbuf)) { 205 (void)fprintf(stderr, "wall: can't stat temporary file.\n"); 206 exit(1); 207 } 208 mbufsize = sbuf.st_size; 209 if (!(mbuf = malloc((u_int)mbufsize))) { 210 (void)fprintf(stderr, "wall: out of memory.\n"); 211 exit(1); 212 } 213 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) { 214 (void)fprintf(stderr, "wall: can't read temporary file.\n"); 215 exit(1); 216 } 217 (void)close(fd); 218 } 219