xref: /csrg-svn/usr.bin/wall/wall.c (revision 50740)
11159Sbill /*
240223Skarels  * Copyright (c) 1988, 1990 Regents of the University of California.
335531Sbostic  * All rights reserved.
435531Sbostic  *
545220Sbostic  * %sccs.include.redist.c%
619909Sdist  */
719909Sdist 
819909Sdist #ifndef lint
919909Sdist char copyright[] =
1035531Sbostic "@(#) Copyright (c) 1988 Regents of the University of California.\n\
1119909Sdist  All rights reserved.\n";
1235531Sbostic #endif /* not lint */
1319909Sdist 
1419909Sdist #ifndef lint
15*50740Smarc static char sccsid[] = "@(#)wall.c	5.15 (Berkeley) 08/01/91";
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>
271159Sbill #include <utmp.h>
2835531Sbostic #include <pwd.h>
2935531Sbostic #include <stdio.h>
3046894Sbostic #include <stdlib.h>
3139124Sbostic #include <paths.h>
3216256Sralph 
3335531Sbostic #define	IGNOREUSER	"sleeper"
341159Sbill 
3540223Skarels int nobanner;
3639089Smarc int mbufsize;
3739089Smarc char *mbuf;
381159Sbill 
3935531Sbostic /* ARGSUSED */
401159Sbill main(argc, argv)
4135531Sbostic 	int argc;
4235531Sbostic 	char **argv;
431159Sbill {
4440223Skarels 	extern int optind;
4540223Skarels 	int ch;
4639423Sbostic 	struct iovec iov;
4735531Sbostic 	struct utmp utmp;
4835531Sbostic 	FILE *fp;
4939423Sbostic 	char *p, *ttymsg();
501159Sbill 
5140223Skarels 	while ((ch = getopt(argc, argv, "n")) != EOF)
5240223Skarels 		switch (ch) {
5340223Skarels 		case 'n':
5440223Skarels 			/* undoc option for shutdown: suppress banner */
5540223Skarels 			if (geteuid() == 0)
5640223Skarels 				nobanner = 1;
5740223Skarels 			break;
5840223Skarels 		case '?':
5940223Skarels 		default:
6040223Skarels usage:
6140223Skarels 			(void)fprintf(stderr, "usage: wall [file]\n");
6240223Skarels 			exit(1);
6340223Skarels 		}
6440223Skarels 	argc -= optind;
6540223Skarels 	argv += optind;
6640223Skarels 	if (argc > 1)
6740223Skarels 		goto usage;
6835531Sbostic 
6940223Skarels 	makemsg(*argv);
7039423Sbostic 
7139089Smarc 	if (!(fp = fopen(_PATH_UTMP, "r"))) {
7239423Sbostic 		(void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP);
7335531Sbostic 		exit(1);
741159Sbill 	}
7539423Sbostic 	iov.iov_base = mbuf;
7639423Sbostic 	iov.iov_len = mbufsize;
7735531Sbostic 	/* NOSTRICT */
7839423Sbostic 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
7939423Sbostic 		if (!utmp.ut_name[0] ||
8039423Sbostic 		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
8139423Sbostic 			continue;
82*50740Smarc 		if (p = ttymsg(&iov, 1, utmp.ut_line, 60*5))
8339423Sbostic 			(void)fprintf(stderr, "wall: %s\n", p);
8439423Sbostic 	}
851159Sbill 	exit(0);
861159Sbill }
871159Sbill 
8839423Sbostic makemsg(fname)
8939423Sbostic 	char *fname;
901159Sbill {
9135531Sbostic 	register int ch, cnt;
9235531Sbostic 	struct tm *lt;
9346894Sbostic 	struct passwd *pw;
9435531Sbostic 	struct stat sbuf;
9535531Sbostic 	time_t now, time();
9635531Sbostic 	FILE *fp;
9735531Sbostic 	int fd;
9835531Sbostic 	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
9946894Sbostic 	char *getlogin(), *strcpy(), *ttyname();
1001159Sbill 
10139089Smarc 	(void)strcpy(tmpname, _PATH_TMP);
10239124Sbostic 	(void)strcat(tmpname, "/wall.XXXXXX");
10335531Sbostic 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
10439423Sbostic 		(void)fprintf(stderr, "wall: can't open temporary file.\n");
10535531Sbostic 		exit(1);
10635531Sbostic 	}
10735531Sbostic 	(void)unlink(tmpname);
10816256Sralph 
10940223Skarels 	if (!nobanner) {
11040223Skarels 		if (!(whom = getlogin()))
11140223Skarels 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
11240223Skarels 		(void)gethostname(hostname, sizeof(hostname));
11340223Skarels 		(void)time(&now);
11440223Skarels 		lt = localtime(&now);
11535531Sbostic 
11640223Skarels 		/*
11740223Skarels 		 * all this stuff is to blank out a square for the message;
11840223Skarels 		 * we wrap message lines at column 79, not 80, because some
11940223Skarels 		 * terminals wrap after 79, some do not, and we can't tell.
12040223Skarels 		 * Which means that we may leave a non-blank character
12140223Skarels 		 * in column 80, but that can't be helped.
12240223Skarels 		 */
12340223Skarels 		(void)fprintf(fp, "\r%79s\r\n", " ");
12440223Skarels 		(void)sprintf(lbuf, "Broadcast Message from %s@%s",
12540223Skarels 		    whom, hostname);
12640223Skarels 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
12740223Skarels 		(void)sprintf(lbuf, "        (%s) at %d:%02d ...", ttyname(2),
12840223Skarels 		    lt->tm_hour, lt->tm_min);
12940223Skarels 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
13040223Skarels 	}
13139423Sbostic 	(void)fprintf(fp, "%79s\r\n", " ");
13235531Sbostic 
13339423Sbostic 	if (*fname && !(freopen(fname, "r", stdin))) {
13439423Sbostic 		(void)fprintf(stderr, "wall: can't read %s.\n", fname);
13535531Sbostic 		exit(1);
13616256Sralph 	}
13735531Sbostic 	while (fgets(lbuf, sizeof(lbuf), stdin))
13839089Smarc 		for (cnt = 0, p = lbuf; ch = *p; ++p, ++cnt) {
13939089Smarc 			if (cnt == 79 || ch == '\n') {
14039423Sbostic 				for (; cnt < 79; ++cnt)
14139423Sbostic 					putc(' ', fp);
14235531Sbostic 				putc('\r', fp);
14335531Sbostic 				putc('\n', fp);
14439089Smarc 				cnt = 0;
14535531Sbostic 			} else
14635531Sbostic 				putc(ch, fp);
14739089Smarc 		}
14839423Sbostic 	(void)fprintf(fp, "%79s\r\n", " ");
14935531Sbostic 	rewind(fp);
15035531Sbostic 
15135531Sbostic 	if (fstat(fd, &sbuf)) {
15239423Sbostic 		(void)fprintf(stderr, "wall: can't stat temporary file.\n");
15335531Sbostic 		exit(1);
15416256Sralph 	}
15535531Sbostic 	mbufsize = sbuf.st_size;
15635531Sbostic 	if (!(mbuf = malloc((u_int)mbufsize))) {
15739423Sbostic 		(void)fprintf(stderr, "wall: out of memory.\n");
15835531Sbostic 		exit(1);
15916256Sralph 	}
16035776Sbostic 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
16139423Sbostic 		(void)fprintf(stderr, "wall: can't read temporary file.\n");
16235776Sbostic 		exit(1);
16335776Sbostic 	}
16435531Sbostic 	(void)close(fd);
16535531Sbostic }
166