date and time created 80/10/01 17:29:40 by bill
[unix-history] / usr / src / usr.bin / write / write.c
CommitLineData
cd784246
BJ
1static char *sccsid = "@(#)write.c 4.1 (Berkeley) %G%";
2/*
3 * write to another user
4 */
5
6#include <stdio.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <signal.h>
10#include <utmp.h>
11#include <time.h>
12
13#define NMAX sizeof(ubuf.ut_name)
14#define LMAX sizeof(ubuf.ut_line)
15
16char *strcat();
17char *strcpy();
18struct utmp ubuf;
19int signum[] = {SIGHUP, SIGINT, SIGQUIT, 0};
20char me[10] = "???";
21char *him;
22char *mytty;
23char histty[32];
24char *histtya;
25char *ttyname();
26char *rindex();
27int logcnt;
28int eof();
29int timout();
30FILE *tf;
31char *getenv();
32
33main(argc, argv)
34char *argv[];
35{
36 struct stat stbuf;
37 register i;
38 register FILE *uf;
39 int c1, c2;
40 long clock = time( 0 );
41 struct tm *localtime();
42 struct tm *localclock = localtime( &clock );
43
44 if(argc < 2) {
45 printf("usage: write user [ttyname]\n");
46 exit(1);
47 }
48 him = argv[1];
49 if(argc > 2)
50 histtya = argv[2];
51 if ((uf = fopen("/etc/utmp", "r")) == NULL) {
52 printf("cannot open /etc/utmp\n");
53 goto cont;
54 }
55 mytty = ttyname(2);
56 if (mytty == NULL) {
57 printf("Can't find your tty\n");
58 exit(1);
59 }
60 mytty = rindex(mytty, '/') + 1;
61 if (histtya) {
62 strcpy(histty, "/dev/");
63 strcat(histty, histtya);
64 }
65 while (fread((char *)&ubuf, sizeof(ubuf), 1, uf) == 1) {
66 if (strcmp(ubuf.ut_line, mytty)==0) {
67 for(i=0; i<NMAX; i++) {
68 c1 = ubuf.ut_name[i];
69 if(c1 == ' ')
70 c1 = 0;
71 me[i] = c1;
72 if(c1 == 0)
73 break;
74 }
75 }
76 if(him[0] != '-' || him[1] != 0)
77 for(i=0; i<NMAX; i++) {
78 c1 = him[i];
79 c2 = ubuf.ut_name[i];
80 if(c1 == 0)
81 if(c2 == 0 || c2 == ' ')
82 break;
83 if(c1 != c2)
84 goto nomat;
85 }
86 logcnt++;
87 if (histty[0]==0) {
88 strcpy(histty, "/dev/");
89 strcat(histty, ubuf.ut_line);
90 }
91 nomat:
92 ;
93 }
94cont:
95 if (logcnt==0 && histty[0]=='\0') {
96 printf("%s not logged in.\n", him);
97 exit(1);
98 }
99 fclose(uf);
100 if (histtya==0 && logcnt > 1) {
101 printf("%s logged more than once\nwriting to %s\n", him, histty+5);
102 }
103 if(histty[0] == 0) {
104 printf(him);
105 if(logcnt)
106 printf(" not on that tty\n"); else
107 printf(" not logged in\n");
108 exit(1);
109 }
110 if (access(histty, 0) < 0) {
111 printf("No such tty\n");
112 exit(1);
113 }
114 signal(SIGALRM, timout);
115 alarm(5);
116 if ((tf = fopen(histty, "w")) == NULL)
117 goto perm;
118 alarm(0);
119 if (fstat(fileno(tf), &stbuf) < 0)
120 goto perm;
121 if ((stbuf.st_mode&02) == 0)
122 goto perm;
123 sigs(eof);
124 fprintf(tf, "\r\nMessage from ");
125#ifdef interdata
126 fprintf(tf, "(Interdata) " );
127#endif
128 fprintf(tf, "%s on %s at %d:%02d ...\r\n\a\a\a"
129 , me, mytty , localclock -> tm_hour , localclock -> tm_min );
130 fflush(tf);
131 for(;;) {
132 char buf[128];
133 i = read(0, buf, 128);
134 if(i <= 0)
135 eof();
136 if(buf[0] == '!') {
137 buf[i] = 0;
138 ex(buf);
139 continue;
140 }
141 write(fileno(tf), buf, i);
142 if ( buf[ i - 1 ] == '\n' )
143 write( fileno( tf ) , "\r" , 1 );
144 }
145
146perm:
147 printf("Permission denied\n");
148 exit(1);
149}
150
151timout()
152{
153
154 printf("Timeout opening their tty\n");
155 exit(1);
156}
157
158eof()
159{
160
161 fprintf(tf, "EOF\r\n");
162 exit(0);
163}
164
165ex(bp)
166char *bp;
167{
168 register i;
169
170 sigs(SIG_IGN);
171 i = fork();
172 if(i < 0) {
173 printf("Try again\n");
174 goto out;
175 }
176 if(i == 0) {
177 sigs((int (*)())0);
178 execl(getenv("SHELL") ? getenv("SHELL") : "/bin/sh", "sh", "-c", bp+1, 0);
179 exit(0);
180 }
181 while(wait((int *)NULL) != i)
182 ;
183 printf("!\n");
184out:
185 sigs(eof);
186}
187
188sigs(sig)
189int (*sig)();
190{
191 register i;
192
193 for(i=0;signum[i];i++)
194 signal(signum[i],sig);
195}