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
--- 33 unchanged lines hidden (view full) ---
42
43#if 0
44#ifndef lint
45static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
46#endif
47#endif
48
49#include <sys/cdefs.h>
50__FBSDID("$FreeBSD: head/usr.bin/write/write.c 200160 2009-12-05 20:22:26Z ed $");
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#define _ULOG_POSIX_NAMES
66#include <ulog.h>
67#include <unistd.h>
68
69void done(int);
70void do_write(char *, char *, uid_t);
71static void usage(void);
72int term_chk(char *, int *, time_t *, int);
73void wr_fputs(unsigned char *s);
74void search_utmp(char *, char *, char *, uid_t);
75int utmp_chk(char *, char *);
--- 66 unchanged lines hidden (view full) ---
142
143/*
144 * utmp_chk - checks that the given user is actually logged in on
145 * the given tty
146 */
147int
148utmp_chk(char *user, char *tty)
149{
150 struct utmpx lu, *u;
151
152 strncpy(lu.ut_line, tty, sizeof lu.ut_line);
153 setutxent();
154 while ((u = getutxline(&lu)) != NULL)
155 if (u->ut_type == USER_PROCESS &&
156 strcmp(user, u->ut_user) == 0) {
157 endutxent();
158 return(0);
159 }
160 endutxent();
161 return(1);
162}
163
164/*
165 * search_utmp - search utmp for the "best" terminal to write to
166 *
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.
171 *
172 * Special case for writing to yourself - ignore the terminal you're
173 * writing from, unless that's the only terminal with messages enabled.
174 */
175void
176search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
177{
178 struct utmpx *u;
179 time_t bestatime, atime;
180 int nloggedttys, nttys, msgsok, user_is_me;
181
182 nloggedttys = nttys = 0;
183 bestatime = 0;
184 user_is_me = 0;
185
186 setutxent();
187 while ((u = getutxent()) != NULL)
188 if (u->ut_type == USER_PROCESS &&
189 strcmp(user, u->ut_user) == 0) {
190 ++nloggedttys;
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) {
196 user_is_me = 1;
197 continue; /* don't write to yourself */
198 }
199 ++nttys;
200 if (atime > bestatime) {
201 bestatime = atime;
202 (void)strlcpy(tty, u->ut_line, MAXPATHLEN);
203 }
204 }
205 endutxent();
206
207 if (nloggedttys == 0)
208 errx(1, "%s is not logged in", user);
209 if (nttys == 0) {
210 if (user_is_me) { /* ok, so write to yourself! */
211 (void)strlcpy(tty, mytty, MAXPATHLEN);
212 return;
213 }
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);
217 }
218}
219
--- 104 unchanged lines hidden ---
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
--- 33 unchanged lines hidden (view full) ---
42
43#if 0
44#ifndef lint
45static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
46#endif
47#endif
48
49#include <sys/cdefs.h>
50__FBSDID("$FreeBSD: head/usr.bin/write/write.c 200160 2009-12-05 20:22:26Z ed $");
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#define _ULOG_POSIX_NAMES
66#include <ulog.h>
67#include <unistd.h>
68
69void done(int);
70void do_write(char *, char *, uid_t);
71static void usage(void);
72int term_chk(char *, int *, time_t *, int);
73void wr_fputs(unsigned char *s);
74void search_utmp(char *, char *, char *, uid_t);
75int utmp_chk(char *, char *);
--- 66 unchanged lines hidden (view full) ---
142
143/*
144 * utmp_chk - checks that the given user is actually logged in on
145 * the given tty
146 */
147int
148utmp_chk(char *user, char *tty)
149{
150 struct utmpx lu, *u;
151
152 strncpy(lu.ut_line, tty, sizeof lu.ut_line);
153 setutxent();
154 while ((u = getutxline(&lu)) != NULL)
155 if (u->ut_type == USER_PROCESS &&
156 strcmp(user, u->ut_user) == 0) {
157 endutxent();
158 return(0);
159 }
160 endutxent();
161 return(1);
162}
163
164/*
165 * search_utmp - search utmp for the "best" terminal to write to
166 *
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.
171 *
172 * Special case for writing to yourself - ignore the terminal you're
173 * writing from, unless that's the only terminal with messages enabled.
174 */
175void
176search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
177{
178 struct utmpx *u;
179 time_t bestatime, atime;
180 int nloggedttys, nttys, msgsok, user_is_me;
181
182 nloggedttys = nttys = 0;
183 bestatime = 0;
184 user_is_me = 0;
185
186 setutxent();
187 while ((u = getutxent()) != NULL)
188 if (u->ut_type == USER_PROCESS &&
189 strcmp(user, u->ut_user) == 0) {
190 ++nloggedttys;
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) {
196 user_is_me = 1;
197 continue; /* don't write to yourself */
198 }
199 ++nttys;
200 if (atime > bestatime) {
201 bestatime = atime;
202 (void)strlcpy(tty, u->ut_line, MAXPATHLEN);
203 }
204 }
205 endutxent();
206
207 if (nloggedttys == 0)
208 errx(1, "%s is not logged in", user);
209 if (nttys == 0) {
210 if (user_is_me) { /* ok, so write to yourself! */
211 (void)strlcpy(tty, mytty, MAXPATHLEN);
212 return;
213 }
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);
217 }
218}
219
--- 104 unchanged lines hidden ---