xref: /csrg-svn/usr.bin/wall/wall.c (revision 64881)
11159Sbill /*
262435Sbostic  * Copyright (c) 1988, 1990, 1993
362435Sbostic  *	The Regents of the University of California.  All rights reserved.
435531Sbostic  *
545220Sbostic  * %sccs.include.redist.c%
619909Sdist  */
719909Sdist 
819909Sdist #ifndef lint
962435Sbostic static char copyright[] =
1062435Sbostic "@(#) Copyright (c) 1988, 1990, 1993\n\
1162435Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1235531Sbostic #endif /* not lint */
1319909Sdist 
1419909Sdist #ifndef lint
15*64881Sbostic static char sccsid[] = "@(#)wall.c	8.2 (Berkeley) 11/16/93";
1635531Sbostic #endif /* not lint */
1719909Sdist 
1819909Sdist /*
191159Sbill  * This program is not related to David Wall, whose Stanford Ph.D. thesis
201159Sbill  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
211159Sbill  */
221159Sbill 
2335531Sbostic #include <sys/param.h>
2439423Sbostic #include <sys/stat.h>
2535531Sbostic #include <sys/time.h>
2639423Sbostic #include <sys/uio.h>
27*64881Sbostic 
28*64881Sbostic #include <paths.h>
2935531Sbostic #include <pwd.h>
3035531Sbostic #include <stdio.h>
3146894Sbostic #include <stdlib.h>
32*64881Sbostic #include <string.h>
33*64881Sbostic #include <unistd.h>
34*64881Sbostic #include <utmp.h>
3516256Sralph 
36*64881Sbostic void	makemsg __P((char *));
37*64881Sbostic 
3835531Sbostic #define	IGNOREUSER	"sleeper"
391159Sbill 
4040223Skarels int nobanner;
4139089Smarc int mbufsize;
4239089Smarc char *mbuf;
431159Sbill 
4435531Sbostic /* ARGSUSED */
45*64881Sbostic int
main(argc,argv)461159Sbill main(argc, argv)
4735531Sbostic 	int argc;
4835531Sbostic 	char **argv;
491159Sbill {
5040223Skarels 	extern int optind;
5140223Skarels 	int ch;
5239423Sbostic 	struct iovec iov;
5335531Sbostic 	struct utmp utmp;
5435531Sbostic 	FILE *fp;
5539423Sbostic 	char *p, *ttymsg();
56*64881Sbostic 	char line[sizeof(utmp.ut_line) + 1];
571159Sbill 
5840223Skarels 	while ((ch = getopt(argc, argv, "n")) != EOF)
5940223Skarels 		switch (ch) {
6040223Skarels 		case 'n':
6140223Skarels 			/* undoc option for shutdown: suppress banner */
6240223Skarels 			if (geteuid() == 0)
6340223Skarels 				nobanner = 1;
6440223Skarels 			break;
6540223Skarels 		case '?':
6640223Skarels 		default:
6740223Skarels usage:
6840223Skarels 			(void)fprintf(stderr, "usage: wall [file]\n");
6940223Skarels 			exit(1);
7040223Skarels 		}
7140223Skarels 	argc -= optind;
7240223Skarels 	argv += optind;
7340223Skarels 	if (argc > 1)
7440223Skarels 		goto usage;
7535531Sbostic 
7640223Skarels 	makemsg(*argv);
7739423Sbostic 
7839089Smarc 	if (!(fp = fopen(_PATH_UTMP, "r"))) {
7939423Sbostic 		(void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP);
8035531Sbostic 		exit(1);
811159Sbill 	}
8239423Sbostic 	iov.iov_base = mbuf;
8339423Sbostic 	iov.iov_len = mbufsize;
8435531Sbostic 	/* NOSTRICT */
8539423Sbostic 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
8639423Sbostic 		if (!utmp.ut_name[0] ||
8739423Sbostic 		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
8839423Sbostic 			continue;
89*64881Sbostic 		strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
90*64881Sbostic 		line[sizeof(utmp.ut_line)] = '\0';
91*64881Sbostic 		if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
9239423Sbostic 			(void)fprintf(stderr, "wall: %s\n", p);
9339423Sbostic 	}
941159Sbill 	exit(0);
951159Sbill }
961159Sbill 
97*64881Sbostic void
makemsg(fname)9839423Sbostic makemsg(fname)
9939423Sbostic 	char *fname;
1001159Sbill {
10135531Sbostic 	register int ch, cnt;
10235531Sbostic 	struct tm *lt;
10346894Sbostic 	struct passwd *pw;
10435531Sbostic 	struct stat sbuf;
10535531Sbostic 	time_t now, time();
10635531Sbostic 	FILE *fp;
10735531Sbostic 	int fd;
10835531Sbostic 	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
10946894Sbostic 	char *getlogin(), *strcpy(), *ttyname();
1101159Sbill 
11139089Smarc 	(void)strcpy(tmpname, _PATH_TMP);
11239124Sbostic 	(void)strcat(tmpname, "/wall.XXXXXX");
11335531Sbostic 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
11439423Sbostic 		(void)fprintf(stderr, "wall: can't open temporary file.\n");
11535531Sbostic 		exit(1);
11635531Sbostic 	}
11735531Sbostic 	(void)unlink(tmpname);
11816256Sralph 
11940223Skarels 	if (!nobanner) {
12040223Skarels 		if (!(whom = getlogin()))
12140223Skarels 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
12240223Skarels 		(void)gethostname(hostname, sizeof(hostname));
12340223Skarels 		(void)time(&now);
12440223Skarels 		lt = localtime(&now);
12535531Sbostic 
12640223Skarels 		/*
12740223Skarels 		 * all this stuff is to blank out a square for the message;
12840223Skarels 		 * we wrap message lines at column 79, not 80, because some
12940223Skarels 		 * terminals wrap after 79, some do not, and we can't tell.
13040223Skarels 		 * Which means that we may leave a non-blank character
13140223Skarels 		 * in column 80, but that can't be helped.
13240223Skarels 		 */
13340223Skarels 		(void)fprintf(fp, "\r%79s\r\n", " ");
13440223Skarels 		(void)sprintf(lbuf, "Broadcast Message from %s@%s",
13540223Skarels 		    whom, hostname);
13640223Skarels 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
13740223Skarels 		(void)sprintf(lbuf, "        (%s) at %d:%02d ...", ttyname(2),
13840223Skarels 		    lt->tm_hour, lt->tm_min);
13940223Skarels 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
14040223Skarels 	}
14139423Sbostic 	(void)fprintf(fp, "%79s\r\n", " ");
14235531Sbostic 
14351439Storek 	if (fname && !(freopen(fname, "r", stdin))) {
14439423Sbostic 		(void)fprintf(stderr, "wall: can't read %s.\n", fname);
14535531Sbostic 		exit(1);
14616256Sralph 	}
14735531Sbostic 	while (fgets(lbuf, sizeof(lbuf), stdin))
148*64881Sbostic 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
14939089Smarc 			if (cnt == 79 || ch == '\n') {
15039423Sbostic 				for (; cnt < 79; ++cnt)
15139423Sbostic 					putc(' ', fp);
15235531Sbostic 				putc('\r', fp);
15335531Sbostic 				putc('\n', fp);
15439089Smarc 				cnt = 0;
15535531Sbostic 			} else
15635531Sbostic 				putc(ch, fp);
15739089Smarc 		}
15839423Sbostic 	(void)fprintf(fp, "%79s\r\n", " ");
15935531Sbostic 	rewind(fp);
16035531Sbostic 
16135531Sbostic 	if (fstat(fd, &sbuf)) {
16239423Sbostic 		(void)fprintf(stderr, "wall: can't stat temporary file.\n");
16335531Sbostic 		exit(1);
16416256Sralph 	}
16535531Sbostic 	mbufsize = sbuf.st_size;
16635531Sbostic 	if (!(mbuf = malloc((u_int)mbufsize))) {
16739423Sbostic 		(void)fprintf(stderr, "wall: out of memory.\n");
16835531Sbostic 		exit(1);
16916256Sralph 	}
17035776Sbostic 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
17139423Sbostic 		(void)fprintf(stderr, "wall: can't read temporary file.\n");
17235776Sbostic 		exit(1);
17335776Sbostic 	}
17435531Sbostic 	(void)close(fd);
17535531Sbostic }
176