2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * This program is not related to David Wall, whose Stanford Ph.D. thesis
34 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
37 #include <sys/param.h>
58 static void makemsg(char *);
59 static void usage(void) __dead2;
61 static struct wallgroup {
62 struct wallgroup *next;
74 char ttybuf[MAXPATHLEN];
76 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
77 if (stat(ttybuf, &sb) == 0) {
84 main(int argc, char *argv[])
96 (void)setlocale(LC_CTYPE, "");
98 while ((ch = getopt(argc, argv, "g:n")) != -1)
101 /* undoc option for shutdown: suppress banner */
106 g = (struct wallgroup *)malloc(sizeof *g);
121 for (g = grouplist; g; g = g->next) {
122 grp = getgrnam(g->name);
124 g->gid = grp->gr_gid;
126 warnx("%s: no such group", g->name);
132 iov.iov_len = mbufsize;
134 while ((utmp = getutxent()) != NULL) {
135 if (utmp->ut_type != USER_PROCESS)
137 if (ttystat(utmp->ut_line) != 0)
141 pw = getpwnam(utmp->ut_user);
144 for (g = grouplist; g && ingroup == 0; g = g->next) {
145 if (g->gid == (gid_t)-1)
147 if (g->gid == pw->pw_gid)
149 else if ((grp = getgrgid(g->gid)) != NULL) {
150 for (np = grp->gr_mem; *np; np++) {
151 if (strcmp(*np, utmp->ut_user) == 0) {
161 if ((p = ttymsg(&iov, 1, utmp->ut_line, 60*5)) != NULL)
170 (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
185 char hostname[MAXHOSTNAMELEN], tmpname[64];
186 wchar_t *p, *tmp, lbuf[256], codebuf[13];
191 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
192 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
193 err(1, "can't open temporary file");
194 (void)unlink(tmpname);
197 tty = ttyname(STDERR_FILENO);
201 if (!(whom = getlogin()))
202 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
203 (void)gethostname(hostname, sizeof(hostname));
205 lt = localtime(&now);
208 * all this stuff is to blank out a square for the message;
209 * we wrap message lines at column 79, not 80, because some
210 * terminals wrap after 79, some do not, and we can't tell.
211 * Which means that we may leave a non-blank character
212 * in column 80, but that can't be helped.
214 (void)fwprintf(fp, L"\r%79s\r\n", " ");
215 (void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t),
216 L"Broadcast Message from %s@%s",
218 (void)fwprintf(fp, L"%-79.79S\007\007\r\n", lbuf);
219 (void)swprintf(lbuf, sizeof(lbuf)/sizeof(wchar_t),
220 L" (%s) at %d:%02d %s...", tty,
221 lt->tm_hour, lt->tm_min, lt->tm_zone);
222 (void)fwprintf(fp, L"%-79.79S\r\n", lbuf);
224 (void)fwprintf(fp, L"%79s\r\n", " ");
229 if (freopen(fname, "r", stdin) == NULL)
230 err(1, "can't read %s", fname);
231 if (setegid(egid) != 0)
232 err(1, "setegid failed");
235 while (fgetws(lbuf, sizeof(lbuf)/sizeof(wchar_t), stdin)) {
236 for (p = lbuf; (ch = *p) != L'\0'; ++p, ++cnt) {
241 } else if (ch == L'\n') {
242 for (; cnt < 79; ++cnt)
253 if (iswprint(ch) || iswspace(ch) || ch == L'\a' || ch == L'\b') {
256 (void)swprintf(codebuf, sizeof(codebuf)/sizeof(wchar_t), L"<0x%X>", ch);
257 for (tmp = codebuf; *tmp != L'\0'; ++tmp) {
269 (void)fwprintf(fp, L"%79s\r\n", " ");
272 if (fstat(fd, &sbuf))
273 err(1, "can't stat temporary file");
274 mbufsize = sbuf.st_size;
275 if (!(mbuf = malloc((u_int)mbufsize)))
276 err(1, "out of memory");
277 if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
278 err(1, "can't read temporary file");