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