Home | History | Annotate | Line # | Download | only in wall
wall.c revision 1.7
      1 /*	$NetBSD: wall.c,v 1.7 1997/02/11 08:42:04 mrg 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 #ifndef lint
     37 static char copyright[] =
     38 "@(#) 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 static char rcsid[] = "$NetBSD: wall.c,v 1.7 1997/02/11 08:42:04 mrg 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 
     67 void	makemsg __P((char *));
     68 
     69 #define	IGNOREUSER	"sleeper"
     70 
     71 int nobanner;
     72 int mbufsize;
     73 char *mbuf;
     74 
     75 /* ARGSUSED */
     76 int
     77 main(argc, argv)
     78 	int argc;
     79 	char **argv;
     80 {
     81 	extern int optind;
     82 	int ch;
     83 	struct iovec iov;
     84 	struct utmp utmp;
     85 	FILE *fp;
     86 	char *p, *ttymsg();
     87 	struct passwd *pep = getpwnam("nobody");
     88 	char line[sizeof(utmp.ut_line) + 1];
     89 
     90 	while ((ch = getopt(argc, argv, "n")) != EOF)
     91 		switch (ch) {
     92 		case 'n':
     93 			/* undoc option for shutdown: suppress banner */
     94 			if (geteuid() == 0 || (pep && getuid() == pep->pw_uid))
     95 				nobanner = 1;
     96 			break;
     97 		case '?':
     98 		default:
     99 usage:
    100 			(void)fprintf(stderr, "usage: wall [file]\n");
    101 			exit(1);
    102 		}
    103 	argc -= optind;
    104 	argv += optind;
    105 	if (argc > 1)
    106 		goto usage;
    107 
    108 	makemsg(*argv);
    109 
    110 	if (!(fp = fopen(_PATH_UTMP, "r"))) {
    111 		(void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP);
    112 		exit(1);
    113 	}
    114 	iov.iov_base = mbuf;
    115 	iov.iov_len = mbufsize;
    116 	/* NOSTRICT */
    117 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
    118 		if (!utmp.ut_name[0] ||
    119 		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
    120 			continue;
    121 		strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
    122 		line[sizeof(utmp.ut_line)] = '\0';
    123 		if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
    124 			(void)fprintf(stderr, "wall: %s\n", p);
    125 	}
    126 	exit(0);
    127 }
    128 
    129 void
    130 makemsg(fname)
    131 	char *fname;
    132 {
    133 	register int ch, cnt;
    134 	struct tm *lt;
    135 	struct passwd *pw;
    136 	struct stat sbuf;
    137 	time_t now, time();
    138 	FILE *fp;
    139 	int fd;
    140 	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
    141 
    142 	(void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP);
    143 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
    144 		(void)fprintf(stderr, "wall: can't open temporary file.\n");
    145 		exit(1);
    146 	}
    147 	(void)unlink(tmpname);
    148 
    149 	if (!nobanner) {
    150 		if (!(whom = getlogin()))
    151 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
    152 		(void)gethostname(hostname, sizeof(hostname));
    153 		(void)time(&now);
    154 		lt = localtime(&now);
    155 
    156 		/*
    157 		 * all this stuff is to blank out a square for the message;
    158 		 * we wrap message lines at column 79, not 80, because some
    159 		 * terminals wrap after 79, some do not, and we can't tell.
    160 		 * Which means that we may leave a non-blank character
    161 		 * in column 80, but that can't be helped.
    162 		 */
    163 		(void)fprintf(fp, "\r%79s\r\n", " ");
    164 		(void)snprintf(lbuf, sizeof lbuf,
    165 		    "Broadcast Message from %s@%s", whom, hostname);
    166 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
    167 		(void)snprintf(lbuf, sizeof lbuf, "        (%s) at %d:%02d ...",
    168 		    ttyname(2), lt->tm_hour, lt->tm_min);
    169 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
    170 	}
    171 	(void)fprintf(fp, "%79s\r\n", " ");
    172 
    173 	if (fname && !(freopen(fname, "r", stdin))) {
    174 		(void)fprintf(stderr, "wall: can't read %s.\n", fname);
    175 		exit(1);
    176 	}
    177 	while (fgets(lbuf, sizeof(lbuf), stdin))
    178 		for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
    179 			if (cnt == 79 || ch == '\n') {
    180 				for (; cnt < 79; ++cnt)
    181 					putc(' ', fp);
    182 				putc('\r', fp);
    183 				putc('\n', fp);
    184 				cnt = -1;
    185 			}
    186 			if (ch != '\n')
    187 				putc(ch, fp);
    188 		}
    189 	(void)fprintf(fp, "%79s\r\n", " ");
    190 	rewind(fp);
    191 
    192 	if (fstat(fd, &sbuf)) {
    193 		(void)fprintf(stderr, "wall: can't stat temporary file.\n");
    194 		exit(1);
    195 	}
    196 	mbufsize = sbuf.st_size;
    197 	if (!(mbuf = malloc((u_int)mbufsize))) {
    198 		(void)fprintf(stderr, "wall: out of memory.\n");
    199 		exit(1);
    200 	}
    201 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
    202 		(void)fprintf(stderr, "wall: can't read temporary file.\n");
    203 		exit(1);
    204 	}
    205 	(void)close(fd);
    206 }
    207