xref: /freebsd/usr.bin/wall/wall.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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   "$FreeBSD$";
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 char   *ttymsg __P((struct iovec *, int, char *, int));
72 
73 #define	IGNOREUSER	"sleeper"
74 
75 int nobanner;
76 int mbufsize;
77 char *mbuf;
78 
79 /* ARGSUSED */
80 int
81 main(argc, argv)
82 	int argc;
83 	char **argv;
84 {
85 	int ch;
86 	struct iovec iov;
87 	struct utmp utmp;
88 	FILE *fp;
89 	char *p;
90 	char line[sizeof(utmp.ut_line) + 1];
91 
92 	(void)setlocale(LC_CTYPE, "");
93 
94 	while ((ch = getopt(argc, argv, "n")) != -1)
95 		switch (ch) {
96 		case 'n':
97 			/* undoc option for shutdown: suppress banner */
98 			if (geteuid() == 0)
99 				nobanner = 1;
100 			break;
101 		case '?':
102 		default:
103 			usage();
104 		}
105 	argc -= optind;
106 	argv += optind;
107 	if (argc > 1)
108 		usage();
109 
110 	makemsg(*argv);
111 
112 	if (!(fp = fopen(_PATH_UTMP, "r")))
113 		err(1, "cannot read %s", _PATH_UTMP);
114 	iov.iov_base = mbuf;
115 	iov.iov_len = mbufsize;
116 	/* NOSTRICT */
117 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
118 		if (!utmp.ut_name[0] ||
119 		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
120 			continue;
121 		strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
122 		line[sizeof(utmp.ut_line)] = '\0';
123 		if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
124 			warnx("%s", p);
125 	}
126 	exit(0);
127 }
128 
129 static void
130 usage()
131 {
132 	(void)fprintf(stderr, "usage: wall [file]\n");
133 	exit(1);
134 }
135 
136 void
137 makemsg(fname)
138 	char *fname;
139 {
140 	register int cnt;
141 	register unsigned char ch;
142 	struct tm *lt;
143 	struct passwd *pw;
144 	struct stat sbuf;
145 	time_t now;
146 	FILE *fp;
147 	int fd;
148 	char *p, *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
149 	const char *whom;
150 
151 	(void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
152 	if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
153 		err(1, "can't open temporary file");
154 	(void)unlink(tmpname);
155 
156 	if (!nobanner) {
157 		tty = ttyname(STDERR_FILENO);
158 		if (tty == NULL)
159 			tty = "no tty";
160 
161 		if (!(whom = getlogin()))
162 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
163 		(void)gethostname(hostname, sizeof(hostname));
164 		(void)time(&now);
165 		lt = localtime(&now);
166 
167 		/*
168 		 * all this stuff is to blank out a square for the message;
169 		 * we wrap message lines at column 79, not 80, because some
170 		 * terminals wrap after 79, some do not, and we can't tell.
171 		 * Which means that we may leave a non-blank character
172 		 * in column 80, but that can't be helped.
173 		 */
174 		(void)fprintf(fp, "\r%79s\r\n", " ");
175 		(void)snprintf(lbuf, sizeof(lbuf),
176 		    "Broadcast Message from %s@%s",
177 		    whom, hostname);
178 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
179 		(void)snprintf(lbuf, sizeof(lbuf),
180 		    "        (%s) at %d:%02d %s...", tty,
181 		    lt->tm_hour, lt->tm_min, lt->tm_zone);
182 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
183 	}
184 	(void)fprintf(fp, "%79s\r\n", " ");
185 
186 	if (fname && !(freopen(fname, "r", stdin)))
187 		err(1, "can't read %s", fname);
188 	while (fgets(lbuf, sizeof(lbuf), stdin))
189 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
190 			if (ch == '\r') {
191 				cnt = 0;
192 			} else if (cnt == 79 || ch == '\n') {
193 				for (; cnt < 79; ++cnt)
194 					putc(' ', fp);
195 				putc('\r', fp);
196 				putc('\n', fp);
197 				cnt = 0;
198 			} else if (((ch & 0x80) && ch < 0xA0) ||
199 				   /* disable upper controls */
200 				   (!isprint(ch) && !isspace(ch) &&
201 				    ch != '\a' && ch != '\b')
202 				  ) {
203 				if (ch & 0x80) {
204 					ch &= 0x7F;
205 					putc('M', fp);
206 					if (++cnt == 79) {
207 						putc('\r', fp);
208 						putc('\n', fp);
209 						cnt = 0;
210 					}
211 					putc('-', fp);
212 					if (++cnt == 79) {
213 						putc('\r', fp);
214 						putc('\n', fp);
215 						cnt = 0;
216 					}
217 				}
218 				if (iscntrl(ch)) {
219 					ch ^= 040;
220 					putc('^', fp);
221 					if (++cnt == 79) {
222 						putc('\r', fp);
223 						putc('\n', fp);
224 						cnt = 0;
225 					}
226 				}
227 				putc(ch, fp);
228 			} else {
229 				putc(ch, fp);
230 			}
231 		}
232 	(void)fprintf(fp, "%79s\r\n", " ");
233 	rewind(fp);
234 
235 	if (fstat(fd, &sbuf))
236 		err(1, "can't stat temporary file");
237 	mbufsize = sbuf.st_size;
238 	if (!(mbuf = malloc((u_int)mbufsize)))
239 		err(1, "out of memory");
240 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
241 		err(1, "can't read temporary file");
242 	(void)close(fd);
243 }
244