indent(1): Use NULL instead of zero for pointers.
[freebsd-src.git] / usr.bin / wall / wall.c
blobb3a63dcbf20ac154ceec6298c5edef6385ef302c
1 /*
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
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 * 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
27 * SUCH DAMAGE.
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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
40 #ifndef lint
41 static const char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93";
42 #endif
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>
50 #include <sys/stat.h>
51 #include <sys/uio.h>
53 #include <ctype.h>
54 #include <err.h>
55 #include <grp.h>
56 #include <locale.h>
57 #include <paths.h>
58 #include <pwd.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <time.h>
63 #include <unistd.h>
64 #include <utmpx.h>
65 #include <wchar.h>
66 #include <wctype.h>
68 #include "ttymsg.h"
70 static void makemsg(char *);
71 static void usage(void);
73 static struct wallgroup {
74 struct wallgroup *next;
75 char *name;
76 gid_t gid;
77 } *grouplist;
78 static int nobanner;
79 static int mbufsize;
80 static char *mbuf;
82 static int
83 ttystat(char *line)
85 struct stat sb;
86 char ttybuf[MAXPATHLEN];
88 (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
89 if (stat(ttybuf, &sb) == 0) {
90 return (0);
91 } else
92 return (-1);
95 int
96 main(int argc, char *argv[])
98 struct iovec iov;
99 struct utmpx *utmp;
100 int ch;
101 int ingroup;
102 struct wallgroup *g;
103 struct group *grp;
104 char **np;
105 const char *p;
106 struct passwd *pw;
108 (void)setlocale(LC_CTYPE, "");
110 while ((ch = getopt(argc, argv, "g:n")) != -1)
111 switch (ch) {
112 case 'n':
113 /* undoc option for shutdown: suppress banner */
114 if (geteuid() == 0)
115 nobanner = 1;
116 break;
117 case 'g':
118 g = (struct wallgroup *)malloc(sizeof *g);
119 g->next = grouplist;
120 g->name = optarg;
121 g->gid = -1;
122 grouplist = g;
123 break;
124 case '?':
125 default:
126 usage();
128 argc -= optind;
129 argv += optind;
130 if (argc > 1)
131 usage();
133 for (g = grouplist; g; g = g->next) {
134 grp = getgrnam(g->name);
135 if (grp != NULL)
136 g->gid = grp->gr_gid;
137 else
138 warnx("%s: no such group", g->name);
141 makemsg(*argv);
143 iov.iov_base = mbuf;
144 iov.iov_len = mbufsize;
145 /* NOSTRICT */
146 while ((utmp = getutxent()) != NULL) {
147 if (utmp->ut_type != USER_PROCESS)
148 continue;
149 if (ttystat(utmp->ut_line) != 0)
150 continue;
151 if (grouplist) {
152 ingroup = 0;
153 pw = getpwnam(utmp->ut_user);
154 if (!pw)
155 continue;
156 for (g = grouplist; g && ingroup == 0; g = g->next) {
157 if (g->gid == (gid_t)-1)
158 continue;
159 if (g->gid == pw->pw_gid)
160 ingroup = 1;
161 else if ((grp = getgrgid(g->gid)) != NULL) {
162 for (np = grp->gr_mem; *np; np++) {
163 if (strcmp(*np, utmp->ut_user) == 0) {
164 ingroup = 1;
165 break;
170 if (ingroup == 0)
171 continue;
173 if ((p = ttymsg(&iov, 1, utmp->ut_line, 60*5)) != NULL)
174 warnx("%s", p);
176 exit(0);
179 static void
180 usage(void)
182 (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
183 exit(1);
186 void
187 makemsg(char *fname)
189 int cnt;
190 wchar_t ch;
191 struct tm *lt;
192 struct passwd *pw;
193 struct stat sbuf;
194 time_t now;
195 FILE *fp;
196 int fd;
197 char hostname[MAXHOSTNAMELEN], tmpname[64];
198 wchar_t *p, *tmp, lbuf[256], codebuf[13];
199 const char *tty;
200 const char *whom;
201 gid_t egid;
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);
208 if (!nobanner) {
209 tty = ttyname(STDERR_FILENO);
210 if (tty == NULL)
211 tty = "no tty";
213 if (!(whom = getlogin()))
214 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
215 (void)gethostname(hostname, sizeof(hostname));
216 (void)time(&now);
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",
229 whom, hostname);
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", " ");
238 if (fname) {
239 egid = getegid();
240 setegid(getgid());
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");
246 cnt = 0;
247 while (fgetws(lbuf, sizeof(lbuf)/sizeof(wchar_t), stdin)) {
248 for (p = lbuf; (ch = *p) != L'\0'; ++p, ++cnt) {
249 if (ch == L'\r') {
250 putwc(L'\r', fp);
251 cnt = 0;
252 continue;
253 } else if (ch == L'\n') {
254 for (; cnt < 79; ++cnt)
255 putwc(L' ', fp);
256 putwc(L'\r', fp);
257 putwc(L'\n', fp);
258 break;
260 if (cnt == 79) {
261 putwc(L'\r', fp);
262 putwc(L'\n', fp);
263 cnt = 0;
265 if (iswprint(ch) || iswspace(ch) || ch == L'\a' || ch == L'\b') {
266 putwc(ch, fp);
267 } else {
268 (void)swprintf(codebuf, sizeof(codebuf)/sizeof(wchar_t), L"<0x%X>", ch);
269 for (tmp = codebuf; *tmp != L'\0'; ++tmp) {
270 putwc(*tmp, fp);
271 if (++cnt == 79) {
272 putwc(L'\r', fp);
273 putwc(L'\n', fp);
274 cnt = 0;
277 --cnt;
281 (void)fwprintf(fp, L"%79s\r\n", " ");
282 rewind(fp);
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");
291 (void)close(fd);