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