2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#) Copyright (c) 1988, 1990, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)wall.c 8.2 (Berkeley) 11/16/93
35 * $FreeBSD: src/usr.bin/wall/wall.c,v 1.13.2.6 2001/10/18 08:08:17 des Exp $
36 * $DragonFly: src/usr.bin/wall/wall.c,v 1.4 2005/08/31 16:27:53 liamfoy Exp $
40 * This program is not related to David Wall, whose Stanford Ph.D. thesis
41 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
44 #include <sys/param.h>
59 #include "utmpentry.h"
63 static void makemsg(char *);
64 static void usage(void);
67 struct wallgroup *next;
76 main(int argc, char *argv[])
88 (void)setlocale(LC_CTYPE, "");
90 while ((ch = getopt(argc, argv, "g:n")) != -1)
93 /* undoc option for shutdown: suppress banner */
98 g = (struct wallgroup *)malloc(sizeof *g);
113 for (g = grouplist; g; g = g->next) {
114 grp = getgrnam(g->name);
116 g->gid = grp->gr_gid;
118 warnx("%s: no such group", g->name);
124 iov.iov_len = mbufsize;
126 getutentries(NULL, &ep);
128 for (; ep; ep = ep->next) {
131 pw = getpwnam(ep->name);
134 for (g = grouplist; g && ingroup == 0; g = g->next) {
135 if (g->gid == (gid_t)-1)
137 if (g->gid == pw->pw_gid)
139 else if ((grp = getgrgid(g->gid)) != NULL) {
140 for (np = grp->gr_mem; *np; np++) {
141 if (strcmp(*np, ep->name) == 0) {
151 /* skip [xgk]dm/xserver entries (":0", ":1", etc.) */
152 if (ep->line[0] == ':' && isdigit((unsigned char)ep->line[1]))
155 if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL)
164 (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
179 char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[MAXPATHLEN];
184 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
185 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
186 err(1, "can't open temporary file");
187 (void)unlink(tmpname);
190 tty = ttyname(STDERR_FILENO);
194 if (!(whom = getlogin()))
195 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
196 (void)gethostname(hostname, sizeof(hostname));
198 lt = localtime(&now);
201 * all this stuff is to blank out a square for the message;
202 * we wrap message lines at column 79, not 80, because some
203 * terminals wrap after 79, some do not, and we can't tell.
204 * Which means that we may leave a non-blank character
205 * in column 80, but that can't be helped.
207 (void)fprintf(fp, "\r%79s\r\n", " ");
208 (void)snprintf(lbuf, sizeof(lbuf),
209 "Broadcast Message from %s@%s",
211 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
212 (void)snprintf(lbuf, sizeof(lbuf),
213 " (%s) at %d:%02d %s...", tty,
214 lt->tm_hour, lt->tm_min, lt->tm_zone);
215 (void)fprintf(fp, "%-79.79s\r\n", lbuf);
217 (void)fprintf(fp, "%79s\r\n", " ");
222 if (freopen(fname, "r", stdin) == NULL)
223 err(1, "can't read %s", fname);
226 while (fgets(lbuf, sizeof(lbuf), stdin))
227 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
230 } else if (cnt == 79 || ch == '\n') {
231 for (; cnt < 79; ++cnt)
236 } else if (((ch & 0x80) && ch < 0xA0) ||
237 /* disable upper controls */
238 (!isprint(ch) && !isspace(ch) &&
239 ch != '\a' && ch != '\b')
270 (void)fprintf(fp, "%79s\r\n", " ");
273 if (fstat(fd, &sbuf))
274 err(1, "can't stat temporary file");
275 mbufsize = sbuf.st_size;
276 if (!(mbuf = malloc((u_int)mbufsize)))
277 err(1, "out of memory");
278 if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
279 err(1, "can't read temporary file");