xref: /original-bsd/usr.bin/write/write.c (revision 6c57d260)
1 static char *sccsid = "@(#)write.c	4.3 (Berkeley) 12/10/80";
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 
16 char	*strcat();
17 char	*strcpy();
18 struct	utmp ubuf;
19 int	signum[] = {SIGHUP, SIGINT, SIGQUIT, 0};
20 char	me[10]	= "???";
21 char	*him;
22 char	*mytty;
23 char	histty[32];
24 char	*histtya;
25 char	*ttyname();
26 char	*rindex();
27 int	logcnt;
28 int	eof();
29 int	timout();
30 FILE	*tf;
31 char	*getenv();
32 
33 main(argc, argv)
34 char *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 	if (stat (mytty, &stbuf) < 0) {
61 		printf ("Can't stat your tty\n");
62 		exit (1);
63 	}
64 	if ((stbuf.st_mode&02) == 0) {
65 		printf ("You have write permission turned off.\n");
66 		exit (1);
67 	}
68 	mytty = rindex(mytty, '/') + 1;
69 	if (histtya) {
70 		strcpy(histty, "/dev/");
71 		strcat(histty, histtya);
72 	}
73 	while (fread((char *)&ubuf, sizeof(ubuf), 1, uf) == 1) {
74 		if (ubuf.ut_name[0] == '\0')
75 			continue;
76 		if (strcmp(ubuf.ut_line, mytty)==0) {
77 			for(i=0; i<NMAX; i++) {
78 				c1 = ubuf.ut_name[i];
79 				if(c1 == ' ')
80 					c1 = 0;
81 				me[i] = c1;
82 				if(c1 == 0)
83 					break;
84 			}
85 		}
86 		if(him[0] != '-' || him[1] != 0)
87 		for(i=0; i<NMAX; i++) {
88 			c1 = him[i];
89 			c2 = ubuf.ut_name[i];
90 			if(c1 == 0)
91 				if(c2 == 0 || c2 == ' ')
92 					break;
93 			if(c1 != c2)
94 				goto nomat;
95 		}
96 		logcnt++;
97 		if (histty[0]==0) {
98 			strcpy(histty, "/dev/");
99 			strcat(histty, ubuf.ut_line);
100 		}
101 	nomat:
102 		;
103 	}
104 cont:
105 	if (logcnt==0 && histty[0]=='\0') {
106 		printf("%s not logged in.\n", him);
107 		exit(1);
108 	}
109 	fclose(uf);
110 	if (histtya==0 && logcnt > 1) {
111 		printf("%s logged more than once\nwriting to %s\n", him, histty+5);
112 	}
113 	if(histty[0] == 0) {
114 		printf(him);
115 		if(logcnt)
116 			printf(" not on that tty\n"); else
117 			printf(" not logged in\n");
118 		exit(1);
119 	}
120 	if (access(histty, 0) < 0) {
121 		printf("No such tty\n");
122 		exit(1);
123 	}
124 	signal(SIGALRM, timout);
125 	alarm(5);
126 	if ((tf = fopen(histty, "w")) == NULL)
127 		goto perm;
128 	alarm(0);
129 	if (fstat(fileno(tf), &stbuf) < 0)
130 		goto perm;
131 	if ((stbuf.st_mode&02) == 0)
132 		goto perm;
133 	sigs(eof);
134 	fprintf(tf, "\r\nMessage from ");
135 #ifdef interdata
136 	fprintf(tf, "(Interdata) " );
137 #endif
138 	fprintf(tf, "%s on %s at %d:%02d ...\r\n"
139 	       , me, mytty , localclock -> tm_hour , localclock -> tm_min );
140 	fflush(tf);
141 	for(;;) {
142 		char buf[128];
143 		i = read(0, buf, 128);
144 		if(i <= 0)
145 			eof();
146 		if(buf[0] == '!') {
147 			buf[i] = 0;
148 			ex(buf);
149 			continue;
150 		}
151 		write(fileno(tf), buf, i);
152 		if ( buf[ i - 1 ] == '\n' )
153 		    write( fileno( tf ) , "\r" , 1 );
154 	}
155 
156 perm:
157 	printf("Permission denied\n");
158 	exit(1);
159 }
160 
161 timout()
162 {
163 
164 	printf("Timeout opening their tty\n");
165 	exit(1);
166 }
167 
168 eof()
169 {
170 
171 	fprintf(tf, "EOF\r\n");
172 	exit(0);
173 }
174 
175 ex(bp)
176 char *bp;
177 {
178 	register i;
179 
180 	sigs(SIG_IGN);
181 	i = fork();
182 	if(i < 0) {
183 		printf("Try again\n");
184 		goto out;
185 	}
186 	if(i == 0) {
187 		sigs((int (*)())0);
188 		execl(getenv("SHELL") ? getenv("SHELL") : "/bin/sh", "sh", "-c", bp+1, 0);
189 		exit(0);
190 	}
191 	while(wait((int *)NULL) != i)
192 		;
193 	printf("!\n");
194 out:
195 	sigs(eof);
196 }
197 
198 sigs(sig)
199 int (*sig)();
200 {
201 	register i;
202 
203 	for(i=0;signum[i];i++)
204 		signal(signum[i],sig);
205 }
206