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