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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1990, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93";
45 * This program is not related to David Wall, whose Stanford Ph.D. thesis
46 * is entitled "Mechanisms for Broadcast and Selective Broadcast".
49 #include <sys/param.h>
68 static void makemsg(char *);
69 static void usage(void);
72 struct wallgroup *next;
84 char ttybuf[MAXPATHLEN];
86 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
87 if (stat(ttybuf, &sb) == 0) {
94 main(int argc, char *argv[])
106 (void)setlocale(LC_CTYPE, "");
108 while ((ch = getopt(argc, argv, "g:n")) != -1)
111 /* undoc option for shutdown: suppress banner */
116 g = (struct wallgroup *)malloc(sizeof *g);
131 for (g = grouplist; g; g = g->next) {
132 grp = getgrnam(g->name);
134 g->gid = grp->gr_gid;
136 warnx("%s: no such group", g->name);
142 iov.iov_len = mbufsize;
144 while ((utmp = getutxent()) != NULL) {
145 if (utmp->ut_type != USER_PROCESS)
147 if (ttystat(utmp->ut_line) != 0)
151 pw = getpwnam(utmp->ut_user);
154 for (g = grouplist; g && ingroup == 0; g = g->next) {
155 if (g->gid == (gid_t)-1)
157 if (g->gid == pw->pw_gid)
159 else if ((grp = getgrgid(g->gid)) != NULL) {
160 for (np = grp->gr_mem; *np; np++) {
161 if (strcmp(*np, utmp->ut_user) == 0) {
171 if ((p = ttymsg(&iov, 1, utmp->ut_line, 60*5)) != NULL)
180 (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
195 char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
200 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
201 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
202 err(1, "can't open temporary file");
203 (void)unlink(tmpname);
206 tty = ttyname(STDERR_FILENO);
210 if (!(whom = getlogin()))
211 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
212 (void)gethostname(hostname, sizeof(hostname));
214 lt = localtime(&now);
217 * all this stuff is to blank out a square for the message;
218 * we wrap message lines at column 79, not 80, because some
219 * terminals wrap after 79, some do not, and we can't tell.
220 * Which means that we may leave a non-blank character
221 * in column 80, but that can't be helped.
223 (void)fprintf(fp, "\r%79s\r\n", " ");
224 (void)snprintf(lbuf, sizeof(lbuf),
225 "Broadcast Message from %s@%s",
227 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
228 (void)snprintf(lbuf, sizeof(lbuf),
229 " (%s) at %d:%02d %s...", tty,
230 lt->tm_hour, lt->tm_min, lt->tm_zone);
231 (void)fprintf(fp, "%-79.79s\r\n", lbuf);
233 (void)fprintf(fp, "%79s\r\n", " ");
238 if (freopen(fname, "r", stdin) == NULL)
239 err(1, "can't read %s", fname);
243 while (fgets(lbuf, sizeof(lbuf), stdin)) {
244 for (p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
249 } else if (ch == '\n') {
250 for (; cnt < 79; ++cnt)
261 if (((ch & 0x80) && ch < 0xA0) ||
262 /* disable upper controls */
263 (!isprint(ch) && !isspace(ch) &&
264 ch != '\a' && ch != '\b')
294 (void)fprintf(fp, "%79s\r\n", " ");
297 if (fstat(fd, &sbuf))
298 err(1, "can't stat temporary file");
299 mbufsize = sbuf.st_size;
300 if (!(mbuf = malloc((u_int)mbufsize)))
301 err(1, "out of memory");
302 if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
303 err(1, "can't read temporary file");