xref: /csrg-svn/usr.bin/wall/wall.c (revision 1159)
1*1159Sbill static char *sccsid = "@(#)wall.c	4.1 (Berkeley) 10/01/80";
2*1159Sbill /*
3*1159Sbill  * wall.c - Broadcast a message to all users.
4*1159Sbill  *
5*1159Sbill  * This program is not related to David Wall, whose Stanford Ph.D. thesis
6*1159Sbill  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
7*1159Sbill  */
8*1159Sbill 
9*1159Sbill #include <stdio.h>
10*1159Sbill #include <utmp.h>
11*1159Sbill #include <time.h>
12*1159Sbill #define	USERS	128
13*1159Sbill 
14*1159Sbill char	mesg[3000];
15*1159Sbill int	msize,sline;
16*1159Sbill struct	utmp utmp[USERS];
17*1159Sbill char	*strcpy();
18*1159Sbill char	*strcat();
19*1159Sbill char who[9] = "???";
20*1159Sbill long	clock;
21*1159Sbill struct tm *localtime();
22*1159Sbill struct tm *localclock;
23*1159Sbill 
24*1159Sbill main(argc, argv)
25*1159Sbill char *argv[];
26*1159Sbill {
27*1159Sbill 	register i;
28*1159Sbill 	register char c;
29*1159Sbill 	register struct utmp *p;
30*1159Sbill 	FILE *f;
31*1159Sbill 
32*1159Sbill 	if((f = fopen("/etc/utmp", "r")) == NULL) {
33*1159Sbill 		fprintf(stderr, "Cannot open /etc/utmp\n");
34*1159Sbill 		exit(1);
35*1159Sbill 	}
36*1159Sbill 	clock = time( 0 );
37*1159Sbill 	localclock = localtime( &clock );
38*1159Sbill 	fread((char *)utmp, sizeof(struct utmp), USERS, f);
39*1159Sbill 	fclose(f);
40*1159Sbill 	f = stdin;
41*1159Sbill 	if(argc >= 2) {
42*1159Sbill 		/* take message from unix file instead of standard input */
43*1159Sbill 		if((f = fopen(argv[1], "r")) == NULL) {
44*1159Sbill 			fprintf(stderr,"Cannot open %s\n", argv[1]);
45*1159Sbill 			exit(1);
46*1159Sbill 		}
47*1159Sbill 	}
48*1159Sbill 	while((i = getc(f)) != EOF) mesg[msize++] = i;
49*1159Sbill 	fclose(f);
50*1159Sbill 	sline = ttyslot(2); /* 'utmp' slot no. of sender */
51*1159Sbill 	if (sline) {
52*1159Sbill 		for (i=0;(c=utmp[sline].ut_name[i]) && i<sizeof(utmp[0].ut_name);i++)
53*1159Sbill 			who[i]=c;
54*1159Sbill 		who[i] = '\0'; /* sender initials */
55*1159Sbill 		}
56*1159Sbill 	for(i=0; i<USERS; i++) {
57*1159Sbill 		p = &utmp[i];
58*1159Sbill 		if(p->ut_name[0] == 0)
59*1159Sbill 			continue;
60*1159Sbill 		sleep(1);
61*1159Sbill 		sendmes(p->ut_line);
62*1159Sbill 	}
63*1159Sbill 	exit(0);
64*1159Sbill }
65*1159Sbill 
66*1159Sbill sendmes(tty)
67*1159Sbill char *tty;
68*1159Sbill {
69*1159Sbill 	register i;
70*1159Sbill 	char t[50], buf[BUFSIZ];
71*1159Sbill 	register char *cp;
72*1159Sbill 	register int c, ch;
73*1159Sbill 	FILE *f;
74*1159Sbill 
75*1159Sbill 	i = fork();
76*1159Sbill 	if(i == -1) {
77*1159Sbill 		fprintf(stderr, "Try again\n");
78*1159Sbill 		return;
79*1159Sbill 	}
80*1159Sbill 	if(i)
81*1159Sbill 		return;
82*1159Sbill 	strcpy(t, "/dev/");
83*1159Sbill 	strcat(t, tty);
84*1159Sbill 
85*1159Sbill 	if((f = fopen(t, "w")) == NULL) {
86*1159Sbill 		fprintf(stderr,"cannot open %s\n", t);
87*1159Sbill 		exit(1);
88*1159Sbill 	}
89*1159Sbill 	setbuf(f, buf);
90*1159Sbill 	fprintf(f, "\nBroadcast Message from %s (%s) at %d:%02d ...\r\n\n"
91*1159Sbill 	       ,who, utmp[sline].ut_line
92*1159Sbill 	       , localclock -> tm_hour , localclock -> tm_min );
93*1159Sbill 	/* fwrite(mesg, msize, 1, f); */
94*1159Sbill 	for (cp = mesg, c = msize; c-- > 0; cp++) {
95*1159Sbill 		ch = *cp;
96*1159Sbill 		if (ch == '\n')
97*1159Sbill 			putc('\r', f);
98*1159Sbill 		putc(ch, f);
99*1159Sbill 	}
100*1159Sbill 
101*1159Sbill 	/*
102*1159Sbill 	 * Bitchin'.
103*1159Sbill 	 */
104*1159Sbill 
105*1159Sbill 	exit(0);
106*1159Sbill }
107