Home | History | Annotate | Line # | Download | only in wall
wall.c revision 1.1
      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[] = "@(#)wall.c	5.14 (Berkeley) 3/2/91";
     42 #endif /* not lint */
     43 
     44 /*
     45  * This program is not related to David Wall, whose Stanford Ph.D. thesis
     46  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
     47  */
     48 
     49 #include <sys/param.h>
     50 #include <sys/stat.h>
     51 #include <sys/time.h>
     52 #include <sys/uio.h>
     53 #include <utmp.h>
     54 #include <pwd.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <paths.h>
     58 
     59 #define	IGNOREUSER	"sleeper"
     60 
     61 int nobanner;
     62 int mbufsize;
     63 char *mbuf;
     64 
     65 /* ARGSUSED */
     66 main(argc, argv)
     67 	int argc;
     68 	char **argv;
     69 {
     70 	extern int optind;
     71 	int ch;
     72 	struct iovec iov;
     73 	struct utmp utmp;
     74 	FILE *fp;
     75 	char *p, *ttymsg();
     76 
     77 	while ((ch = getopt(argc, argv, "n")) != EOF)
     78 		switch (ch) {
     79 		case 'n':
     80 			/* undoc option for shutdown: suppress banner */
     81 			if (geteuid() == 0)
     82 				nobanner = 1;
     83 			break;
     84 		case '?':
     85 		default:
     86 usage:
     87 			(void)fprintf(stderr, "usage: wall [file]\n");
     88 			exit(1);
     89 		}
     90 	argc -= optind;
     91 	argv += optind;
     92 	if (argc > 1)
     93 		goto usage;
     94 
     95 	makemsg(*argv);
     96 
     97 	if (!(fp = fopen(_PATH_UTMP, "r"))) {
     98 		(void)fprintf(stderr, "wall: cannot read %s.\n", _PATH_UTMP);
     99 		exit(1);
    100 	}
    101 	iov.iov_base = mbuf;
    102 	iov.iov_len = mbufsize;
    103 	/* NOSTRICT */
    104 	while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
    105 		if (!utmp.ut_name[0] ||
    106 		    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
    107 			continue;
    108 		if (p = ttymsg(&iov, 1, utmp.ut_line))
    109 			(void)fprintf(stderr, "wall: %s\n", p);
    110 	}
    111 	exit(0);
    112 }
    113 
    114 makemsg(fname)
    115 	char *fname;
    116 {
    117 	register int ch, cnt;
    118 	struct tm *lt;
    119 	struct passwd *pw;
    120 	struct stat sbuf;
    121 	time_t now, time();
    122 	FILE *fp;
    123 	int fd;
    124 	char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[15];
    125 	char *getlogin(), *strcpy(), *ttyname();
    126 
    127 	(void)strcpy(tmpname, _PATH_TMP);
    128 	(void)strcat(tmpname, "/wall.XXXXXX");
    129 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+"))) {
    130 		(void)fprintf(stderr, "wall: can't open temporary file.\n");
    131 		exit(1);
    132 	}
    133 	(void)unlink(tmpname);
    134 
    135 	if (!nobanner) {
    136 		if (!(whom = getlogin()))
    137 			whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
    138 		(void)gethostname(hostname, sizeof(hostname));
    139 		(void)time(&now);
    140 		lt = localtime(&now);
    141 
    142 		/*
    143 		 * all this stuff is to blank out a square for the message;
    144 		 * we wrap message lines at column 79, not 80, because some
    145 		 * terminals wrap after 79, some do not, and we can't tell.
    146 		 * Which means that we may leave a non-blank character
    147 		 * in column 80, but that can't be helped.
    148 		 */
    149 		(void)fprintf(fp, "\r%79s\r\n", " ");
    150 		(void)sprintf(lbuf, "Broadcast Message from %s@%s",
    151 		    whom, hostname);
    152 		(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
    153 		(void)sprintf(lbuf, "        (%s) at %d:%02d ...", ttyname(2),
    154 		    lt->tm_hour, lt->tm_min);
    155 		(void)fprintf(fp, "%-79.79s\r\n", lbuf);
    156 	}
    157 	(void)fprintf(fp, "%79s\r\n", " ");
    158 
    159 	if (*fname && !(freopen(fname, "r", stdin))) {
    160 		(void)fprintf(stderr, "wall: can't read %s.\n", fname);
    161 		exit(1);
    162 	}
    163 	while (fgets(lbuf, sizeof(lbuf), stdin))
    164 		for (cnt = 0, p = lbuf; ch = *p; ++p, ++cnt) {
    165 			if (cnt == 79 || ch == '\n') {
    166 				for (; cnt < 79; ++cnt)
    167 					putc(' ', fp);
    168 				putc('\r', fp);
    169 				putc('\n', fp);
    170 				cnt = 0;
    171 			} else
    172 				putc(ch, fp);
    173 		}
    174 	(void)fprintf(fp, "%79s\r\n", " ");
    175 	rewind(fp);
    176 
    177 	if (fstat(fd, &sbuf)) {
    178 		(void)fprintf(stderr, "wall: can't stat temporary file.\n");
    179 		exit(1);
    180 	}
    181 	mbufsize = sbuf.st_size;
    182 	if (!(mbuf = malloc((u_int)mbufsize))) {
    183 		(void)fprintf(stderr, "wall: out of memory.\n");
    184 		exit(1);
    185 	}
    186 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize) {
    187 		(void)fprintf(stderr, "wall: can't read temporary file.\n");
    188 		exit(1);
    189 	}
    190 	(void)close(fd);
    191 }
    192