Bell 32V development
[unix-history] / usr / src / cmd / wall.c
CommitLineData
6c3e3718
TL
1#include <stdio.h>
2#include <utmp.h>
3#define USERS 50
4
5char mesg[3000];
6int msize,sline;
7struct utmp utmp[USERS];
8char *strcpy();
9char *strcat();
10char who[9] = "???";
11
12main(argc, argv)
13char *argv[];
14{
15 register i;
16 register char c;
17 register struct utmp *p;
18 FILE *f;
19
20 if((f = fopen("/etc/utmp", "r")) == NULL) {
21 fprintf(stderr, "Cannot open /etc/utmp\n");
22 exit(1);
23 }
24 fread((char *)utmp, sizeof(struct utmp), USERS, f);
25 fclose(f);
26 f = stdin;
27 if(argc >= 2) {
28 /* take message from unix file instead of standard input */
29 if((f = fopen(argv[1], "r")) == NULL) {
30 fprintf(stderr,"Cannot open %s\n", argv[1]);
31 exit(1);
32 }
33 }
34 while((i = getc(f)) != EOF) mesg[msize++] = i;
35 fclose(f);
36 sline = ttyslot(2); /* 'utmp' slot no. of sender */
37 if (sline) {
38 for (i=0;c=utmp[sline].ut_name[i];i++)
39 who[i]=c;
40 who[i] = '\0'; /* sender initials */
41 }
42 for(i=0; i<USERS; i++) {
43 p = &utmp[i];
44 if(p->ut_name[0] == 0)
45 continue;
46 sleep(1);
47 sendmes(p->ut_line);
48 }
49 exit(0);
50}
51
52sendmes(tty)
53char *tty;
54{
55 register i;
56 char t[50], buf[BUFSIZ];
57 FILE *f;
58
59 i = fork();
60 if(i == -1) {
61 fprintf(stderr, "Try again\n");
62 return;
63 }
64 if(i)
65 return;
66 strcpy(t, "/dev/");
67 strcat(t, tty);
68
69 if((f = fopen(t, "w")) == NULL) {
70 fprintf(stderr,"cannot open %s\n", t);
71 exit(1);
72 }
73 setbuf(f, buf);
74 fprintf(f, "\a\a\aBroadcast Message from %s (%s) ...\n\n",who,
75 utmp[sline].ut_line);
76 fwrite(mesg, msize, 1, f);
77 exit(0);
78}