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>
70 static void makemsg(char *);
71 static void usage(void);
73 static struct wallgroup
{
74 struct wallgroup
*next
;
86 char ttybuf
[MAXPATHLEN
];
88 (void)snprintf(ttybuf
, sizeof(ttybuf
), "%s%s", _PATH_DEV
, line
);
89 if (stat(ttybuf
, &sb
) == 0) {
96 main(int argc
, char *argv
[])
108 (void)setlocale(LC_CTYPE
, "");
110 while ((ch
= getopt(argc
, argv
, "g:n")) != -1)
113 /* undoc option for shutdown: suppress banner */
118 g
= (struct wallgroup
*)malloc(sizeof *g
);
133 for (g
= grouplist
; g
; g
= g
->next
) {
134 grp
= getgrnam(g
->name
);
136 g
->gid
= grp
->gr_gid
;
138 warnx("%s: no such group", g
->name
);
144 iov
.iov_len
= mbufsize
;
146 while ((utmp
= getutxent()) != NULL
) {
147 if (utmp
->ut_type
!= USER_PROCESS
)
149 if (ttystat(utmp
->ut_line
) != 0)
153 pw
= getpwnam(utmp
->ut_user
);
156 for (g
= grouplist
; g
&& ingroup
== 0; g
= g
->next
) {
157 if (g
->gid
== (gid_t
)-1)
159 if (g
->gid
== pw
->pw_gid
)
161 else if ((grp
= getgrgid(g
->gid
)) != NULL
) {
162 for (np
= grp
->gr_mem
; *np
; np
++) {
163 if (strcmp(*np
, utmp
->ut_user
) == 0) {
173 if ((p
= ttymsg(&iov
, 1, utmp
->ut_line
, 60*5)) != NULL
)
182 (void)fprintf(stderr
, "usage: wall [-g group] [file]\n");
197 char hostname
[MAXHOSTNAMELEN
], tmpname
[64];
198 wchar_t *p
, *tmp
, lbuf
[256], codebuf
[13];
203 (void)snprintf(tmpname
, sizeof(tmpname
), "%s/wall.XXXXXX", _PATH_TMP
);
204 if ((fd
= mkstemp(tmpname
)) == -1 || !(fp
= fdopen(fd
, "r+")))
205 err(1, "can't open temporary file");
206 (void)unlink(tmpname
);
209 tty
= ttyname(STDERR_FILENO
);
213 if (!(whom
= getlogin()))
214 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
215 (void)gethostname(hostname
, sizeof(hostname
));
217 lt
= localtime(&now
);
220 * all this stuff is to blank out a square for the message;
221 * we wrap message lines at column 79, not 80, because some
222 * terminals wrap after 79, some do not, and we can't tell.
223 * Which means that we may leave a non-blank character
224 * in column 80, but that can't be helped.
226 (void)fwprintf(fp
, L
"\r%79s\r\n", " ");
227 (void)swprintf(lbuf
, sizeof(lbuf
)/sizeof(wchar_t),
228 L
"Broadcast Message from %s@%s",
230 (void)fwprintf(fp
, L
"%-79.79S\007\007\r\n", lbuf
);
231 (void)swprintf(lbuf
, sizeof(lbuf
)/sizeof(wchar_t),
232 L
" (%s) at %d:%02d %s...", tty
,
233 lt
->tm_hour
, lt
->tm_min
, lt
->tm_zone
);
234 (void)fwprintf(fp
, L
"%-79.79S\r\n", lbuf
);
236 (void)fwprintf(fp
, L
"%79s\r\n", " ");
241 if (freopen(fname
, "r", stdin
) == NULL
)
242 err(1, "can't read %s", fname
);
243 if (setegid(egid
) != 0)
244 err(1, "setegid failed");
247 while (fgetws(lbuf
, sizeof(lbuf
)/sizeof(wchar_t), stdin
)) {
248 for (p
= lbuf
; (ch
= *p
) != L
'\0'; ++p
, ++cnt
) {
253 } else if (ch
== L
'\n') {
254 for (; cnt
< 79; ++cnt
)
265 if (iswprint(ch
) || iswspace(ch
) || ch
== L
'\a' || ch
== L
'\b') {
268 (void)swprintf(codebuf
, sizeof(codebuf
)/sizeof(wchar_t), L
"<0x%X>", ch
);
269 for (tmp
= codebuf
; *tmp
!= L
'\0'; ++tmp
) {
281 (void)fwprintf(fp
, L
"%79s\r\n", " ");
284 if (fstat(fd
, &sbuf
))
285 err(1, "can't stat temporary file");
286 mbufsize
= sbuf
.st_size
;
287 if (!(mbuf
= malloc((u_int
)mbufsize
)))
288 err(1, "out of memory");
289 if ((int)fread(mbuf
, sizeof(*mbuf
), mbufsize
, fp
) != mbufsize
)
290 err(1, "can't read temporary file");