2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
52 #include <sys/param.h>
53 #include <sys/signal.h>
65 #define _ULOG_POSIX_NAMES
70 void do_write(char *, char *, uid_t);
71 static void usage(void);
72 int term_chk(char *, int *, time_t *, int);
73 void wr_fputs(unsigned char *s);
74 void search_utmp(char *, char *, char *, uid_t);
75 int utmp_chk(char *, char *);
78 main(int argc, char **argv)
83 char tty[MAXPATHLEN], *mytty;
85 (void)setlocale(LC_CTYPE, "");
87 while (getopt(argc, argv, "") != -1)
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);
100 errx(1, "can't find your tty");
101 if (!(mytty = ttyname(myttyfd)))
102 errx(1, "can't find your tty's name");
103 if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV)))
104 mytty += strlen(_PATH_DEV);
105 if (term_chk(mytty, &msgsok, &atime, 1))
108 errx(1, "you have write permission turned off");
115 search_utmp(argv[0], tty, mytty, myuid);
116 do_write(tty, mytty, myuid);
119 if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV)))
120 argv[1] += strlen(_PATH_DEV);
121 if (utmp_chk(argv[0], argv[1]))
122 errx(1, "%s is not logged in on %s", argv[0], argv[1]);
123 if (term_chk(argv[1], &msgsok, &atime, 1))
125 if (myuid && !msgsok)
126 errx(1, "%s has messages disabled on %s", argv[0], argv[1]);
127 do_write(argv[1], mytty, myuid);
139 (void)fprintf(stderr, "usage: write user [tty]\n");
144 * utmp_chk - checks that the given user is actually logged in on
148 utmp_chk(char *user, char *tty)
152 strncpy(lu.ut_line, tty, sizeof lu.ut_line);
154 while ((u = getutxline(&lu)) != NULL)
155 if (u->ut_type == USER_PROCESS &&
156 strcmp(user, u->ut_user) == 0) {
165 * search_utmp - search utmp for the "best" terminal to write to
167 * Ignores terminals with messages disabled, and of the rest, returns
168 * the one with the most recent access time. Returns as value the number
169 * of the user's terminals with messages enabled, or -1 if the user is
170 * not logged in at all.
172 * Special case for writing to yourself - ignore the terminal you're
173 * writing from, unless that's the only terminal with messages enabled.
176 search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
179 time_t bestatime, atime;
180 int nloggedttys, nttys, msgsok, user_is_me;
182 nloggedttys = nttys = 0;
187 while ((u = getutxent()) != NULL)
188 if (u->ut_type == USER_PROCESS &&
189 strcmp(user, u->ut_user) == 0) {
191 if (term_chk(u->ut_line, &msgsok, &atime, 0))
192 continue; /* bad term? skip */
193 if (myuid && !msgsok)
194 continue; /* skip ttys with msgs off */
195 if (strcmp(u->ut_line, mytty) == 0) {
197 continue; /* don't write to yourself */
200 if (atime > bestatime) {
202 (void)strlcpy(tty, u->ut_line, MAXPATHLEN);
207 if (nloggedttys == 0)
208 errx(1, "%s is not logged in", user);
210 if (user_is_me) { /* ok, so write to yourself! */
211 (void)strlcpy(tty, mytty, MAXPATHLEN);
214 errx(1, "%s has messages disabled", user);
215 } else if (nttys > 1) {
216 warnx("%s is logged in more than once; writing to %s", user, tty);
221 * term_chk - check that a terminal exists, and get the message bit
222 * and the access time
225 term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
228 char path[MAXPATHLEN];
230 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
231 if (stat(path, &s) < 0) {
236 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
237 *atimeP = s.st_atime;
242 * do_write - actually make the connection
245 do_write(char *tty, char *mytty, uid_t myuid)
251 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
253 /* Determine our login name before we reopen() stdout */
254 if ((login = getlogin()) == NULL) {
255 if ((pwd = getpwuid(myuid)))
256 login = pwd->pw_name;
261 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
262 if ((freopen(path, "w", stdout)) == NULL)
265 (void)signal(SIGINT, done);
266 (void)signal(SIGHUP, done);
269 if (gethostname(host, sizeof(host)) < 0)
270 (void)strcpy(host, "???");
271 now = time((time_t *)NULL);
274 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
275 login, host, mytty, nows + 11);
277 while (fgets(line, sizeof(line), stdin) != NULL)
282 * done - cleanup and exit
287 (void)printf("EOF\r\n");
292 * wr_fputs - like fputs(), but makes control characters visible and
296 wr_fputs(unsigned char *s)
299 #define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
301 for (; *s != '\0'; ++s) {
304 } else if (((*s & 0x80) && *s < 0xA0) ||
305 /* disable upper controls */
306 (!isprint(*s) && !isspace(*s) &&
307 *s != '\a' && *s != '\b')