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