xref: /netbsd/usr.bin/wall/wall.c (revision c897d730)
1*c897d730Schristos /*	$NetBSD: wall.c,v 1.8 1997/06/29 18:28:20 christos Exp $	*/
2a8bfd3d1Sjtc 
361f28255Scgd /*
4a8bfd3d1Sjtc  * Copyright (c) 1988, 1990, 1993
5a8bfd3d1Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1561f28255Scgd  * 3. All advertising materials mentioning features or use of this software
1661f28255Scgd  *    must display the following acknowledgement:
1761f28255Scgd  *	This product includes software developed by the University of
1861f28255Scgd  *	California, Berkeley and its contributors.
1961f28255Scgd  * 4. Neither the name of the University nor the names of its contributors
2061f28255Scgd  *    may be used to endorse or promote products derived from this software
2161f28255Scgd  *    without specific prior written permission.
2261f28255Scgd  *
2361f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2461f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2561f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2661f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2761f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2861f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2961f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3061f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3161f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3261f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3361f28255Scgd  * SUCH DAMAGE.
3461f28255Scgd  */
3561f28255Scgd 
36*c897d730Schristos #include <sys/cdefs.h>
3761f28255Scgd #ifndef lint
38*c897d730Schristos __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\n\
39*c897d730Schristos 	The Regents of the University of California.  All rights reserved.\n");
4061f28255Scgd #endif /* not lint */
4161f28255Scgd 
4261f28255Scgd #ifndef lint
43a8bfd3d1Sjtc #if 0
44a8bfd3d1Sjtc static char sccsid[] = "@(#)wall.c	8.2 (Berkeley) 11/16/93";
45a8bfd3d1Sjtc #endif
46*c897d730Schristos __RCSID("$NetBSD: wall.c,v 1.8 1997/06/29 18:28:20 christos Exp $");
4761f28255Scgd #endif /* not lint */
4861f28255Scgd 
4961f28255Scgd /*
5061f28255Scgd  * This program is not related to David Wall, whose Stanford Ph.D. thesis
5161f28255Scgd  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
5261f28255Scgd  */
5361f28255Scgd 
5461f28255Scgd #include <sys/param.h>
5561f28255Scgd #include <sys/stat.h>
5661f28255Scgd #include <sys/time.h>
5761f28255Scgd #include <sys/uio.h>
58a8bfd3d1Sjtc 
59a8bfd3d1Sjtc #include <paths.h>
6061f28255Scgd #include <pwd.h>
6161f28255Scgd #include <stdio.h>
6261f28255Scgd #include <stdlib.h>
63f7c6bf57Sjtc #include <string.h>
64f7c6bf57Sjtc #include <unistd.h>
65a8bfd3d1Sjtc #include <utmp.h>
66*c897d730Schristos #include <util.h>
67a8bfd3d1Sjtc 
68a8bfd3d1Sjtc void	makemsg __P((char *));
69*c897d730Schristos int	main __P((int, char *[]));
7061f28255Scgd 
7161f28255Scgd #define	IGNOREUSER	"sleeper"
7261f28255Scgd 
7361f28255Scgd int nobanner;
7461f28255Scgd int mbufsize;
7561f28255Scgd char *mbuf;
7661f28255Scgd 
7761f28255Scgd /* ARGSUSED */
78f7c6bf57Sjtc int
7961f28255Scgd main(argc, argv)
8061f28255Scgd 	int argc;
8161f28255Scgd 	char **argv;
8261f28255Scgd {
8361f28255Scgd 	extern int optind;
8461f28255Scgd 	int ch;
8561f28255Scgd 	struct iovec iov;
8661f28255Scgd 	struct utmp utmp;
8761f28255Scgd 	FILE *fp;
88*c897d730Schristos 	char *p;
89b9b17c30Scgd 	struct passwd *pep = getpwnam("nobody");
90a8bfd3d1Sjtc 	char line[sizeof(utmp.ut_line) + 1];
9161f28255Scgd 
9261f28255Scgd 	while ((ch = getopt(argc, argv, "n")) != EOF)
9361f28255Scgd 		switch (ch) {
9461f28255Scgd 		case 'n':
9561f28255Scgd 			/* undoc option for shutdown: suppress banner */
96b9b17c30Scgd 			if (geteuid() == 0 || (pep && getuid() == pep->pw_uid))
9761f28255Scgd 				nobanner = 1;
9861f28255Scgd 			break;
9961f28255Scgd 		case '?':
10061f28255Scgd 		default:
10161f28255Scgd usage:
10261f28255Scgd 			(void)fprintf(stderr, "usage: wall [file]\n");
10361f28255Scgd 			exit(1);
10461f28255Scgd 		}
10561f28255Scgd 	argc -= optind;
10661f28255Scgd 	argv += optind;
10761f28255Scgd 	if (argc > 1)
10861f28255Scgd 		goto usage;
10961f28255Scgd 
11061f28255Scgd 	makemsg(*argv);
11161f28255Scgd 
11261f28255Scgd 	if (!(fp = fopen(_PATH_UTMP, "r"))) {
11361f28255Scgd 		(void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP);
11461f28255Scgd 		exit(1);
11561f28255Scgd 	}
11661f28255Scgd 	iov.iov_base = mbuf;
11761f28255Scgd 	iov.iov_len = mbufsize;
11861f28255Scgd 	/* NOSTRICT */
11961f28255Scgd 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
12061f28255Scgd 		if (!utmp.ut_name[0] ||
12161f28255Scgd 		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
12261f28255Scgd 			continue;
123a8bfd3d1Sjtc 		strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
124a8bfd3d1Sjtc 		line[sizeof(utmp.ut_line)] = '\0';
125a8bfd3d1Sjtc 		if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
12661f28255Scgd 			(void)fprintf(stderr, "wall: %s\n", p);
12761f28255Scgd 	}
12861f28255Scgd 	exit(0);
12961f28255Scgd }
13061f28255Scgd 
131f7c6bf57Sjtc void
13261f28255Scgd makemsg(fname)
13361f28255Scgd 	char *fname;
13461f28255Scgd {
13561f28255Scgd 	register int ch, cnt;
13661f28255Scgd 	struct tm *lt;
13761f28255Scgd 	struct passwd *pw;
13861f28255Scgd 	struct stat sbuf;
139*c897d730Schristos 	time_t now;
14061f28255Scgd 	FILE *fp;
14161f28255Scgd 	int fd;
14261f28255Scgd 	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
14361f28255Scgd 
14423768a9eSmrg 	(void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP);
14561f28255Scgd 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
14661f28255Scgd 		(void)fprintf(stderr, "wall: can't open temporary file.\n");
14761f28255Scgd 		exit(1);
14861f28255Scgd 	}
14961f28255Scgd 	(void)unlink(tmpname);
15061f28255Scgd 
15161f28255Scgd 	if (!nobanner) {
15261f28255Scgd 		if (!(whom = getlogin()))
15361f28255Scgd 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
15461f28255Scgd 		(void)gethostname(hostname, sizeof(hostname));
15561f28255Scgd 		(void)time(&now);
15661f28255Scgd 		lt = localtime(&now);
15761f28255Scgd 
15861f28255Scgd 		/*
15961f28255Scgd 		 * all this stuff is to blank out a square for the message;
16061f28255Scgd 		 * we wrap message lines at column 79, not 80, because some
16161f28255Scgd 		 * terminals wrap after 79, some do not, and we can't tell.
16261f28255Scgd 		 * Which means that we may leave a non-blank character
16361f28255Scgd 		 * in column 80, but that can't be helped.
16461f28255Scgd 		 */
16561f28255Scgd 		(void)fprintf(fp, "\r%79s\r\n", " ");
16623768a9eSmrg 		(void)snprintf(lbuf, sizeof lbuf,
16723768a9eSmrg 		    "Broadcast Message from %s@%s", whom, hostname);
16861f28255Scgd 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
16923768a9eSmrg 		(void)snprintf(lbuf, sizeof lbuf, "        (%s) at %d:%02d ...",
17023768a9eSmrg 		    ttyname(2), lt->tm_hour, lt->tm_min);
17161f28255Scgd 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
17261f28255Scgd 	}
17361f28255Scgd 	(void)fprintf(fp, "%79s\r\n", " ");
17461f28255Scgd 
175f90eda1cSmycroft 	if (fname && !(freopen(fname, "r", stdin))) {
17661f28255Scgd 		(void)fprintf(stderr, "wall: can't read %s.\n", fname);
17761f28255Scgd 		exit(1);
17861f28255Scgd 	}
17961f28255Scgd 	while (fgets(lbuf, sizeof(lbuf), stdin))
180a8bfd3d1Sjtc 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
18161f28255Scgd 			if (cnt == 79 || ch == '\n') {
18261f28255Scgd 				for (; cnt < 79; ++cnt)
18361f28255Scgd 					putc(' ', fp);
18461f28255Scgd 				putc('\r', fp);
18561f28255Scgd 				putc('\n', fp);
186f90eda1cSmycroft 				cnt = -1;
187f90eda1cSmycroft 			}
188f90eda1cSmycroft 			if (ch != '\n')
18961f28255Scgd 				putc(ch, fp);
19061f28255Scgd 		}
19161f28255Scgd 	(void)fprintf(fp, "%79s\r\n", " ");
19261f28255Scgd 	rewind(fp);
19361f28255Scgd 
19461f28255Scgd 	if (fstat(fd, &sbuf)) {
19561f28255Scgd 		(void)fprintf(stderr, "wall: can't stat temporary file.\n");
19661f28255Scgd 		exit(1);
19761f28255Scgd 	}
19861f28255Scgd 	mbufsize = sbuf.st_size;
19961f28255Scgd 	if (!(mbuf = malloc((u_int)mbufsize))) {
20061f28255Scgd 		(void)fprintf(stderr, "wall: out of memory.\n");
20161f28255Scgd 		exit(1);
20261f28255Scgd 	}
20361f28255Scgd 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
20461f28255Scgd 		(void)fprintf(stderr, "wall: can't read temporary file.\n");
20561f28255Scgd 		exit(1);
20661f28255Scgd 	}
20761f28255Scgd 	(void)close(fd);
20861f28255Scgd }
209