Annotation of src/usr.bin/wall/wall.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: wall.c,v 1.3 1996/08/06 19:25:35 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: wall.c,v 1.6 1994/11/17 07:17:58 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1988, 1990, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1988, 1990, 1993\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93";
46: #endif
1.4 ! deraadt 47: static char rcsid[] = "$OpenBSD: wall.c,v 1.3 1996/08/06 19:25:35 deraadt Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: /*
51: * This program is not related to David Wall, whose Stanford Ph.D. thesis
52: * is entitled "Mechanisms for Broadcast and Selective Broadcast".
53: */
54:
55: #include <sys/param.h>
56: #include <sys/stat.h>
57: #include <sys/time.h>
58: #include <sys/uio.h>
59:
60: #include <paths.h>
61: #include <pwd.h>
62: #include <stdio.h>
63: #include <stdlib.h>
64: #include <string.h>
65: #include <unistd.h>
66: #include <utmp.h>
1.4 ! deraadt 67: #include <vis.h>
1.1 deraadt 68:
69: void makemsg __P((char *));
70:
71: #define IGNOREUSER "sleeper"
72:
73: int nobanner;
74: int mbufsize;
75: char *mbuf;
76:
77: /* ARGSUSED */
78: int
79: main(argc, argv)
80: int argc;
81: char **argv;
82: {
83: extern int optind;
84: int ch;
85: struct iovec iov;
86: struct utmp utmp;
87: FILE *fp;
88: char *p, *ttymsg();
89: struct passwd *pep = getpwnam("nobody");
90: char line[sizeof(utmp.ut_line) + 1];
91:
92: while ((ch = getopt(argc, argv, "n")) != EOF)
93: switch (ch) {
94: case 'n':
95: /* undoc option for shutdown: suppress banner */
96: if (geteuid() == 0 || (pep && getuid() == pep->pw_uid))
97: nobanner = 1;
98: break;
99: case '?':
100: default:
101: usage:
102: (void)fprintf(stderr, "usage: wall [file]\n");
103: exit(1);
104: }
105: argc -= optind;
106: argv += optind;
107: if (argc > 1)
108: goto usage;
109:
110: makemsg(*argv);
111:
112: if (!(fp = fopen(_PATH_UTMP, "r"))) {
113: (void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP);
114: exit(1);
115: }
116: iov.iov_base = mbuf;
117: iov.iov_len = mbufsize;
118: /* NOSTRICT */
119: while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
120: if (!utmp.ut_name[0] ||
121: !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
122: continue;
123: strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
124: line[sizeof(utmp.ut_line)] = '\0';
125: if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
126: (void)fprintf(stderr, "wall: %s\n", p);
127: }
128: exit(0);
129: }
130:
131: void
132: makemsg(fname)
133: char *fname;
134: {
135: register int ch, cnt;
136: struct tm *lt;
137: struct passwd *pw;
138: struct stat sbuf;
139: time_t now, time();
140: FILE *fp;
141: int fd;
142: char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
1.4 ! deraadt 143: char tmpbuf[5];
1.1 deraadt 144:
145: (void)strcpy(tmpname, _PATH_TMP);
146: (void)strcat(tmpname, "/wall.XXXXXX");
147: if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
148: (void)fprintf(stderr, "wall: can't open temporary file.\n");
149: exit(1);
150: }
151: (void)unlink(tmpname);
152:
153: if (!nobanner) {
154: if (!(whom = getlogin()))
155: whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
156: (void)gethostname(hostname, sizeof(hostname));
157: (void)time(&now);
158: lt = localtime(&now);
159:
160: /*
161: * all this stuff is to blank out a square for the message;
162: * we wrap message lines at column 79, not 80, because some
163: * terminals wrap after 79, some do not, and we can't tell.
164: * Which means that we may leave a non-blank character
165: * in column 80, but that can't be helped.
166: */
167: (void)fprintf(fp, "\r%79s\r\n", " ");
1.3 deraadt 168: (void)snprintf(lbuf, sizeof lbuf,
169: "Broadcast Message from %s@%s", whom, hostname);
1.1 deraadt 170: (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
1.3 deraadt 171: (void)snprintf(lbuf, sizeof lbuf,
172: " (%s) at %d:%02d ...", ttyname(2),
1.1 deraadt 173: lt->tm_hour, lt->tm_min);
174: (void)fprintf(fp, "%-79.79s\r\n", lbuf);
175: }
176: (void)fprintf(fp, "%79s\r\n", " ");
177:
178: if (fname && !(freopen(fname, "r", stdin))) {
179: (void)fprintf(stderr, "wall: can't read %s.\n", fname);
180: exit(1);
181: }
182: while (fgets(lbuf, sizeof(lbuf), stdin))
183: for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
1.4 ! deraadt 184: vis(tmpbuf, ch, VIS_SAFE, p[1]);
! 185: if (cnt == 79+1-strlen(tmpbuf) || ch == '\n') {
! 186: for (; cnt < 79+1-strlen(tmpbuf); ++cnt)
1.1 deraadt 187: putc(' ', fp);
188: putc('\r', fp);
189: putc('\n', fp);
190: cnt = -1;
191: }
1.4 ! deraadt 192: if (ch != '\n') {
! 193: int xx;
! 194:
! 195: for (xx = 0; tmpbuf[xx]; xx++)
! 196: putc(tmpbuf[xx], fp);
! 197: }
1.1 deraadt 198: }
199: (void)fprintf(fp, "%79s\r\n", " ");
200: rewind(fp);
201:
202: if (fstat(fd, &sbuf)) {
203: (void)fprintf(stderr, "wall: can't stat temporary file.\n");
204: exit(1);
205: }
206: mbufsize = sbuf.st_size;
207: if (!(mbuf = malloc((u_int)mbufsize))) {
208: (void)fprintf(stderr, "wall: out of memory.\n");
209: exit(1);
210: }
211: if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
212: (void)fprintf(stderr, "wall: can't read temporary file.\n");
213: exit(1);
214: }
215: (void)close(fd);
216: }