|
|
1.1 root 1: static char *sccsid = "@(#)wall.c 4.5 (Berkeley) 81/06/12";
2: /*
3: *
4: * This program is not related to David Wall, whose Stanford Ph.D. thesis
5: * is entitled "Mechanisms for Broadcast and Selective Broadcast".
6: */
7:
8: #include <stdio.h>
9: #include <utmp.h>
10: #include <time.h>
11: #include <sys/utsname.h>
12: #include <signal.h>
13: #include <sys/types.h>
14: #include <sys/stat.h>
15: #define USERS 128
16:
17: char mesg[3000];
18: int msize,sline;
19: struct utmp utmp[USERS];
20: char *strcpy(), *strncpy();
21: char *strcat(), *strncat();
22: char who[9] = "greg";
23: long clock, time();
24: struct tm *localtime();
25: struct tm *localclock;
26: struct utsname ourname;
27:
28: main(argc, argv)
29: char *argv[];
30: {
31: register i;
32: register char c;
33: register struct utmp *p;
34: FILE *f;
35: FILE *mf;
36:
37: if((f = fopen("/etc/utmp", "r")) == NULL) {
38: fprintf(stderr, "Cannot open /etc/utmp\n");
39: exit(1);
40: }
41: clock = time( 0 );
42: localclock = localtime( &clock );
43: mf = stdin;
44: if(argc >= 2) {
45: /* take message from unix file instead of standard input */
46: if((mf = fopen(argv[1], "r")) == NULL) {
47: fprintf(stderr,"Cannot open %s\n", argv[1]);
48: exit(1);
49: }
50: }
51: while((i = getc(mf)) != EOF) {
52: if (msize >= sizeof mesg) {
53: fprintf(stderr, "Message too long\n");
54: exit(1);
55: }
56: mesg[msize++] = i;
57: }
58: fclose(mf);
59: sline = ttyslot(2); /* 'utmp' slot no. of sender */
60: fread((char *)utmp, sizeof(struct utmp), USERS, f);
61: fclose(f);
62: if (sline)
63: strncpy(who, utmp[sline].ut_name, sizeof(utmp[sline].ut_name));
64: for(i=0; i<USERS; i++) {
65: register int j;
66: p = &utmp[i];
67: if (p->ut_name[0] == 0)
68: continue;
69: /* reject people not really logged in */
70: j = 0;
71: while (j < sizeof (p->ut_name) && p->ut_name[j] != '*')
72: j++;
73: if (j < sizeof (p->ut_name))
74: continue;
75: sendmes(p->ut_line, sizeof(p->ut_line));
76: }
77: exit(0);
78: }
79:
80: sendmes(tty, len)
81: char *tty;
82: {
83: register i;
84: char t[50], buf[BUFSIZ];
85: register char *cp;
86: register int c, ch;
87: FILE *f;
88:
89: while ((i = fork()) == -1)
90: if (wait((int *)0) == -1) {
91: fprintf(stderr, "Try again\n");
92: return;
93: }
94: if(i)
95: return;
96: strcpy(t, "/dev/");
97: strncat(t, tty, len);
98: t[sizeof("/dev/")+len] = 0;
99:
100: if (ckmode(t) == 0)
101: exit(0);
102: signal(SIGALRM, SIG_DFL); /* blow away if open hangs */
103: alarm(10);
104:
105: if((f = fopen(t, "w")) == NULL) {
106: fprintf(stderr,"cannot open %s\n", t);
107: exit(1);
108: }
109: setbuf(f, buf);
110: uname (&ourname);
111: fprintf(f,
112: "\nBroadcast Message from %s!%s (%.*s) at %d:%02d ...\r\n\n"
113: , ourname.nodename
114: , who
115: , sizeof(utmp[sline].ut_line)
116: , utmp[sline].ut_line
117: , localclock -> tm_hour
118: , localclock -> tm_min
119: );
120: /* fwrite(mesg, msize, 1, f); */
121: for (cp = mesg, c = msize; c-- > 0; cp++) {
122: ch = *cp;
123: if (ch == '\n')
124: putc('\r', f);
125: putc(ch, f);
126: }
127:
128: /*
129: * Bitchin'.
130: */
131:
132: exit(0);
133: }
134:
135: /*
136: * don't write on ttys whose mode is 0
137: * hack to prevent hanging ndcon
138: */
139:
140: ckmode(tty)
141: char *tty;
142: {
143: struct stat sb;
144:
145: if (stat(tty, &sb) < 0)
146: return (1); /* eh? */
147: if (sb.st_mode & ~S_IFMT)
148: return (1);
149: return (0);
150: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.