kernel - Remove msf buffer code
[dragonfly.git] / usr.bin / wall / wall.c
blobc5733e640920182630111ab849f61c42cde44585
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 * 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
31 * SUCH DAMAGE.
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>
45 #include <sys/stat.h>
46 #include <sys/uio.h>
48 #include <ctype.h>
49 #include <err.h>
50 #include <grp.h>
51 #include <locale.h>
52 #include <paths.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <unistd.h>
59 #include <utmp.h>
61 #include "ttymsg.h"
63 static void makemsg(char *);
64 static void usage(void);
66 struct wallgroup {
67 struct wallgroup *next;
68 char *name;
69 gid_t gid;
70 } *grouplist;
71 int nobanner;
72 int mbufsize;
73 char *mbuf;
75 int
76 main(int argc, char *argv[])
78 struct iovec iov;
79 struct utmp utmp;
80 int ch;
81 int ingroup;
82 FILE *fp;
83 struct wallgroup *g;
84 struct group *grp;
85 char **np;
86 const char *p;
87 struct passwd *pw;
88 char line[sizeof(utmp.ut_line) + 1];
89 char username[sizeof(utmp.ut_name) + 1];
91 (void)setlocale(LC_CTYPE, "");
93 while ((ch = getopt(argc, argv, "g:n")) != -1)
94 switch (ch) {
95 case 'n':
96 /* undoc option for shutdown: suppress banner */
97 if (geteuid() == 0)
98 nobanner = 1;
99 break;
100 case 'g':
101 g = (struct wallgroup *)malloc(sizeof *g);
102 g->next = grouplist;
103 g->name = optarg;
104 g->gid = -1;
105 grouplist = g;
106 break;
107 case '?':
108 default:
109 usage();
111 argc -= optind;
112 argv += optind;
113 if (argc > 1)
114 usage();
116 for (g = grouplist; g; g = g->next) {
117 grp = getgrnam(g->name);
118 if (grp != NULL)
119 g->gid = grp->gr_gid;
120 else
121 warnx("%s: no such group", g->name);
124 makemsg(*argv);
126 if (!(fp = fopen(_PATH_UTMP, "r")))
127 err(1, "cannot read %s", _PATH_UTMP);
128 iov.iov_base = mbuf;
129 iov.iov_len = mbufsize;
130 /* NOSTRICT */
131 while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
132 if (!utmp.ut_name[0])
133 continue;
134 if (grouplist) {
135 ingroup = 0;
136 strlcpy(username, utmp.ut_name, sizeof(utmp.ut_name));
137 pw = getpwnam(username);
138 if (!pw)
139 continue;
140 for (g = grouplist; g && ingroup == 0; g = g->next) {
141 if (g->gid == (gid_t)-1)
142 continue;
143 if (g->gid == pw->pw_gid)
144 ingroup = 1;
145 else if ((grp = getgrgid(g->gid)) != NULL) {
146 for (np = grp->gr_mem; *np; np++) {
147 if (strcmp(*np, username) == 0) {
148 ingroup = 1;
149 break;
154 if (ingroup == 0)
155 continue;
157 strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
158 line[sizeof(utmp.ut_line)] = '\0';
159 if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
160 warnx("%s", p);
162 exit(0);
165 static void
166 usage(void)
168 (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
169 exit(1);
172 void
173 makemsg(char *fname)
175 int cnt;
176 unsigned char ch;
177 struct tm *lt;
178 struct passwd *pw;
179 struct stat sbuf;
180 time_t now;
181 FILE *fp;
182 int fd;
183 char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[MAXPATHLEN];
184 const char *tty;
185 const char *whom;
186 gid_t egid;
188 (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
189 if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
190 err(1, "can't open temporary file");
191 (void)unlink(tmpname);
193 if (!nobanner) {
194 tty = ttyname(STDERR_FILENO);
195 if (tty == NULL)
196 tty = "no tty";
198 if (!(whom = getlogin()))
199 whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
200 (void)gethostname(hostname, sizeof(hostname));
201 (void)time(&now);
202 lt = localtime(&now);
205 * all this stuff is to blank out a square for the message;
206 * we wrap message lines at column 79, not 80, because some
207 * terminals wrap after 79, some do not, and we can't tell.
208 * Which means that we may leave a non-blank character
209 * in column 80, but that can't be helped.
211 (void)fprintf(fp, "\r%79s\r\n", " ");
212 (void)snprintf(lbuf, sizeof(lbuf),
213 "Broadcast Message from %s@%s",
214 whom, hostname);
215 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
216 (void)snprintf(lbuf, sizeof(lbuf),
217 " (%s) at %d:%02d %s...", tty,
218 lt->tm_hour, lt->tm_min, lt->tm_zone);
219 (void)fprintf(fp, "%-79.79s\r\n", lbuf);
221 (void)fprintf(fp, "%79s\r\n", " ");
223 if (fname) {
224 egid = getegid();
225 setegid(getgid());
226 if (freopen(fname, "r", stdin) == NULL)
227 err(1, "can't read %s", fname);
228 setegid(egid);
230 while (fgets(lbuf, sizeof(lbuf), stdin))
231 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
232 if (ch == '\r') {
233 cnt = 0;
234 } else if (cnt == 79 || ch == '\n') {
235 for (; cnt < 79; ++cnt)
236 putc(' ', fp);
237 putc('\r', fp);
238 putc('\n', fp);
239 cnt = 0;
240 } else if (((ch & 0x80) && ch < 0xA0) ||
241 /* disable upper controls */
242 (!isprint(ch) && !isspace(ch) &&
243 ch != '\a' && ch != '\b')
245 if (ch & 0x80) {
246 ch &= 0x7F;
247 putc('M', fp);
248 if (++cnt == 79) {
249 putc('\r', fp);
250 putc('\n', fp);
251 cnt = 0;
253 putc('-', fp);
254 if (++cnt == 79) {
255 putc('\r', fp);
256 putc('\n', fp);
257 cnt = 0;
260 if (iscntrl(ch)) {
261 ch ^= 040;
262 putc('^', fp);
263 if (++cnt == 79) {
264 putc('\r', fp);
265 putc('\n', fp);
266 cnt = 0;
269 putc(ch, fp);
270 } else {
271 putc(ch, fp);
274 (void)fprintf(fp, "%79s\r\n", " ");
275 rewind(fp);
277 if (fstat(fd, &sbuf))
278 err(1, "can't stat temporary file");
279 mbufsize = sbuf.st_size;
280 if (!(mbuf = malloc((u_int)mbufsize)))
281 err(1, "out of memory");
282 if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
283 err(1, "can't read temporary file");
284 (void)close(fd);