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