xref: /freebsd/usr.sbin/ac/ac.c (revision 07df15dd9e6db9065f63cf2ae6bf941b63bf2680)
1ff9c3a32SDavid Greenman /*
2ff9c3a32SDavid Greenman  *      Copyright (c) 1994 Christopher G. Demetriou.
3ff9c3a32SDavid Greenman  *      @(#)Copyright (c) 1994, Simon J. Gerraty.
4ff9c3a32SDavid Greenman  *
5ff9c3a32SDavid Greenman  *      This is free software.  It comes with NO WARRANTY.
6ff9c3a32SDavid Greenman  *      Permission to use, modify and distribute this source code
7ff9c3a32SDavid Greenman  *      is granted subject to the following conditions.
8ff9c3a32SDavid Greenman  *      1/ that the above copyright notice and this notice
9ff9c3a32SDavid Greenman  *      are preserved in all copies and that due credit be given
10ff9c3a32SDavid Greenman  *      to the author.
11ff9c3a32SDavid Greenman  *      2/ that any changes to this code are clearly commented
12ff9c3a32SDavid Greenman  *      as such so that the author does not get blamed for bugs
13ff9c3a32SDavid Greenman  *      other than his own.
14ff9c3a32SDavid Greenman  */
15ff9c3a32SDavid Greenman 
16b51547cfSPhilippe Charnier #include <sys/cdefs.h>
17b51547cfSPhilippe Charnier __FBSDID("$FreeBSD$");
18ff9c3a32SDavid Greenman 
19b42c08d3SEd Schouten #include <sys/queue.h>
20ff9c3a32SDavid Greenman #include <sys/time.h>
21b42c08d3SEd Schouten 
22ff9c3a32SDavid Greenman #include <err.h>
23ff9c3a32SDavid Greenman #include <errno.h>
24497c7558SAndrey A. Chernov #include <langinfo.h>
253efa2f58SPhilippe Charnier #include <locale.h>
26ff9c3a32SDavid Greenman #include <stdio.h>
27ff9c3a32SDavid Greenman #include <stdlib.h>
28ff9c3a32SDavid Greenman #include <string.h>
29db774d60SBruce Evans #include <timeconv.h>
30ff9c3a32SDavid Greenman #include <unistd.h>
31cf4d4e15SEd Schouten #include <utmpx.h>
32ff9c3a32SDavid Greenman 
33ff9c3a32SDavid Greenman /*
34ff9c3a32SDavid Greenman  * this is for our list of currently logged in sessions
35ff9c3a32SDavid Greenman  */
36b42c08d3SEd Schouten struct utmpx_entry {
37b42c08d3SEd Schouten 	SLIST_ENTRY(utmpx_entry) next;
38b42c08d3SEd Schouten 	char		user[sizeof(((struct utmpx *)0)->ut_user)];
39b42c08d3SEd Schouten 	char		id[sizeof(((struct utmpx *)0)->ut_id)];
40b42c08d3SEd Schouten #ifdef CONSOLE_TTY
41b42c08d3SEd Schouten 	char		line[sizeof(((struct utmpx *)0)->ut_line)];
42b42c08d3SEd Schouten #endif
43b42c08d3SEd Schouten 	struct timeval	time;
44ff9c3a32SDavid Greenman };
45ff9c3a32SDavid Greenman 
46ff9c3a32SDavid Greenman /*
47ff9c3a32SDavid Greenman  * this is for our list of users that are accumulating time.
48ff9c3a32SDavid Greenman  */
49b42c08d3SEd Schouten struct user_entry {
50b42c08d3SEd Schouten 	SLIST_ENTRY(user_entry) next;
51b42c08d3SEd Schouten 	char		user[sizeof(((struct utmpx *)0)->ut_user)];
52b42c08d3SEd Schouten 	struct timeval	time;
53ff9c3a32SDavid Greenman };
54ff9c3a32SDavid Greenman 
55ff9c3a32SDavid Greenman /*
56ff9c3a32SDavid Greenman  * this is for chosing whether to ignore a login
57ff9c3a32SDavid Greenman  */
58b42c08d3SEd Schouten struct tty_entry {
59b42c08d3SEd Schouten 	SLIST_ENTRY(tty_entry) next;
60b42c08d3SEd Schouten 	char		line[sizeof(((struct utmpx *)0)->ut_line) + 2];
61b51547cfSPhilippe Charnier 	size_t		len;
62ff9c3a32SDavid Greenman 	int		ret;
63ff9c3a32SDavid Greenman };
64ff9c3a32SDavid Greenman 
65ff9c3a32SDavid Greenman /*
66ff9c3a32SDavid Greenman  * globals - yes yuk
67ff9c3a32SDavid Greenman  */
68ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY
69b42c08d3SEd Schouten static const char *Console = CONSOLE_TTY;
70ff9c3a32SDavid Greenman #endif
71b42c08d3SEd Schouten static struct timeval Total = { 0, 0 };
72b42c08d3SEd Schouten static struct timeval FirstTime = { 0, 0 };
73ff9c3a32SDavid Greenman static int	Flags = 0;
74b42c08d3SEd Schouten static SLIST_HEAD(, utmpx_entry) CurUtmpx = SLIST_HEAD_INITIALIZER(CurUtmpx);
75b42c08d3SEd Schouten static SLIST_HEAD(, user_entry) Users = SLIST_HEAD_INITIALIZER(Users);
76b42c08d3SEd Schouten static SLIST_HEAD(, tty_entry) Ttys = SLIST_HEAD_INITIALIZER(Ttys);
77ff9c3a32SDavid Greenman 
78ff9c3a32SDavid Greenman #define	AC_W	1				/* not _PATH_WTMP */
79ff9c3a32SDavid Greenman #define	AC_D	2				/* daily totals (ignore -p) */
80ff9c3a32SDavid Greenman #define	AC_P	4				/* per-user totals */
81ff9c3a32SDavid Greenman #define	AC_U	8				/* specified users only */
82ff9c3a32SDavid Greenman #define	AC_T	16				/* specified ttys only */
83ff9c3a32SDavid Greenman 
84b42c08d3SEd Schouten static void	ac(const char *);
85b42c08d3SEd Schouten static void	usage(void);
86ff9c3a32SDavid Greenman 
87b42c08d3SEd Schouten static void
88b42c08d3SEd Schouten add_tty(const char *line)
89ff9c3a32SDavid Greenman {
90b42c08d3SEd Schouten 	struct tty_entry *tp;
912adaffb0SGarance A Drosehn 	char *rcp;
92ff9c3a32SDavid Greenman 
93ff9c3a32SDavid Greenman 	Flags |= AC_T;
94ff9c3a32SDavid Greenman 
95b42c08d3SEd Schouten 	if ((tp = malloc(sizeof(*tp))) == NULL)
96c3737d34SPhilippe Charnier 		errx(1, "malloc failed");
97ff9c3a32SDavid Greenman 	tp->len = 0;				/* full match */
98ff9c3a32SDavid Greenman 	tp->ret = 1;				/* do if match */
99b42c08d3SEd Schouten 	if (*line == '!') {			/* don't do if match */
100ff9c3a32SDavid Greenman 		tp->ret = 0;
101b42c08d3SEd Schouten 		line++;
102ff9c3a32SDavid Greenman 	}
103b42c08d3SEd Schouten 	strlcpy(tp->line, line, sizeof(tp->line));
104b42c08d3SEd Schouten 	/* Wildcard. */
105b42c08d3SEd Schouten 	if ((rcp = strchr(tp->line, '*')) != NULL) {
106ff9c3a32SDavid Greenman 		*rcp = '\0';
107b42c08d3SEd Schouten 		/* Match len bytes only. */
108b42c08d3SEd Schouten 		tp->len = strlen(tp->line);
109ff9c3a32SDavid Greenman 	}
110b42c08d3SEd Schouten 	SLIST_INSERT_HEAD(&Ttys, tp, next);
111ff9c3a32SDavid Greenman }
112ff9c3a32SDavid Greenman 
113ff9c3a32SDavid Greenman /*
114ff9c3a32SDavid Greenman  * should we process the named tty?
115ff9c3a32SDavid Greenman  */
116b42c08d3SEd Schouten static int
117b42c08d3SEd Schouten do_tty(const char *line)
118ff9c3a32SDavid Greenman {
119b42c08d3SEd Schouten 	struct tty_entry *tp;
120ff9c3a32SDavid Greenman 	int def_ret = 0;
121ff9c3a32SDavid Greenman 
122b42c08d3SEd Schouten 	SLIST_FOREACH(tp, &Ttys, next) {
123ff9c3a32SDavid Greenman 		if (tp->ret == 0)		/* specific don't */
124ff9c3a32SDavid Greenman 			def_ret = 1;		/* default do */
125ff9c3a32SDavid Greenman 		if (tp->len != 0) {
126b42c08d3SEd Schouten 			if (strncmp(line, tp->line, tp->len) == 0)
127ff9c3a32SDavid Greenman 				return tp->ret;
128ff9c3a32SDavid Greenman 		} else {
129b42c08d3SEd Schouten 			if (strncmp(line, tp->line, sizeof(tp->line)) == 0)
130ff9c3a32SDavid Greenman 				return tp->ret;
131ff9c3a32SDavid Greenman 		}
132ff9c3a32SDavid Greenman 	}
133b42c08d3SEd Schouten 	return (def_ret);
134ff9c3a32SDavid Greenman }
135ff9c3a32SDavid Greenman 
136ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY
137ff9c3a32SDavid Greenman /*
138ff9c3a32SDavid Greenman  * is someone logged in on Console?
139ff9c3a32SDavid Greenman  */
140b42c08d3SEd Schouten static int
141b42c08d3SEd Schouten on_console(void)
142ff9c3a32SDavid Greenman {
143b42c08d3SEd Schouten 	struct utmpx_entry *up;
144ff9c3a32SDavid Greenman 
145b42c08d3SEd Schouten 	SLIST_FOREACH(up, &CurUtmpx, next)
146b42c08d3SEd Schouten 		if (strcmp(up->line, Console) == 0)
147b42c08d3SEd Schouten 			return (1);
148b42c08d3SEd Schouten 	return (0);
149ff9c3a32SDavid Greenman }
150ff9c3a32SDavid Greenman #endif
151ff9c3a32SDavid Greenman 
152ff9c3a32SDavid Greenman /*
153b42c08d3SEd Schouten  * Update user's login time.
154b42c08d3SEd Schouten  * If no entry for this user is found, a new entry is inserted into the
155b42c08d3SEd Schouten  * list alphabetically.
156ff9c3a32SDavid Greenman  */
157b42c08d3SEd Schouten static void
158b42c08d3SEd Schouten update_user(const char *user, struct timeval secs)
159ff9c3a32SDavid Greenman {
160b42c08d3SEd Schouten 	struct user_entry *up, *aup;
161b42c08d3SEd Schouten 	int c;
162ff9c3a32SDavid Greenman 
163b42c08d3SEd Schouten 	aup = NULL;
164b42c08d3SEd Schouten 	SLIST_FOREACH(up, &Users, next) {
165b42c08d3SEd Schouten 		c = strcmp(up->user, user);
166b42c08d3SEd Schouten 		if (c == 0) {
167b42c08d3SEd Schouten 			timeradd(&up->time, &secs, &up->time);
168b42c08d3SEd Schouten 			timeradd(&Total, &secs, &Total);
169b42c08d3SEd Schouten 			return;
170b42c08d3SEd Schouten 		} else if (c > 0)
171b42c08d3SEd Schouten 			break;
172b42c08d3SEd Schouten 		aup = up;
173ff9c3a32SDavid Greenman 	}
174ff9c3a32SDavid Greenman 	/*
175ff9c3a32SDavid Greenman 	 * not found so add new user unless specified users only
176ff9c3a32SDavid Greenman 	 */
177ff9c3a32SDavid Greenman 	if (Flags & AC_U)
178b42c08d3SEd Schouten 		return;
179ff9c3a32SDavid Greenman 
180b42c08d3SEd Schouten 	if ((up = malloc(sizeof(*up))) == NULL)
181c3737d34SPhilippe Charnier 		errx(1, "malloc failed");
182b42c08d3SEd Schouten 	if (aup == NULL)
183b42c08d3SEd Schouten 		SLIST_INSERT_HEAD(&Users, up, next);
184b42c08d3SEd Schouten 	else
185b42c08d3SEd Schouten 		SLIST_INSERT_AFTER(aup, up, next);
186b42c08d3SEd Schouten 	strlcpy(up->user, user, sizeof(up->user));
187b42c08d3SEd Schouten 	up->time = secs;
188b42c08d3SEd Schouten 	timeradd(&Total, &secs, &Total);
189ff9c3a32SDavid Greenman }
190ff9c3a32SDavid Greenman 
191ff9c3a32SDavid Greenman int
1927f761c52SGarance A Drosehn main(int argc, char *argv[])
193ff9c3a32SDavid Greenman {
194cf4d4e15SEd Schouten 	const char *wtmpf = NULL;
195ff9c3a32SDavid Greenman 	int c;
196ff9c3a32SDavid Greenman 
1975877948cSAndrey A. Chernov 	(void) setlocale(LC_TIME, "");
1985877948cSAndrey A. Chernov 
199b42c08d3SEd Schouten 	while ((c = getopt(argc, argv, "c:dpt:w:")) != -1) {
200ff9c3a32SDavid Greenman 		switch (c) {
201ff9c3a32SDavid Greenman 		case 'c':
202ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY
203ff9c3a32SDavid Greenman 			Console = optarg;
204ff9c3a32SDavid Greenman #else
205ff9c3a32SDavid Greenman 			usage();		/* XXX */
206ff9c3a32SDavid Greenman #endif
207ff9c3a32SDavid Greenman 			break;
208ff9c3a32SDavid Greenman 		case 'd':
209ff9c3a32SDavid Greenman 			Flags |= AC_D;
210ff9c3a32SDavid Greenman 			break;
211ff9c3a32SDavid Greenman 		case 'p':
212ff9c3a32SDavid Greenman 			Flags |= AC_P;
213ff9c3a32SDavid Greenman 			break;
214ff9c3a32SDavid Greenman 		case 't':			/* only do specified ttys */
215ff9c3a32SDavid Greenman 			add_tty(optarg);
216ff9c3a32SDavid Greenman 			break;
217ff9c3a32SDavid Greenman 		case 'w':
218cf4d4e15SEd Schouten 			Flags |= AC_W;
219cf4d4e15SEd Schouten 			wtmpf = optarg;
220ff9c3a32SDavid Greenman 			break;
221ff9c3a32SDavid Greenman 		case '?':
222ff9c3a32SDavid Greenman 		default:
223ff9c3a32SDavid Greenman 			usage();
224ff9c3a32SDavid Greenman 			break;
225ff9c3a32SDavid Greenman 		}
226ff9c3a32SDavid Greenman 	}
227ff9c3a32SDavid Greenman 	if (optind < argc) {
228ff9c3a32SDavid Greenman 		/*
229ff9c3a32SDavid Greenman 		 * initialize user list
230ff9c3a32SDavid Greenman 		 */
231ff9c3a32SDavid Greenman 		for (; optind < argc; optind++) {
232b42c08d3SEd Schouten 			update_user(argv[optind], (struct timeval){ 0, 0 });
233ff9c3a32SDavid Greenman 		}
234ff9c3a32SDavid Greenman 		Flags |= AC_U;			/* freeze user list */
235ff9c3a32SDavid Greenman 	}
236ff9c3a32SDavid Greenman 	if (Flags & AC_D)
237ff9c3a32SDavid Greenman 		Flags &= ~AC_P;
238cf4d4e15SEd Schouten 	ac(wtmpf);
239ff9c3a32SDavid Greenman 
240b42c08d3SEd Schouten 	return (0);
241ff9c3a32SDavid Greenman }
242ff9c3a32SDavid Greenman 
243ff9c3a32SDavid Greenman /*
244ff9c3a32SDavid Greenman  * print login time in decimal hours
245ff9c3a32SDavid Greenman  */
246b42c08d3SEd Schouten static void
247b42c08d3SEd Schouten show(const char *user, struct timeval secs)
248ff9c3a32SDavid Greenman {
249cf4d4e15SEd Schouten 	(void)printf("\t%-*s %8.2f\n",
250b42c08d3SEd Schouten 	    (int)sizeof(((struct user_entry *)0)->user), user,
251b42c08d3SEd Schouten 	    (double)secs.tv_sec / 3600);
252ff9c3a32SDavid Greenman }
253ff9c3a32SDavid Greenman 
254b42c08d3SEd Schouten static void
255b42c08d3SEd Schouten show_users(void)
256ff9c3a32SDavid Greenman {
257b42c08d3SEd Schouten 	struct user_entry *lp;
258ff9c3a32SDavid Greenman 
259b42c08d3SEd Schouten 	SLIST_FOREACH(lp, &Users, next)
260b42c08d3SEd Schouten 		show(lp->user, lp->time);
261ff9c3a32SDavid Greenman }
262ff9c3a32SDavid Greenman 
263ff9c3a32SDavid Greenman /*
264ff9c3a32SDavid Greenman  * print total login time for 24hr period in decimal hours
265ff9c3a32SDavid Greenman  */
266b42c08d3SEd Schouten static void
267b42c08d3SEd Schouten show_today(struct timeval today)
268ff9c3a32SDavid Greenman {
269b42c08d3SEd Schouten 	struct user_entry *up;
270b42c08d3SEd Schouten 	struct utmpx_entry *lp;
271ff9c3a32SDavid Greenman 	char date[64];
272*07df15ddSEd Schouten 	struct timeval diff, total = { 0, 0 }, usec = { 0, 1 }, yesterday;
273497c7558SAndrey A. Chernov 	static int d_first = -1;
274ff9c3a32SDavid Greenman 
275497c7558SAndrey A. Chernov 	if (d_first < 0)
276497c7558SAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
277b42c08d3SEd Schouten 	timersub(&today, &usec, &yesterday);
278497c7558SAndrey A. Chernov 	(void)strftime(date, sizeof(date),
279497c7558SAndrey A. Chernov 		       d_first ? "%e %b  total" : "%b %e  total",
280b42c08d3SEd Schouten 		       localtime(&yesterday.tv_sec));
281ff9c3a32SDavid Greenman 
282b42c08d3SEd Schouten 	SLIST_FOREACH(lp, &CurUtmpx, next) {
283*07df15ddSEd Schouten 		timersub(&today, &lp->time, &diff);
284*07df15ddSEd Schouten 		update_user(lp->user, diff);
285b42c08d3SEd Schouten 		/* As if they just logged in. */
286*07df15ddSEd Schouten 		lp->time = today;
287ff9c3a32SDavid Greenman 	}
288b42c08d3SEd Schouten 	SLIST_FOREACH(up, &Users, next) {
289*07df15ddSEd Schouten 		timeradd(&total, &up->time, &total);
290b42c08d3SEd Schouten 		/* For next day. */
291b42c08d3SEd Schouten 		timerclear(&up->time);
292ff9c3a32SDavid Greenman 	}
293*07df15ddSEd Schouten 	if (timerisset(&total))
294*07df15ddSEd Schouten 		(void)printf("%s %11.2f\n", date, (double)total.tv_sec / 3600);
295ff9c3a32SDavid Greenman }
296ff9c3a32SDavid Greenman 
297ff9c3a32SDavid Greenman /*
298b42c08d3SEd Schouten  * Log a user out and update their times.
299b42c08d3SEd Schouten  * If ut_type is BOOT_TIME or SHUTDOWN_TIME, we log all users out as the
300b42c08d3SEd Schouten  * system has been shut down.
301ff9c3a32SDavid Greenman  */
302b42c08d3SEd Schouten static void
303b42c08d3SEd Schouten log_out(const struct utmpx *up)
304ff9c3a32SDavid Greenman {
305b42c08d3SEd Schouten 	struct utmpx_entry *lp, *lp2, *tlp;
306b42c08d3SEd Schouten 	struct timeval secs;
307ff9c3a32SDavid Greenman 
308b42c08d3SEd Schouten 	for (lp = SLIST_FIRST(&CurUtmpx), lp2 = NULL; lp != NULL;)
309cf4d4e15SEd Schouten 		if (up->ut_type == BOOT_TIME || up->ut_type == SHUTDOWN_TIME ||
310cf4d4e15SEd Schouten 		    (up->ut_type == DEAD_PROCESS &&
311b42c08d3SEd Schouten 		    memcmp(lp->id, up->ut_id, sizeof(up->ut_id)) == 0)) {
312b42c08d3SEd Schouten 			timersub(&up->ut_tv, &lp->time, &secs);
313b42c08d3SEd Schouten 			update_user(lp->user, secs);
314ff9c3a32SDavid Greenman 			/*
315ff9c3a32SDavid Greenman 			 * now lose it
316ff9c3a32SDavid Greenman 			 */
317ff9c3a32SDavid Greenman 			tlp = lp;
318b42c08d3SEd Schouten 			lp = SLIST_NEXT(lp, next);
319b42c08d3SEd Schouten 			if (lp2 == NULL)
320b42c08d3SEd Schouten 				SLIST_REMOVE_HEAD(&CurUtmpx, next);
321b42c08d3SEd Schouten 			else
322b42c08d3SEd Schouten 				SLIST_REMOVE_AFTER(lp2, next);
323ff9c3a32SDavid Greenman 			free(tlp);
324ff9c3a32SDavid Greenman 		} else {
325ff9c3a32SDavid Greenman 			lp2 = lp;
326b42c08d3SEd Schouten 			lp = SLIST_NEXT(lp, next);
327ff9c3a32SDavid Greenman 		}
328ff9c3a32SDavid Greenman }
329ff9c3a32SDavid Greenman 
330ff9c3a32SDavid Greenman /*
331ff9c3a32SDavid Greenman  * if do_tty says ok, login a user
332ff9c3a32SDavid Greenman  */
333b42c08d3SEd Schouten static void
334b42c08d3SEd Schouten log_in(struct utmpx *up)
335ff9c3a32SDavid Greenman {
336b42c08d3SEd Schouten 	struct utmpx_entry *lp;
337ff9c3a32SDavid Greenman 
338ff9c3a32SDavid Greenman 	/*
339ff9c3a32SDavid Greenman 	 * this could be a login. if we're not dealing with
340ff9c3a32SDavid Greenman 	 * the console name, say it is.
341ff9c3a32SDavid Greenman 	 *
342ff9c3a32SDavid Greenman 	 * If we are, and if ut_host==":0.0" we know that it
343ff9c3a32SDavid Greenman 	 * isn't a real login. _But_ if we have not yet recorded
344ff9c3a32SDavid Greenman 	 * someone being logged in on Console - due to the wtmp
345ff9c3a32SDavid Greenman 	 * file starting after they logged in, we'll pretend they
346ff9c3a32SDavid Greenman 	 * logged in, at the start of the wtmp file.
347ff9c3a32SDavid Greenman 	 */
348ff9c3a32SDavid Greenman 
349ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY
350ff9c3a32SDavid Greenman 	if (up->ut_host[0] == ':') {
351ff9c3a32SDavid Greenman 		/*
352ff9c3a32SDavid Greenman 		 * SunOS 4.0.2 does not treat ":0.0" as special but we
353ff9c3a32SDavid Greenman 		 * do.
354ff9c3a32SDavid Greenman 		 */
355b42c08d3SEd Schouten 		if (on_console())
356b42c08d3SEd Schouten 			return;
357ff9c3a32SDavid Greenman 		/*
358ff9c3a32SDavid Greenman 		 * ok, no recorded login, so they were here when wtmp
359ff9c3a32SDavid Greenman 		 * started!  Adjust ut_time!
360ff9c3a32SDavid Greenman 		 */
361b42c08d3SEd Schouten 		up->ut_tv = FirstTime;
362ff9c3a32SDavid Greenman 		/*
363ff9c3a32SDavid Greenman 		 * this allows us to pick the right logout
364ff9c3a32SDavid Greenman 		 */
365fd96447aSKris Kennaway 		strlcpy(up->ut_line, Console, sizeof(up->ut_line));
366ff9c3a32SDavid Greenman 	}
367ff9c3a32SDavid Greenman #endif
368ff9c3a32SDavid Greenman 	/*
369ff9c3a32SDavid Greenman 	 * If we are doing specified ttys only, we ignore
370ff9c3a32SDavid Greenman 	 * anything else.
371ff9c3a32SDavid Greenman 	 */
372b42c08d3SEd Schouten 	if (Flags & AC_T && !do_tty(up->ut_line))
373b42c08d3SEd Schouten 		return;
374ff9c3a32SDavid Greenman 
375ff9c3a32SDavid Greenman 	/*
376ff9c3a32SDavid Greenman 	 * go ahead and log them in
377ff9c3a32SDavid Greenman 	 */
378b42c08d3SEd Schouten 	if ((lp = malloc(sizeof(*lp))) == NULL)
379c3737d34SPhilippe Charnier 		errx(1, "malloc failed");
380b42c08d3SEd Schouten 	SLIST_INSERT_HEAD(&CurUtmpx, lp, next);
381b42c08d3SEd Schouten 	strlcpy(lp->user, up->ut_user, sizeof(lp->user));
382b42c08d3SEd Schouten 	memcpy(lp->id, up->ut_id, sizeof(lp->id));
383b42c08d3SEd Schouten #ifdef CONSOLE_TTY
384b42c08d3SEd Schouten 	memcpy(lp->line, up->ut_line, sizeof(lp->line));
385ff9c3a32SDavid Greenman #endif
386b42c08d3SEd Schouten 	lp->time = up->ut_tv;
387ff9c3a32SDavid Greenman }
388ff9c3a32SDavid Greenman 
389b42c08d3SEd Schouten static void
390cf4d4e15SEd Schouten ac(const char *file)
391ff9c3a32SDavid Greenman {
392b42c08d3SEd Schouten 	struct utmpx_entry *lp;
393cf4d4e15SEd Schouten 	struct utmpx *usr, usht;
394ff9c3a32SDavid Greenman 	struct tm *ltm;
395b42c08d3SEd Schouten 	struct timeval prev_secs, ut_timecopy, secs, clock_shift, now;
396b42c08d3SEd Schouten 	int day, rfound;
397ff9c3a32SDavid Greenman 
398e3b218cdSGarance A Drosehn 	day = -1;
399b42c08d3SEd Schouten 	timerclear(&prev_secs);	/* Minimum acceptable date == 1970. */
400b42c08d3SEd Schouten 	timerclear(&secs);
401b42c08d3SEd Schouten 	timerclear(&clock_shift);
402b42c08d3SEd Schouten 	rfound = 0;
403cf4d4e15SEd Schouten 	if (setutxdb(UTXDB_LOG, file) != 0)
404cf4d4e15SEd Schouten 		err(1, "%s", file);
405cf4d4e15SEd Schouten 	while ((usr = getutxent()) != NULL) {
406e3b218cdSGarance A Drosehn 		rfound++;
407b42c08d3SEd Schouten 		ut_timecopy = usr->ut_tv;
408b42c08d3SEd Schouten 		/* Don't let the time run backwards. */
409b42c08d3SEd Schouten 		if (timercmp(&ut_timecopy, &prev_secs, <))
410b42c08d3SEd Schouten 			ut_timecopy = prev_secs;
411e3b218cdSGarance A Drosehn 		prev_secs = ut_timecopy;
412e3b218cdSGarance A Drosehn 
413b42c08d3SEd Schouten 		if (!timerisset(&FirstTime))
414e3b218cdSGarance A Drosehn 			FirstTime = ut_timecopy;
415ff9c3a32SDavid Greenman 		if (Flags & AC_D) {
416b42c08d3SEd Schouten 			ltm = localtime(&ut_timecopy.tv_sec);
417ff9c3a32SDavid Greenman 			if (day >= 0 && day != ltm->tm_yday) {
418ff9c3a32SDavid Greenman 				day = ltm->tm_yday;
419ff9c3a32SDavid Greenman 				/*
420ff9c3a32SDavid Greenman 				 * print yesterday's total
421ff9c3a32SDavid Greenman 				 */
422e3b218cdSGarance A Drosehn 				secs = ut_timecopy;
423b42c08d3SEd Schouten 				secs.tv_sec -= ltm->tm_sec;
424b42c08d3SEd Schouten 				secs.tv_sec -= 60 * ltm->tm_min;
425b42c08d3SEd Schouten 				secs.tv_sec -= 3600 * ltm->tm_hour;
426b42c08d3SEd Schouten 				secs.tv_usec = 0;
427b42c08d3SEd Schouten 				show_today(secs);
428ff9c3a32SDavid Greenman 			} else
429ff9c3a32SDavid Greenman 				day = ltm->tm_yday;
430ff9c3a32SDavid Greenman 		}
431cf4d4e15SEd Schouten 		switch(usr->ut_type) {
432cf4d4e15SEd Schouten 		case OLD_TIME:
433b42c08d3SEd Schouten 			clock_shift = ut_timecopy;
434ff9c3a32SDavid Greenman 			break;
435cf4d4e15SEd Schouten 		case NEW_TIME:
436b42c08d3SEd Schouten 			timersub(&clock_shift, &ut_timecopy, &clock_shift);
437ff9c3a32SDavid Greenman 			/*
438ff9c3a32SDavid Greenman 			 * adjust time for those logged in
439ff9c3a32SDavid Greenman 			 */
440b42c08d3SEd Schouten 			SLIST_FOREACH(lp, &CurUtmpx, next)
441b42c08d3SEd Schouten 				timersub(&lp->time, &clock_shift, &lp->time);
442ff9c3a32SDavid Greenman 			break;
443cf4d4e15SEd Schouten 		case BOOT_TIME:
444cf4d4e15SEd Schouten 		case SHUTDOWN_TIME:
445b42c08d3SEd Schouten 			log_out(usr);
446e3b218cdSGarance A Drosehn 			FirstTime = ut_timecopy; /* shouldn't be needed */
447ff9c3a32SDavid Greenman 			break;
448cf4d4e15SEd Schouten 		case USER_PROCESS:
449ff9c3a32SDavid Greenman 			/*
450b42c08d3SEd Schouten 			 * If they came in on pts/..., then it is only
451b42c08d3SEd Schouten 			 * a login session if the ut_host field is non-empty.
452ff9c3a32SDavid Greenman 			 */
453b42c08d3SEd Schouten 			if (strncmp(usr->ut_line, "pts/", 4) != 0 ||
454cf4d4e15SEd Schouten 			    *usr->ut_host != '\0')
455b42c08d3SEd Schouten 				log_in(usr);
456cf4d4e15SEd Schouten 			break;
457cf4d4e15SEd Schouten 		case DEAD_PROCESS:
458b42c08d3SEd Schouten 			log_out(usr);
459ff9c3a32SDavid Greenman 			break;
460ff9c3a32SDavid Greenman 		}
461ff9c3a32SDavid Greenman 	}
462cf4d4e15SEd Schouten 	endutxent();
463b42c08d3SEd Schouten 	(void)gettimeofday(&now, NULL);
464b42c08d3SEd Schouten 	if (Flags & AC_W)
465b42c08d3SEd Schouten 		usht.ut_tv = ut_timecopy;
4669c8d0b96SEd Schouten 	else
467b42c08d3SEd Schouten 		usht.ut_tv = now;
468cf4d4e15SEd Schouten 	usht.ut_type = SHUTDOWN_TIME;
469ff9c3a32SDavid Greenman 
470ff9c3a32SDavid Greenman 	if (Flags & AC_D) {
471b42c08d3SEd Schouten 		ltm = localtime(&ut_timecopy.tv_sec);
472ff9c3a32SDavid Greenman 		if (day >= 0 && day != ltm->tm_yday) {
473ff9c3a32SDavid Greenman 			/*
474ff9c3a32SDavid Greenman 			 * print yesterday's total
475ff9c3a32SDavid Greenman 			 */
476e3b218cdSGarance A Drosehn 			secs = ut_timecopy;
477b42c08d3SEd Schouten 			secs.tv_sec -= ltm->tm_sec;
478b42c08d3SEd Schouten 			secs.tv_sec -= 60 * ltm->tm_min;
479b42c08d3SEd Schouten 			secs.tv_sec -= 3600 * ltm->tm_hour;
480b42c08d3SEd Schouten 			secs.tv_usec = 0;
481b42c08d3SEd Schouten 			show_today(secs);
482ff9c3a32SDavid Greenman 		}
483ff9c3a32SDavid Greenman 	}
484ff9c3a32SDavid Greenman 	/*
485ff9c3a32SDavid Greenman 	 * anyone still logged in gets time up to now
486ff9c3a32SDavid Greenman 	 */
487b42c08d3SEd Schouten 	log_out(&usht);
488ff9c3a32SDavid Greenman 
489ff9c3a32SDavid Greenman 	if (Flags & AC_D)
490b42c08d3SEd Schouten 		show_today(now);
491ff9c3a32SDavid Greenman 	else {
492ff9c3a32SDavid Greenman 		if (Flags & AC_P)
493b42c08d3SEd Schouten 			show_users();
494ff9c3a32SDavid Greenman 		show("total", Total);
495ff9c3a32SDavid Greenman 	}
496ff9c3a32SDavid Greenman }
497ff9c3a32SDavid Greenman 
498b42c08d3SEd Schouten static void
4997f761c52SGarance A Drosehn usage(void)
500ff9c3a32SDavid Greenman {
501ff9c3a32SDavid Greenman 	(void)fprintf(stderr,
502ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY
503ff9c3a32SDavid Greenman 	    "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
504ff9c3a32SDavid Greenman #else
505ff9c3a32SDavid Greenman 	    "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");
506ff9c3a32SDavid Greenman #endif
507ff9c3a32SDavid Greenman 	exit(1);
508ff9c3a32SDavid Greenman }
509