xref: /csrg-svn/usr.bin/wall/wall.c (revision 40223)
11159Sbill /*
2*40223Skarels  * Copyright (c) 1988, 1990 Regents of the University of California.
335531Sbostic  * All rights reserved.
435531Sbostic  *
535531Sbostic  * Redistribution and use in source and binary forms are permitted
635531Sbostic  * provided that the above copyright notice and this paragraph are
735531Sbostic  * duplicated in all such forms and that any documentation,
835531Sbostic  * advertising materials, and other materials related to such
935531Sbostic  * distribution and use acknowledge that the software was developed
1035531Sbostic  * by the University of California, Berkeley.  The name of the
1135531Sbostic  * University may not be used to endorse or promote products derived
1235531Sbostic  * from this software without specific prior written permission.
1335531Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435531Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535531Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1619909Sdist  */
1719909Sdist 
1819909Sdist #ifndef lint
1919909Sdist char copyright[] =
2035531Sbostic "@(#) Copyright (c) 1988 Regents of the University of California.\n\
2119909Sdist  All rights reserved.\n";
2235531Sbostic #endif /* not lint */
2319909Sdist 
2419909Sdist #ifndef lint
25*40223Skarels static char sccsid[] = "@(#)wall.c	5.11 (Berkeley) 02/24/90";
2635531Sbostic #endif /* not lint */
2719909Sdist 
2819909Sdist /*
291159Sbill  * This program is not related to David Wall, whose Stanford Ph.D. thesis
301159Sbill  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
311159Sbill  */
321159Sbill 
3335531Sbostic #include <sys/param.h>
3439423Sbostic #include <sys/stat.h>
3535531Sbostic #include <sys/time.h>
3639423Sbostic #include <sys/uio.h>
371159Sbill #include <utmp.h>
3835531Sbostic #include <pwd.h>
3935531Sbostic #include <stdio.h>
4039124Sbostic #include <paths.h>
4116256Sralph 
4235531Sbostic #define	IGNOREUSER	"sleeper"
431159Sbill 
44*40223Skarels int nobanner;
4539089Smarc int mbufsize;
4639089Smarc char *mbuf;
471159Sbill 
4835531Sbostic /* ARGSUSED */
491159Sbill main(argc, argv)
5035531Sbostic 	int argc;
5135531Sbostic 	char **argv;
521159Sbill {
53*40223Skarels 	extern int optind;
54*40223Skarels 	int ch;
5539423Sbostic 	struct iovec iov;
5635531Sbostic 	struct utmp utmp;
5735531Sbostic 	FILE *fp;
5839423Sbostic 	char *p, *ttymsg();
591159Sbill 
60*40223Skarels 	while ((ch = getopt(argc, argv, "n")) != EOF)
61*40223Skarels 		switch (ch) {
62*40223Skarels 		case 'n':
63*40223Skarels 			/* undoc option for shutdown: suppress banner */
64*40223Skarels 			if (geteuid() == 0)
65*40223Skarels 				nobanner = 1;
66*40223Skarels 			break;
67*40223Skarels 		case '?':
68*40223Skarels 		default:
69*40223Skarels usage:
70*40223Skarels 			(void)fprintf(stderr, "usage: wall [file]\n");
71*40223Skarels 			exit(1);
72*40223Skarels 		}
73*40223Skarels 	argc -= optind;
74*40223Skarels 	argv += optind;
75*40223Skarels 	if (argc > 1)
76*40223Skarels 		goto usage;
7735531Sbostic 
78*40223Skarels 	makemsg(*argv);
7939423Sbostic 
8039089Smarc 	if (!(fp = fopen(_PATH_UTMP, "r"))) {
8139423Sbostic 		(void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP);
8235531Sbostic 		exit(1);
831159Sbill 	}
8439423Sbostic 	iov.iov_base = mbuf;
8539423Sbostic 	iov.iov_len = mbufsize;
8635531Sbostic 	/* NOSTRICT */
8739423Sbostic 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
8839423Sbostic 		if (!utmp.ut_name[0] ||
8939423Sbostic 		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
9039423Sbostic 			continue;
9139423Sbostic 		if (p = ttymsg(&iov, 1, utmp.ut_line, 1))
9239423Sbostic 			(void)fprintf(stderr, "wall: %s\n", p);
9339423Sbostic 	}
941159Sbill 	exit(0);
951159Sbill }
961159Sbill 
9739423Sbostic makemsg(fname)
9839423Sbostic 	char *fname;
991159Sbill {
10035531Sbostic 	register int ch, cnt;
10135531Sbostic 	struct tm *lt;
10235531Sbostic 	struct passwd *pw, *getpwuid();
10335531Sbostic 	struct stat sbuf;
10435531Sbostic 	time_t now, time();
10535531Sbostic 	FILE *fp;
10635531Sbostic 	int fd;
10735531Sbostic 	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
10835531Sbostic 	char *getlogin(), *malloc(), *strcpy(), *ttyname();
1091159Sbill 
11039089Smarc 	(void)strcpy(tmpname, _PATH_TMP);
11139124Sbostic 	(void)strcat(tmpname, "/wall.XXXXXX");
11235531Sbostic 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
11339423Sbostic 		(void)fprintf(stderr, "wall: can't open temporary file.\n");
11435531Sbostic 		exit(1);
11535531Sbostic 	}
11635531Sbostic 	(void)unlink(tmpname);
11716256Sralph 
118*40223Skarels 	if (!nobanner) {
119*40223Skarels 		if (!(whom = getlogin()))
120*40223Skarels 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
121*40223Skarels 		(void)gethostname(hostname, sizeof(hostname));
122*40223Skarels 		(void)time(&now);
123*40223Skarels 		lt = localtime(&now);
12435531Sbostic 
125*40223Skarels 		/*
126*40223Skarels 		 * all this stuff is to blank out a square for the message;
127*40223Skarels 		 * we wrap message lines at column 79, not 80, because some
128*40223Skarels 		 * terminals wrap after 79, some do not, and we can't tell.
129*40223Skarels 		 * Which means that we may leave a non-blank character
130*40223Skarels 		 * in column 80, but that can't be helped.
131*40223Skarels 		 */
132*40223Skarels 		(void)fprintf(fp, "\r%79s\r\n", " ");
133*40223Skarels 		(void)sprintf(lbuf, "Broadcast Message from %s@%s",
134*40223Skarels 		    whom, hostname);
135*40223Skarels 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
136*40223Skarels 		(void)sprintf(lbuf, "        (%s) at %d:%02d ...", ttyname(2),
137*40223Skarels 		    lt->tm_hour, lt->tm_min);
138*40223Skarels 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
139*40223Skarels 	}
14039423Sbostic 	(void)fprintf(fp, "%79s\r\n", " ");
14135531Sbostic 
14239423Sbostic 	if (*fname && !(freopen(fname, "r", stdin))) {
14339423Sbostic 		(void)fprintf(stderr, "wall: can't read %s.\n", fname);
14435531Sbostic 		exit(1);
14516256Sralph 	}
14635531Sbostic 	while (fgets(lbuf, sizeof(lbuf), stdin))
14739089Smarc 		for (cnt = 0, p = lbuf; ch = *p; ++p, ++cnt) {
14839089Smarc 			if (cnt == 79 || ch == '\n') {
14939423Sbostic 				for (; cnt < 79; ++cnt)
15039423Sbostic 					putc(' ', fp);
15135531Sbostic 				putc('\r', fp);
15235531Sbostic 				putc('\n', fp);
15339089Smarc 				cnt = 0;
15435531Sbostic 			} else
15535531Sbostic 				putc(ch, fp);
15639089Smarc 		}
15739423Sbostic 	(void)fprintf(fp, "%79s\r\n", " ");
15835531Sbostic 	rewind(fp);
15935531Sbostic 
16035531Sbostic 	if (fstat(fd, &sbuf)) {
16139423Sbostic 		(void)fprintf(stderr, "wall: can't stat temporary file.\n");
16235531Sbostic 		exit(1);
16316256Sralph 	}
16435531Sbostic 	mbufsize = sbuf.st_size;
16535531Sbostic 	if (!(mbuf = malloc((u_int)mbufsize))) {
16639423Sbostic 		(void)fprintf(stderr, "wall: out of memory.\n");
16735531Sbostic 		exit(1);
16816256Sralph 	}
16935776Sbostic 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
17039423Sbostic 		(void)fprintf(stderr, "wall: can't read temporary file.\n");
17135776Sbostic 		exit(1);
17235776Sbostic 	}
17335531Sbostic 	(void)close(fd);
17435531Sbostic }
175