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