Replace all casts of NULL to something with NULL.
[games.git] / usr.bin / 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  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
37  * @(#)write.c  8.1 (Berkeley) 6/6/93
38  * $FreeBSD: src/usr.bin/write/write.c,v 1.12 1999/08/28 01:07:48 peter Exp $
39  * $DragonFly: src/usr.bin/write/write.c,v 1.7 2005/08/30 22:02:36 liamfoy Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/file.h>
45
46 #include <ctype.h>
47 #include <err.h>
48 #include <locale.h>
49 #include <paths.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <signal.h>
55 #include <time.h>
56 #include <unistd.h>
57 #include <utmp.h>
58
59 static void     done(int);
60 static void     do_write(const char *, const char *, uid_t, int *);
61 static void     usage(void);
62 static int      term_chk(const char *, int *, time_t *, int);
63 static void     wr_fputs(unsigned char *s);
64 static void     search_utmp(const char *, char *, size_t, const char *, uid_t);
65 static int      utmp_chk(const char *, const char *);
66
67 int
68 main(int argc, char **argv)
69 {
70         char *cp;
71         time_t atime;
72         uid_t myuid;
73         int msgsok, mymsgok, myttyfd;
74         char tty[MAXPATHLEN], *mytty;
75
76         setlocale(LC_CTYPE, "");
77
78         /* check that sender has write enabled */
79         if (isatty(fileno(stdin)))
80                 myttyfd = fileno(stdin);
81         else if (isatty(fileno(stdout)))
82                 myttyfd = fileno(stdout);
83         else if (isatty(fileno(stderr)))
84                 myttyfd = fileno(stderr);
85         else
86                 errx(1, "can't find your tty");
87         if (!(mytty = ttyname(myttyfd)))
88                 errx(1, "can't find your tty's name");
89         if ((cp = strrchr(mytty, '/')))
90                 mytty = cp + 1;
91         if (term_chk(mytty, &mymsgok, &atime, 1))
92                 exit(1);
93         if (!mymsgok)
94                 warnx("you have write permission turned off (man mesg)");
95
96         myuid = getuid();
97
98         /* check args */
99         switch (argc) {
100         case 2:
101                 search_utmp(argv[1], tty, sizeof tty, mytty, myuid);
102                 do_write(tty, mytty, myuid, &mymsgok);
103                 break;
104         case 3:
105                 if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
106                         argv[2] += strlen(_PATH_DEV);
107                 if (utmp_chk(argv[1], argv[2]))
108                         errx(1, "%s is not logged in on %s", argv[1], argv[2]);
109                 if (term_chk(argv[2], &msgsok, &atime, 1))
110                         exit(1);
111                 if (myuid && !msgsok)
112                         errx(1, "%s has messages disabled on %s", argv[1], argv[2]);
113                 do_write(argv[2], mytty, myuid, &mymsgok);
114                 break;
115         default:
116                 usage();
117         }
118         done(0);
119         return (0);
120 }
121
122 static void
123 usage(void)
124 {
125         fprintf(stderr, "usage: write user [tty]\n");
126         exit(1);
127 }
128
129 /*
130  * utmp_chk - checks that the given user is actually logged in on
131  *     the given tty
132  */
133 static int
134 utmp_chk(const char *user, const char *tty)
135 {
136         struct utmp u;
137         int ufd;
138
139         if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
140                 err(1, "open failed: %s\n", _PATH_UTMP);
141
142         while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) {
143                 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
144                     strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
145                         close(ufd);
146                         return(0);
147                 }
148         }
149
150         close(ufd);
151         return(1);
152 }
153
154 /*
155  * search_utmp - search utmp for the "best" terminal to write to
156  *
157  * Ignores terminals with messages disabled, and of the rest, returns
158  * the one with the most recent access time.  Returns as value the number
159  * of the user's terminals with messages enabled, or -1 if the user is
160  * not logged in at all.
161  *
162  * Special case for writing to yourself - ignore the terminal you're
163  * writing from, unless that's the only terminal with messages enabled.
164  */
165 static void
166 search_utmp(const char *user, char *tty, size_t ttyl, const char *mytty, uid_t myuid)
167 {
168         struct utmp u;
169         time_t bestatime, atime;
170         int ufd, nloggedttys, nttys, msgsok, user_is_me;
171         char atty[UT_LINESIZE + 1];
172
173         if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
174                 err(1, "open failed: %s", _PATH_UTMP);
175
176         nloggedttys = nttys = 0;
177         bestatime = 0;
178         user_is_me = 0;
179         while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
180                 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
181                         ++nloggedttys;
182                         strlcpy(atty, u.ut_line, ttyl);
183                         if (term_chk(atty, &msgsok, &atime, 0))
184                                 continue;       /* bad term? skip */
185                         if (myuid && !msgsok)
186                                 continue;       /* skip ttys with msgs off */
187                         if (strcmp(atty, mytty) == 0) {
188                                 user_is_me = 1;
189                                 continue;       /* don't write to yourself */
190                         }
191                         ++nttys;
192                         if (atime > bestatime) {
193                                 bestatime = atime;
194                                 strlcpy(tty, atty, ttyl);
195                         }
196                 }
197
198         close(ufd);
199         if (nloggedttys == 0)
200                 errx(1, "%s is not logged in", user);
201         if (nttys == 0) {
202                 if (user_is_me) {               /* ok, so write to yourself! */
203                         strlcpy(tty, mytty, ttyl);
204                         return;
205                 }
206                 errx(1, "%s has messages disabled", user);
207         } else if (nttys > 1) {
208                 warnx("%s is logged in more than once; writing to %s", user, tty);
209         }
210 }
211
212 /*
213  * term_chk - check that a terminal exists, and get the message bit
214  *     and the access time
215  */
216 static int
217 term_chk(const char *tty, int *msgsokP, time_t *atimeP, int showerror)
218 {
219         struct stat s;
220         char path[MAXPATHLEN];
221
222         snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
223         if (stat(path, &s) < 0) {
224                 if (showerror)
225                         warn("%s", path);
226                 return(1);
227         }
228         *msgsokP = (s.st_mode & S_IWGRP) != 0;  /* group write bit */
229         *atimeP = s.st_atime;
230         return(0);
231 }
232
233 /*
234  * do_write - actually make the connection
235  */
236 static void
237 do_write(const char *tty, const char *mytty, uid_t myuid, int *mymsgok)
238 {
239         const char *login;
240         char *nows;
241         struct passwd *pwd;
242         time_t now;
243         char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
244
245         /* Determine our login name before we reopen() stdout */
246         if ((login = getlogin()) == NULL) {
247                 if ((pwd = getpwuid(myuid)))
248                         login = pwd->pw_name;
249                 else
250                         login = "???";
251         }
252
253         snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
254         if ((freopen(path, "w", stdout)) == NULL)
255                 err(1, "%s", path);
256
257         signal(SIGINT, done);
258         signal(SIGHUP, done);
259
260         /* print greeting */
261         if (gethostname(host, sizeof(host)) < 0)
262                 strlcpy(host, "???", sizeof host);
263         now = time(NULL);
264         nows = ctime(&now);
265         nows[16] = '\0';
266         printf("\r\n\007\007\007Message from %s@%s on %s at %s (%s %s replies)...\r\n",
267             login, host, mytty, nows + 11, login, *mymsgok == 0 ? "does not accept" : "accepts");
268
269         while (fgets(line, sizeof(line), stdin) != NULL)
270                 wr_fputs(line);
271 }
272
273 /*
274  * done - cleanup and exit
275  */
276 static void
277 done(__unused int n)
278 {
279         printf("EOF\r\n");
280         exit(0);
281 }
282
283 /*
284  * wr_fputs - like fputs(), but makes control characters visible and
285  *     turns \n into \r\n
286  */
287 static void
288 wr_fputs(unsigned char *s)
289 {
290
291 #define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
292
293         for (; *s != '\0'; ++s) {
294                 if (*s == '\n') {
295                         PUTC('\r');
296                 } else if (((*s & 0x80) && *s < 0xA0) ||
297                            /* disable upper controls */
298                            (!isprint(*s) && !isspace(*s) &&
299                             *s != '\a' && *s != '\b')
300                           ) {
301                         if (*s & 0x80) {
302                                 *s &= ~0x80;
303                                 PUTC('M');
304                                 PUTC('-');
305                         }
306                         if (iscntrl(*s)) {
307                                 *s ^= 0x40;
308                                 PUTC('^');
309                         }
310                 }
311                 PUTC(*s);
312         }
313         return;
314 #undef PUTC
315 }