Home | History | Annotate | Line # | Download | only in wall
wall.c revision 1.8
      1 /*	$NetBSD: wall.c,v 1.8 1997/06/29 18:28:20 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)wall.c	8.2 (Berkeley) 11/16/93";
     45 #endif
     46 __RCSID("$NetBSD: wall.c,v 1.8 1997/06/29 18:28:20 christos Exp $");
     47 #endif /* not lint */
     48 
     49 /*
     50  * This program is not related to David Wall, whose Stanford Ph.D. thesis
     51  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
     52  */
     53 
     54 #include <sys/param.h>
     55 #include <sys/stat.h>
     56 #include <sys/time.h>
     57 #include <sys/uio.h>
     58 
     59 #include <paths.h>
     60 #include <pwd.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 #include <utmp.h>
     66 #include <util.h>
     67 
     68 void	makemsg __P((char *));
     69 int	main __P((int, char *[]));
     70 
     71 #define	IGNOREUSER	"sleeper"
     72 
     73 int nobanner;
     74 int mbufsize;
     75 char *mbuf;
     76 
     77 /* ARGSUSED */
     78 int
     79 main(argc, argv)
     80 	int argc;
     81 	char **argv;
     82 {
     83 	extern int optind;
     84 	int ch;
     85 	struct iovec iov;
     86 	struct utmp utmp;
     87 	FILE *fp;
     88 	char *p;
     89 	struct passwd *pep = getpwnam("nobody");
     90 	char line[sizeof(utmp.ut_line) + 1];
     91 
     92 	while ((ch = getopt(argc, argv, "n")) != EOF)
     93 		switch (ch) {
     94 		case 'n':
     95 			/* undoc option for shutdown: suppress banner */
     96 			if (geteuid() == 0 || (pep && getuid() == pep->pw_uid))
     97 				nobanner = 1;
     98 			break;
     99 		case '?':
    100 		default:
    101 usage:
    102 			(void)fprintf(stderr, "usage: wall [file]\n");
    103 			exit(1);
    104 		}
    105 	argc -= optind;
    106 	argv += optind;
    107 	if (argc > 1)
    108 		goto usage;
    109 
    110 	makemsg(*argv);
    111 
    112 	if (!(fp = fopen(_PATH_UTMP, "r"))) {
    113 		(void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP);
    114 		exit(1);
    115 	}
    116 	iov.iov_base = mbuf;
    117 	iov.iov_len = mbufsize;
    118 	/* NOSTRICT */
    119 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
    120 		if (!utmp.ut_name[0] ||
    121 		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
    122 			continue;
    123 		strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
    124 		line[sizeof(utmp.ut_line)] = '\0';
    125 		if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
    126 			(void)fprintf(stderr, "wall: %s\n", p);
    127 	}
    128 	exit(0);
    129 }
    130 
    131 void
    132 makemsg(fname)
    133 	char *fname;
    134 {
    135 	register int ch, cnt;
    136 	struct tm *lt;
    137 	struct passwd *pw;
    138 	struct stat sbuf;
    139 	time_t now;
    140 	FILE *fp;
    141 	int fd;
    142 	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
    143 
    144 	(void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP);
    145 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
    146 		(void)fprintf(stderr, "wall: can't open temporary file.\n");
    147 		exit(1);
    148 	}
    149 	(void)unlink(tmpname);
    150 
    151 	if (!nobanner) {
    152 		if (!(whom = getlogin()))
    153 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
    154 		(void)gethostname(hostname, sizeof(hostname));
    155 		(void)time(&now);
    156 		lt = localtime(&now);
    157 
    158 		/*
    159 		 * all this stuff is to blank out a square for the message;
    160 		 * we wrap message lines at column 79, not 80, because some
    161 		 * terminals wrap after 79, some do not, and we can't tell.
    162 		 * Which means that we may leave a non-blank character
    163 		 * in column 80, but that can't be helped.
    164 		 */
    165 		(void)fprintf(fp, "\r%79s\r\n", " ");
    166 		(void)snprintf(lbuf, sizeof lbuf,
    167 		    "Broadcast Message from %s@%s", whom, hostname);
    168 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
    169 		(void)snprintf(lbuf, sizeof lbuf, "        (%s) at %d:%02d ...",
    170 		    ttyname(2), lt->tm_hour, lt->tm_min);
    171 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
    172 	}
    173 	(void)fprintf(fp, "%79s\r\n", " ");
    174 
    175 	if (fname && !(freopen(fname, "r", stdin))) {
    176 		(void)fprintf(stderr, "wall: can't read %s.\n", fname);
    177 		exit(1);
    178 	}
    179 	while (fgets(lbuf, sizeof(lbuf), stdin))
    180 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
    181 			if (cnt == 79 || ch == '\n') {
    182 				for (; cnt < 79; ++cnt)
    183 					putc(' ', fp);
    184 				putc('\r', fp);
    185 				putc('\n', fp);
    186 				cnt = -1;
    187 			}
    188 			if (ch != '\n')
    189 				putc(ch, fp);
    190 		}
    191 	(void)fprintf(fp, "%79s\r\n", " ");
    192 	rewind(fp);
    193 
    194 	if (fstat(fd, &sbuf)) {
    195 		(void)fprintf(stderr, "wall: can't stat temporary file.\n");
    196 		exit(1);
    197 	}
    198 	mbufsize = sbuf.st_size;
    199 	if (!(mbuf = malloc((u_int)mbufsize))) {
    200 		(void)fprintf(stderr, "wall: out of memory.\n");
    201 		exit(1);
    202 	}
    203 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
    204 		(void)fprintf(stderr, "wall: can't read temporary file.\n");
    205 		exit(1);
    206 	}
    207 	(void)close(fd);
    208 }
    209