]> git.cameronkatri.com Git - apple_cmds.git/blob - basic_cmds/write/write.c
developer_cmds: All but indent
[apple_cmds.git] / basic_cmds / write / write.c
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __unused static const char copyright[] =
40 "@(#) Copyright (c) 1989, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
42 #endif
43
44 #if 0
45 #ifndef lint
46 static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
47 #endif
48 #endif
49
50 __RCSID("$FreeBSD: src/usr.bin/write/write.c,v 1.17 2002/09/04 23:29:09 dwmalone Exp $");
51
52 #include <sys/param.h>
53 #include <sys/signal.h>
54 #include <sys/stat.h>
55 #include <sys/file.h>
56 #include <sys/time.h>
57 #include <ctype.h>
58 #include <err.h>
59 #include <locale.h>
60 #include <paths.h>
61 #include <pwd.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #include <utmpx.h>
67
68 void done(int);
69 void do_write(char *, char *, uid_t);
70 static void usage(void);
71 int term_chk(char *, int *, time_t *, int);
72 void wr_fputs(unsigned char *s);
73 void search_utmp(char *, char *, char *, uid_t);
74 int utmp_chk(char *, char *);
75
76 int
77 main(int argc, char **argv)
78 {
79 char *cp;
80 time_t atime;
81 uid_t myuid;
82 int msgsok, myttyfd;
83 char tty[MAXPATHLEN], *mytty;
84
85 (void)setlocale(LC_CTYPE, "");
86
87 while (getopt(argc, argv, "") != -1)
88 usage();
89 argc -= optind;
90 argv += optind;
91
92 /* check that sender has write enabled */
93 if (isatty(fileno(stdin)))
94 myttyfd = fileno(stdin);
95 else if (isatty(fileno(stdout)))
96 myttyfd = fileno(stdout);
97 else if (isatty(fileno(stderr)))
98 myttyfd = fileno(stderr);
99 else
100 myttyfd=-1;
101 if(myttyfd == -1)
102 mytty = "tty??";
103 else
104 {
105 if (!(mytty = ttyname(myttyfd)))
106 errx(1, "can't find your tty's name");
107 if ((cp = rindex(mytty, '/')))
108 mytty = cp + 1;
109 if (term_chk(mytty, &msgsok, &atime, 1))
110 exit(1);
111 if (!msgsok)
112 errx(1, "you have write permission turned off");
113 }
114 myuid = getuid();
115
116 /* check args */
117 switch (argc) {
118 case 1:
119 search_utmp(argv[0], tty, mytty, myuid);
120 do_write(tty, mytty, myuid);
121 break;
122 case 2:
123 if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV)))
124 argv[1] += strlen(_PATH_DEV);
125 if (utmp_chk(argv[0], argv[1]))
126 errx(1, "%s is not logged in on %s", argv[0], argv[1]);
127 if (term_chk(argv[1], &msgsok, &atime, 1))
128 exit(1);
129 if (myuid && !msgsok)
130 errx(1, "%s has messages disabled on %s", argv[0], argv[1]);
131 do_write(argv[1], mytty, myuid);
132 break;
133 default:
134 usage();
135 }
136 done(0);
137 return (0);
138 }
139
140 static void
141 usage(void)
142 {
143 (void)fprintf(stderr, "usage: write user [tty]\n");
144 exit(1);
145 }
146
147 /*
148 * utmp_chk - checks that the given user is actually logged in on
149 * the given tty
150 */
151 int
152 utmp_chk(char *user, char *tty)
153 {
154 struct utmpx *u;
155
156 setutxent();
157 while((u = getutxent()) != NULL)
158 if(strncmp(u->ut_line, tty, sizeof(u->ut_line)) == 0) {
159 if(u->ut_type == USER_PROCESS &&
160 strncmp(u->ut_user, user, sizeof(u->ut_user)) == 0) {
161 endutxent();
162 return 0;
163 }
164 break;
165 }
166 endutxent();
167 return 1;
168 }
169
170 /*
171 * search_utmp - search utmp for the "best" terminal to write to
172 *
173 * Ignores terminals with messages disabled, and of the rest, returns
174 * the one with the most recent access time. Returns as value the number
175 * of the user's terminals with messages enabled, or -1 if the user is
176 * not logged in at all.
177 *
178 * Special case for writing to yourself - ignore the terminal you're
179 * writing from, unless that's the only terminal with messages enabled.
180 */
181 void
182 search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
183 {
184 struct utmpx ux, *u;
185 time_t bestatime, atime;
186 int nloggedttys, nttys, msgsok, user_is_me;
187 char atty[sizeof(ux.ut_line) + 1];
188
189 nloggedttys = nttys = 0;
190 bestatime = 0;
191 user_is_me = 0;
192 setutxent();
193 while ((u = getutxent()) != NULL) {
194 if (u->ut_type != USER_PROCESS)
195 continue;
196 if (strncmp(user, u->ut_user, sizeof(u->ut_user)) == 0) {
197 ++nloggedttys;
198 (void)strlcpy(atty, u->ut_line, sizeof(atty));
199 if (term_chk(atty, &msgsok, &atime, 0))
200 continue; /* bad term? skip */
201 if (myuid && !msgsok)
202 continue; /* skip ttys with msgs off */
203 if (strcmp(atty, mytty) == 0) {
204 user_is_me = 1;
205 continue; /* don't write to yourself */
206 }
207 ++nttys;
208 if (atime > bestatime) {
209 bestatime = atime;
210 (void)strcpy(tty, atty);
211 }
212 }
213 }
214
215 endutxent();
216 if (nloggedttys == 0)
217 errx(1, "%s is not logged in", user);
218 if (nttys == 0) {
219 if (user_is_me) { /* ok, so write to yourself! */
220 (void)strcpy(tty, mytty);
221 return;
222 }
223 errx(1, "%s has messages disabled", user);
224 } else if (nttys > 1) {
225 warnx("%s is logged in more than once; writing to %s", user, tty);
226 }
227 }
228
229 /*
230 * term_chk - check that a terminal exists, and get the message bit
231 * and the access time
232 */
233 int
234 term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
235 {
236 struct stat s;
237 char path[MAXPATHLEN];
238
239 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
240 if (stat(path, &s) < 0) {
241 if (showerror)
242 warn("%s", path);
243 return(1);
244 }
245 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
246 *atimeP = s.st_atime;
247 return(0);
248 }
249
250 /*
251 * do_write - actually make the connection
252 */
253 void
254 do_write(char *tty, char *mytty, uid_t myuid)
255 {
256 const char *login;
257 char *nows;
258 struct passwd *pwd;
259 time_t now;
260 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
261
262 /* Determine our login name before the we reopen() stdout */
263 if ((login = getlogin()) == NULL) {
264 if ((pwd = getpwuid(myuid)))
265 login = pwd->pw_name;
266 else
267 login = "???";
268 }
269
270 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
271 if ((freopen(path, "w", stdout)) == NULL)
272 err(1, "%s", path);
273
274 (void)signal(SIGINT, done);
275 (void)signal(SIGHUP, done);
276
277 /* print greeting */
278 if (gethostname(host, sizeof(host)) < 0)
279 (void)strcpy(host, "???");
280 now = time((time_t *)NULL);
281 nows = ctime(&now);
282 nows[16] = '\0';
283 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
284 login, host, mytty, nows + 11);
285
286 while (fgets(line, sizeof(line), stdin) != NULL)
287 wr_fputs((unsigned char *)line);
288 }
289
290 /*
291 * done - cleanup and exit
292 */
293 void
294 done(int n)
295 {
296 (void)printf("EOF\r\n");
297 exit(0);
298 }
299
300 /*
301 * wr_fputs - like fputs(), but makes control characters visible and
302 * turns \n into \r\n
303 */
304 void
305 wr_fputs(unsigned char *s)
306 {
307
308 #define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
309
310 for (; *s != '\0'; ++s) {
311 if (*s == '\n') {
312 PUTC('\r');
313 } else if (((*s & 0x80) && *s < 0xA0) ||
314 /* disable upper controls */
315 (!isprint(*s) && !isspace(*s) &&
316 *s != '\a' && *s != '\b')
317 ) {
318 if (*s & 0x80) {
319 *s &= ~0x80;
320 PUTC('M');
321 PUTC('-');
322 }
323 if (iscntrl(*s)) {
324 *s ^= 0x40;
325 PUTC('^');
326 }
327 }
328 PUTC(*s);
329 }
330 return;
331 #undef PUTC
332 }