xref: /original-bsd/usr.bin/wall/wall.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1988, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1988, 1990, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)wall.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 /*
19  * This program is not related to David Wall, whose Stanford Ph.D. thesis
20  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
21  */
22 
23 #include <sys/param.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <sys/uio.h>
27 #include <utmp.h>
28 #include <pwd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <paths.h>
32 
33 #define	IGNOREUSER	"sleeper"
34 
35 int nobanner;
36 int mbufsize;
37 char *mbuf;
38 
39 /* ARGSUSED */
40 main(argc, argv)
41 	int argc;
42 	char **argv;
43 {
44 	extern int optind;
45 	int ch;
46 	struct iovec iov;
47 	struct utmp utmp;
48 	FILE *fp;
49 	char *p, *ttymsg();
50 
51 	while ((ch = getopt(argc, argv, "n")) != EOF)
52 		switch (ch) {
53 		case 'n':
54 			/* undoc option for shutdown: suppress banner */
55 			if (geteuid() == 0)
56 				nobanner = 1;
57 			break;
58 		case '?':
59 		default:
60 usage:
61 			(void)fprintf(stderr, "usage: wall [file]\n");
62 			exit(1);
63 		}
64 	argc -= optind;
65 	argv += optind;
66 	if (argc > 1)
67 		goto usage;
68 
69 	makemsg(*argv);
70 
71 	if (!(fp = fopen(_PATH_UTMP, "r"))) {
72 		(void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP);
73 		exit(1);
74 	}
75 	iov.iov_base = mbuf;
76 	iov.iov_len = mbufsize;
77 	/* NOSTRICT */
78 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
79 		if (!utmp.ut_name[0] ||
80 		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
81 			continue;
82 		if (p = ttymsg(&iov, 1, utmp.ut_line, 60*5))
83 			(void)fprintf(stderr, "wall: %s\n", p);
84 	}
85 	exit(0);
86 }
87 
88 makemsg(fname)
89 	char *fname;
90 {
91 	register int ch, cnt;
92 	struct tm *lt;
93 	struct passwd *pw;
94 	struct stat sbuf;
95 	time_t now, time();
96 	FILE *fp;
97 	int fd;
98 	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
99 	char *getlogin(), *strcpy(), *ttyname();
100 
101 	(void)strcpy(tmpname, _PATH_TMP);
102 	(void)strcat(tmpname, "/wall.XXXXXX");
103 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
104 		(void)fprintf(stderr, "wall: can't open temporary file.\n");
105 		exit(1);
106 	}
107 	(void)unlink(tmpname);
108 
109 	if (!nobanner) {
110 		if (!(whom = getlogin()))
111 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
112 		(void)gethostname(hostname, sizeof(hostname));
113 		(void)time(&now);
114 		lt = localtime(&now);
115 
116 		/*
117 		 * all this stuff is to blank out a square for the message;
118 		 * we wrap message lines at column 79, not 80, because some
119 		 * terminals wrap after 79, some do not, and we can't tell.
120 		 * Which means that we may leave a non-blank character
121 		 * in column 80, but that can't be helped.
122 		 */
123 		(void)fprintf(fp, "\r%79s\r\n", " ");
124 		(void)sprintf(lbuf, "Broadcast Message from %s@%s",
125 		    whom, hostname);
126 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
127 		(void)sprintf(lbuf, "        (%s) at %d:%02d ...", ttyname(2),
128 		    lt->tm_hour, lt->tm_min);
129 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
130 	}
131 	(void)fprintf(fp, "%79s\r\n", " ");
132 
133 	if (fname && !(freopen(fname, "r", stdin))) {
134 		(void)fprintf(stderr, "wall: can't read %s.\n", fname);
135 		exit(1);
136 	}
137 	while (fgets(lbuf, sizeof(lbuf), stdin))
138 		for (cnt = 0, p = lbuf; ch = *p; ++p, ++cnt) {
139 			if (cnt == 79 || ch == '\n') {
140 				for (; cnt < 79; ++cnt)
141 					putc(' ', fp);
142 				putc('\r', fp);
143 				putc('\n', fp);
144 				cnt = 0;
145 			} else
146 				putc(ch, fp);
147 		}
148 	(void)fprintf(fp, "%79s\r\n", " ");
149 	rewind(fp);
150 
151 	if (fstat(fd, &sbuf)) {
152 		(void)fprintf(stderr, "wall: can't stat temporary file.\n");
153 		exit(1);
154 	}
155 	mbufsize = sbuf.st_size;
156 	if (!(mbuf = malloc((u_int)mbufsize))) {
157 		(void)fprintf(stderr, "wall: out of memory.\n");
158 		exit(1);
159 	}
160 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
161 		(void)fprintf(stderr, "wall: can't read temporary file.\n");
162 		exit(1);
163 	}
164 	(void)close(fd);
165 }
166