xref: /csrg-svn/usr.bin/wall/wall.c (revision 39124)
11159Sbill /*
235531Sbostic  * Copyright (c) 1988 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*39124Sbostic static char sccsid[] = "@(#)wall.c	5.8 (Berkeley) 09/11/89";
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>
3435531Sbostic #include <sys/time.h>
3535531Sbostic #include <sys/stat.h>
3635531Sbostic #include <sys/dir.h>
3739089Smarc #include <sys/file.h>
381159Sbill #include <utmp.h>
3935531Sbostic #include <pwd.h>
4016256Sralph #include <errno.h>
4135531Sbostic #include <stdio.h>
42*39124Sbostic #include <paths.h>
4316256Sralph 
4435531Sbostic #define	IGNOREUSER	"sleeper"
451159Sbill 
4639089Smarc int mbufsize;
4739089Smarc char *mbuf;
481159Sbill 
4935531Sbostic /* ARGSUSED */
501159Sbill main(argc, argv)
5135531Sbostic 	int argc;
5235531Sbostic 	char **argv;
531159Sbill {
5435531Sbostic 	struct utmp utmp;
5535531Sbostic 	FILE *fp;
561159Sbill 
5735531Sbostic 	if (argc > 2) {
5835531Sbostic 		fprintf(stderr, "usage: wall [file]\n");
591159Sbill 		exit(1);
601159Sbill 	}
6135531Sbostic 	makemsg(argv);
6235531Sbostic 
6339089Smarc 	if (!(fp = fopen(_PATH_UTMP, "r"))) {
6439089Smarc 		fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP);
6535531Sbostic 		exit(1);
661159Sbill 	}
6735531Sbostic 	/* NOSTRICT */
6835776Sbostic 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1)
6935776Sbostic 		if (utmp.ut_name[0] &&
7035776Sbostic 		    strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
7139089Smarc 			sendmsg(utmp.ut_line, 1, mbufsize);
721159Sbill 	exit(0);
731159Sbill }
741159Sbill 
7535531Sbostic makemsg(argv)
7635531Sbostic 	char **argv;
771159Sbill {
7835531Sbostic 	register int ch, cnt;
7935531Sbostic 	struct tm *lt;
8035531Sbostic 	struct passwd *pw, *getpwuid();
8135531Sbostic 	struct stat sbuf;
8235531Sbostic 	time_t now, time();
8335531Sbostic 	FILE *fp;
8435531Sbostic 	int fd;
8535531Sbostic 	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
8635531Sbostic 	char *getlogin(), *malloc(), *strcpy(), *ttyname();
871159Sbill 
8839089Smarc 	(void)strcpy(tmpname, _PATH_TMP);
89*39124Sbostic 	(void)strcat(tmpname, "/wall.XXXXXX");
9035531Sbostic 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
9135531Sbostic 		fprintf(stderr, "wall: can't open temporary file.\n");
9235531Sbostic 		exit(1);
9335531Sbostic 	}
9435531Sbostic 	(void)unlink(tmpname);
9516256Sralph 
9635531Sbostic 	if (!(whom = getlogin()))
9735531Sbostic 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
9835531Sbostic 	(void)gethostname(hostname, sizeof(hostname));
9935531Sbostic 	(void)time(&now);
10035531Sbostic 	lt = localtime(&now);
10135531Sbostic 
10235531Sbostic 	/*
10335531Sbostic 	 * all this stuff is to blank out a square for the message; we
10439089Smarc 	 * wrap message lines at column 79, not 80, because some terminals
10539089Smarc 	 * wrap after 79, some do not, and we can't tell.
10635531Sbostic 	 */
10735531Sbostic 	fprintf(fp, "\r%79s\r\n", " ");
10835531Sbostic 	(void)sprintf(lbuf, "Broadcast Message from %s@%s", whom, hostname);
10935531Sbostic 	fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
11035531Sbostic 	(void)sprintf(lbuf, "        (%s) at %d:%02d ...", ttyname(2),
11135531Sbostic 	    lt->tm_hour, lt->tm_min);
11235531Sbostic 	fprintf(fp, "%-79.79s\r\n", lbuf);
11335531Sbostic 	fprintf(fp, "%79s\r\n", " ");
11435531Sbostic 
11535531Sbostic 	if (*++argv && !(freopen(*argv, "r", stdin))) {
11635531Sbostic 		fprintf(stderr, "wall: can't read %s.\n", *argv);
11735531Sbostic 		exit(1);
11816256Sralph 	}
11935531Sbostic 	while (fgets(lbuf, sizeof(lbuf), stdin))
12039089Smarc 		for (cnt = 0, p = lbuf; ch = *p; ++p, ++cnt) {
12139089Smarc 			if (cnt == 79 || ch == '\n') {
12235531Sbostic 				putc('\r', fp);
12335531Sbostic 				putc('\n', fp);
12439089Smarc 				cnt = 0;
12535531Sbostic 			} else
12635531Sbostic 				putc(ch, fp);
12739089Smarc 		}
12835531Sbostic 	fprintf(fp, "%79s\r\n", " ");
12935531Sbostic 	rewind(fp);
13035531Sbostic 
13135531Sbostic 	if (fstat(fd, &sbuf)) {
13235531Sbostic 		fprintf(stderr, "wall: can't stat temporary file.\n");
13335531Sbostic 		exit(1);
13416256Sralph 	}
13535531Sbostic 	mbufsize = sbuf.st_size;
13635531Sbostic 	if (!(mbuf = malloc((u_int)mbufsize))) {
13735531Sbostic 		fprintf(stderr, "wall: out of memory.\n");
13835531Sbostic 		exit(1);
13916256Sralph 	}
14035776Sbostic 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
14135776Sbostic 		fprintf(stderr, "wall: can't read temporary file.\n");
14235776Sbostic 		exit(1);
14335776Sbostic 	}
14435531Sbostic 	(void)close(fd);
14535531Sbostic }
14635531Sbostic 
14739089Smarc sendmsg(line, nonblock, left)
14835531Sbostic 	char *line;
14935531Sbostic {
15035531Sbostic 	extern int errno;
15139089Smarc 	static char device[MAXNAMLEN] = _PATH_DEV;
15239089Smarc 	register int fd, wret;
15339089Smarc 	char *lp, *strcpy(), *strerror();
15435531Sbostic 
15535531Sbostic 	(void)strcpy(device + 5, line);
15639089Smarc 	/*
15739089Smarc 	 * open will fail on slip lines or exclusive-use lines
15839089Smarc 	 * if not running as root
15939089Smarc 	 */
16039089Smarc 	if ((fd = open(device, O_WRONLY|(nonblock ? O_NONBLOCK : 0), 0)) < 0) {
16139089Smarc 		if (errno != EBUSY && errno != EPERM)
16239089Smarc 			(void)fprintf(stderr, "wall: %s: %s\n",
16339089Smarc 			    device, strerror(errno));
16439089Smarc 		return;
16516256Sralph 	}
16639089Smarc 	lp = mbuf + mbufsize - left;
16739089Smarc 	while (left) {
16839089Smarc 		wret = write(fd, lp, left);
16935776Sbostic 		if (wret >= 0) {
17035776Sbostic 			lp += wret;
17135776Sbostic 			left -= wret;
17239089Smarc 		} else
17339089Smarc 		if (errno == EWOULDBLOCK) {
17439089Smarc 			if (fork()) {
17535531Sbostic 				(void)close(fd);
17635531Sbostic 				return;
17735531Sbostic 			}
17839089Smarc 			/* wait at most 5 minutes */
17935531Sbostic 			(void)alarm((u_int)(60 * 5));
18039089Smarc 			sendmsg(line, 0, left);
18135531Sbostic 			exit(0);
18239089Smarc 		} else if (errno != ENODEV) {
18339089Smarc 			/*
18439089Smarc 			 * We get ENODEV on a slip line
18539089Smarc 			 * if we're running as root
18639089Smarc 			 */
18739089Smarc 			(void)fprintf(stderr, "wall: %s: %s\n",
18839089Smarc 			    device, strerror(errno));
18935531Sbostic 			break;
19035531Sbostic 		}
19135531Sbostic 	}
19235531Sbostic 	(void)close(fd);
1931159Sbill }
194