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