1 /* 2 * Copyright (c) 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 char copyright[] = 20 "@(#) Copyright (c) 1988 Regents of the University of California.\n\ 21 All rights reserved.\n"; 22 #endif /* not lint */ 23 24 #ifndef lint 25 static char sccsid[] = "@(#)wall.c 5.10 (Berkeley) 10/28/89"; 26 #endif /* not lint */ 27 28 /* 29 * This program is not related to David Wall, whose Stanford Ph.D. thesis 30 * is entitled "Mechanisms for Broadcast and Selective Broadcast". 31 */ 32 33 #include <sys/param.h> 34 #include <sys/stat.h> 35 #include <sys/time.h> 36 #include <sys/uio.h> 37 #include <utmp.h> 38 #include <pwd.h> 39 #include <stdio.h> 40 #include <paths.h> 41 42 #define IGNOREUSER "sleeper" 43 44 int mbufsize; 45 char *mbuf; 46 47 /* ARGSUSED */ 48 main(argc, argv) 49 int argc; 50 char **argv; 51 { 52 struct iovec iov; 53 struct utmp utmp; 54 FILE *fp; 55 char *p, *ttymsg(); 56 57 if (argc > 2) { 58 (void)fprintf(stderr, "usage: wall [file]\n"); 59 exit(1); 60 } 61 62 makemsg(*++argv); 63 64 if (!(fp = fopen(_PATH_UTMP, "r"))) { 65 (void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP); 66 exit(1); 67 } 68 iov.iov_base = mbuf; 69 iov.iov_len = mbufsize; 70 /* NOSTRICT */ 71 while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) { 72 if (!utmp.ut_name[0] || 73 !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name))) 74 continue; 75 if (p = ttymsg(&iov, 1, utmp.ut_line, 1)) 76 (void)fprintf(stderr, "wall: %s\n", p); 77 } 78 exit(0); 79 } 80 81 makemsg(fname) 82 char *fname; 83 { 84 register int ch, cnt; 85 struct tm *lt; 86 struct passwd *pw, *getpwuid(); 87 struct stat sbuf; 88 time_t now, time(); 89 FILE *fp; 90 int fd; 91 char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15]; 92 char *getlogin(), *malloc(), *strcpy(), *ttyname(); 93 94 (void)strcpy(tmpname, _PATH_TMP); 95 (void)strcat(tmpname, "/wall.XXXXXX"); 96 if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) { 97 (void)fprintf(stderr, "wall: can't open temporary file.\n"); 98 exit(1); 99 } 100 (void)unlink(tmpname); 101 102 if (!(whom = getlogin())) 103 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 104 (void)gethostname(hostname, sizeof(hostname)); 105 (void)time(&now); 106 lt = localtime(&now); 107 108 /* 109 * all this stuff is to blank out a square for the message; we wrap 110 * message lines at column 79, not 80, because some terminals wrap 111 * after 79, some do not, and we can't tell. Which means that we 112 * may leave a non-blank character in column 80, but that can't be 113 * helped. 114 */ 115 (void)fprintf(fp, "\r%79s\r\n", " "); 116 (void)sprintf(lbuf, "Broadcast Message from %s@%s", whom, hostname); 117 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf); 118 (void)sprintf(lbuf, " (%s) at %d:%02d ...", ttyname(2), 119 lt->tm_hour, lt->tm_min); 120 (void)fprintf(fp, "%-79.79s\r\n", lbuf); 121 (void)fprintf(fp, "%79s\r\n", " "); 122 123 if (*fname && !(freopen(fname, "r", stdin))) { 124 (void)fprintf(stderr, "wall: can't read %s.\n", fname); 125 exit(1); 126 } 127 while (fgets(lbuf, sizeof(lbuf), stdin)) 128 for (cnt = 0, p = lbuf; ch = *p; ++p, ++cnt) { 129 if (cnt == 79 || ch == '\n') { 130 for (; cnt < 79; ++cnt) 131 putc(' ', fp); 132 putc('\r', fp); 133 putc('\n', fp); 134 cnt = 0; 135 } else 136 putc(ch, fp); 137 } 138 (void)fprintf(fp, "%79s\r\n", " "); 139 rewind(fp); 140 141 if (fstat(fd, &sbuf)) { 142 (void)fprintf(stderr, "wall: can't stat temporary file.\n"); 143 exit(1); 144 } 145 mbufsize = sbuf.st_size; 146 if (!(mbuf = malloc((u_int)mbufsize))) { 147 (void)fprintf(stderr, "wall: out of memory.\n"); 148 exit(1); 149 } 150 if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) { 151 (void)fprintf(stderr, "wall: can't read temporary file.\n"); 152 exit(1); 153 } 154 (void)close(fd); 155 } 156