Home | History | Annotate | Line # | Download | only in rwall
rwall.c revision 1.10.8.1
      1  1.10.8.1   minoura /* $NetBSD: rwall.c,v 1.10.8.1 2000/06/23 16:39:55 minoura Exp $ */
      2       1.6       tls 
      3       1.1       cgd /*
      4       1.1       cgd  * Copyright (c) 1993 Christopher G. Demetriou
      5       1.1       cgd  * Copyright (c) 1988, 1990 Regents of the University of California.
      6       1.1       cgd  * All rights reserved.
      7       1.1       cgd  *
      8       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9       1.1       cgd  * modification, are permitted provided that the following conditions
     10       1.1       cgd  * are met:
     11       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17       1.1       cgd  *    must display the following acknowledgement:
     18       1.1       cgd  *	This product includes software developed by the University of
     19       1.1       cgd  *	California, Berkeley and its contributors.
     20       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     21       1.1       cgd  *    may be used to endorse or promote products derived from this software
     22       1.1       cgd  *    without specific prior written permission.
     23       1.1       cgd  *
     24       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34       1.1       cgd  * SUCH DAMAGE.
     35       1.1       cgd  */
     36       1.1       cgd 
     37       1.7     lukem #include <sys/cdefs.h>
     38       1.1       cgd #ifndef lint
     39       1.7     lukem __COPYRIGHT("@(#) Copyright (c) 1988 Regents of the University of California.\n\
     40       1.7     lukem  All rights reserved.\n");
     41       1.1       cgd #endif /* not lint */
     42       1.1       cgd 
     43       1.1       cgd #ifndef lint
     44       1.7     lukem #if 0
     45       1.7     lukem static char sccsid[] = "from: @(#)wall.c	5.14 (Berkeley) 3/2/91";
     46       1.7     lukem #else
     47  1.10.8.1   minoura __RCSID("$NetBSD: rwall.c,v 1.10.8.1 2000/06/23 16:39:55 minoura Exp $");
     48       1.7     lukem #endif
     49       1.1       cgd #endif /* not lint */
     50       1.1       cgd 
     51       1.1       cgd /*
     52       1.1       cgd  * This program is not related to David Wall, whose Stanford Ph.D. thesis
     53       1.1       cgd  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
     54       1.1       cgd  */
     55       1.7     lukem #include <sys/types.h>
     56       1.7     lukem #include <sys/param.h>
     57       1.7     lukem #include <sys/stat.h>
     58       1.7     lukem #include <err.h>
     59       1.7     lukem #include <paths.h>
     60       1.7     lukem #include <pwd.h>
     61       1.5       jtc #include <stdio.h>
     62       1.5       jtc #include <stdlib.h>
     63       1.5       jtc #include <string.h>
     64       1.5       jtc #include <time.h>
     65       1.5       jtc #include <unistd.h>
     66       1.1       cgd 
     67       1.1       cgd #include <rpc/rpc.h>
     68       1.1       cgd #include <rpcsvc/rwall.h>
     69       1.1       cgd 
     70       1.5       jtc struct timeval timeout = { 25, 0 };
     71       1.1       cgd int mbufsize;
     72       1.1       cgd char *mbuf;
     73       1.1       cgd 
     74       1.7     lukem int	main __P((int, char **));
     75       1.7     lukem void	makemsg __P((char *));
     76       1.5       jtc 
     77       1.5       jtc int
     78       1.1       cgd main(argc, argv)
     79       1.1       cgd 	int argc;
     80       1.1       cgd 	char **argv;
     81       1.1       cgd {
     82       1.3    brezak 	char *wallhost, res;
     83       1.1       cgd 	CLIENT *cl;
     84       1.1       cgd 
     85       1.1       cgd 	if ((argc < 2) || (argc > 3)) {
     86       1.1       cgd 		fprintf(stderr, "usage: %s hostname [file]\n", argv[0]);
     87       1.1       cgd 		exit(1);
     88       1.1       cgd 	}
     89       1.1       cgd 
     90       1.1       cgd 	wallhost = argv[1];
     91       1.1       cgd 
     92       1.1       cgd 	makemsg(argv[2]);
     93       1.1       cgd 
     94       1.1       cgd 	/*
     95       1.1       cgd 	 * Create client "handle" used for calling MESSAGEPROG on the
     96       1.1       cgd 	 * server designated on the command line. We tell the rpc package
     97       1.1       cgd 	 * to use the "tcp" protocol when contacting the server.
     98       1.1       cgd 	*/
     99       1.1       cgd 	cl = clnt_create(wallhost, WALLPROG, WALLVERS, "udp");
    100       1.1       cgd 	if (cl == NULL) {
    101       1.1       cgd 		/*
    102       1.1       cgd 		 * Couldn't establish connection with server.
    103       1.1       cgd 		 * Print error message and die.
    104       1.1       cgd 		 */
    105       1.1       cgd 		clnt_pcreateerror(wallhost);
    106       1.1       cgd 		exit(1);
    107       1.1       cgd 	}
    108       1.1       cgd 
    109      1.10  christos 	if (clnt_call(cl, WALLPROC_WALL, xdr_wrapstring, (caddr_t)&mbuf, xdr_void, &res, timeout) != RPC_SUCCESS) {
    110       1.1       cgd 		/*
    111       1.1       cgd 		 * An error occurred while calling the server.
    112       1.1       cgd 		 * Print error message and die.
    113       1.1       cgd 		 */
    114       1.1       cgd 		clnt_perror(cl, wallhost);
    115       1.1       cgd 		exit(1);
    116       1.1       cgd 	}
    117       1.1       cgd 
    118       1.1       cgd 	exit(0);
    119       1.1       cgd }
    120       1.1       cgd 
    121       1.5       jtc void
    122       1.1       cgd makemsg(fname)
    123       1.1       cgd 	char *fname;
    124       1.1       cgd {
    125       1.1       cgd 	struct tm *lt;
    126       1.1       cgd 	struct passwd *pw;
    127       1.1       cgd 	struct stat sbuf;
    128       1.5       jtc 	time_t now;
    129       1.1       cgd 	FILE *fp;
    130       1.1       cgd 	int fd;
    131       1.9   mycroft 	const char *whom;
    132       1.9   mycroft 	char hostname[MAXHOSTNAMELEN + 1], lbuf[100], tmpname[32];
    133       1.1       cgd 
    134       1.1       cgd 	(void)strcpy(tmpname, _PATH_TMP);
    135       1.1       cgd 	(void)strcat(tmpname, "/wall.XXXXXX");
    136       1.7     lukem 	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+")))
    137       1.7     lukem 		err(1, "can't open temporary file.");
    138       1.1       cgd 	(void)unlink(tmpname);
    139       1.1       cgd 
    140       1.1       cgd 	if (!(whom = getlogin()))
    141       1.1       cgd 		whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
    142       1.1       cgd 	(void)gethostname(hostname, sizeof(hostname));
    143       1.8       mrg 	hostname[sizeof(hostname) - 1] = '\0';
    144       1.1       cgd 	(void)time(&now);
    145       1.1       cgd 	lt = localtime(&now);
    146       1.1       cgd 
    147       1.1       cgd 	/*
    148       1.1       cgd 	 * all this stuff is to blank out a square for the message;
    149       1.1       cgd 	 * we wrap message lines at column 79, not 80, because some
    150       1.1       cgd 	 * terminals wrap after 79, some do not, and we can't tell.
    151       1.1       cgd 	 * Which means that we may leave a non-blank character
    152       1.1       cgd 	 * in column 80, but that can't be helped.
    153       1.1       cgd 	 */
    154       1.1       cgd 	(void)fprintf(fp, "Remote Broadcast Message from %s@%s\n",
    155       1.1       cgd 	    whom, hostname);
    156       1.1       cgd 	(void)fprintf(fp, "        (%s) at %d:%02d ...\n", ttyname(2),
    157       1.1       cgd 	    lt->tm_hour, lt->tm_min);
    158       1.1       cgd 
    159       1.1       cgd 	putc('\n', fp);
    160       1.1       cgd 
    161       1.7     lukem 	if (fname && !(freopen(fname, "r", stdin)))
    162       1.7     lukem 		err(1, "can't read %s.", fname);
    163       1.1       cgd 	while (fgets(lbuf, sizeof(lbuf), stdin))
    164       1.1       cgd 		fputs(lbuf, fp);
    165       1.1       cgd 	rewind(fp);
    166       1.1       cgd 
    167       1.7     lukem 	if (fstat(fd, &sbuf))
    168       1.7     lukem 		err(1, "can't stat temporary file.");
    169       1.1       cgd 	mbufsize = sbuf.st_size;
    170       1.7     lukem 	if (!(mbuf = malloc((u_int)mbufsize)))
    171       1.7     lukem 		err(1, "malloc");
    172       1.7     lukem 	if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
    173       1.7     lukem 		err(1, "can't read temporary file.");
    174       1.1       cgd 	(void)close(fd);
    175       1.1       cgd }
    176