2 * Copyright (c) 1994 Christopher G. Demetriou.
3 * @(#)Copyright (c) 1994, Simon J. Gerraty.
5 * This is free software. It comes with NO WARRANTY.
6 * Permission to use, modify and distribute this source code
7 * is granted subject to the following conditions.
8 * 1/ that the above copyright notice and this notice
9 * are preserved in all copies and that due credit be given
11 * 2/ that any changes to this code are clearly commented
12 * as such so that the author does not get blamed for bugs
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
19 #include <sys/queue.h>
34 * this is for our list of currently logged in sessions
37 SLIST_ENTRY(utmpx_entry) next;
38 char user[sizeof(((struct utmpx *)0)->ut_user)];
39 char id[sizeof(((struct utmpx *)0)->ut_id)];
41 char line[sizeof(((struct utmpx *)0)->ut_line)];
47 * this is for our list of users that are accumulating time.
50 SLIST_ENTRY(user_entry) next;
51 char user[sizeof(((struct utmpx *)0)->ut_user)];
56 * this is for chosing whether to ignore a login
59 SLIST_ENTRY(tty_entry) next;
60 char line[sizeof(((struct utmpx *)0)->ut_line) + 2];
69 static const char *Console = CONSOLE_TTY;
71 static struct timeval Total = { 0, 0 };
72 static struct timeval FirstTime = { 0, 0 };
74 static SLIST_HEAD(, utmpx_entry) CurUtmpx = SLIST_HEAD_INITIALIZER(CurUtmpx);
75 static SLIST_HEAD(, user_entry) Users = SLIST_HEAD_INITIALIZER(Users);
76 static SLIST_HEAD(, tty_entry) Ttys = SLIST_HEAD_INITIALIZER(Ttys);
78 #define AC_W 1 /* not _PATH_WTMP */
79 #define AC_D 2 /* daily totals (ignore -p) */
80 #define AC_P 4 /* per-user totals */
81 #define AC_U 8 /* specified users only */
82 #define AC_T 16 /* specified ttys only */
84 static void ac(const char *);
85 static void usage(void);
88 add_tty(const char *line)
95 if ((tp = malloc(sizeof(*tp))) == NULL)
96 errx(1, "malloc failed");
97 tp->len = 0; /* full match */
98 tp->ret = 1; /* do if match */
99 if (*line == '!') { /* don't do if match */
103 strlcpy(tp->line, line, sizeof(tp->line));
105 if ((rcp = strchr(tp->line, '*')) != NULL) {
107 /* Match len bytes only. */
108 tp->len = strlen(tp->line);
110 SLIST_INSERT_HEAD(&Ttys, tp, next);
114 * should we process the named tty?
117 do_tty(const char *line)
119 struct tty_entry *tp;
122 SLIST_FOREACH(tp, &Ttys, next) {
123 if (tp->ret == 0) /* specific don't */
124 def_ret = 1; /* default do */
126 if (strncmp(line, tp->line, tp->len) == 0)
129 if (strncmp(line, tp->line, sizeof(tp->line)) == 0)
138 * is someone logged in on Console?
143 struct utmpx_entry *up;
145 SLIST_FOREACH(up, &CurUtmpx, next)
146 if (strcmp(up->line, Console) == 0)
153 * Update user's login time.
154 * If no entry for this user is found, a new entry is inserted into the
155 * list alphabetically.
158 update_user(const char *user, struct timeval secs)
160 struct user_entry *up, *aup;
164 SLIST_FOREACH(up, &Users, next) {
165 c = strcmp(up->user, user);
167 timeradd(&up->time, &secs, &up->time);
168 timeradd(&Total, &secs, &Total);
175 * not found so add new user unless specified users only
180 if ((up = malloc(sizeof(*up))) == NULL)
181 errx(1, "malloc failed");
183 SLIST_INSERT_HEAD(&Users, up, next);
185 SLIST_INSERT_AFTER(aup, up, next);
186 strlcpy(up->user, user, sizeof(up->user));
188 timeradd(&Total, &secs, &Total);
192 main(int argc, char *argv[])
194 const char *wtmpf = NULL;
197 (void) setlocale(LC_TIME, "");
199 while ((c = getopt(argc, argv, "c:dpt:w:")) != -1) {
214 case 't': /* only do specified ttys */
229 * initialize user list
231 for (; optind < argc; optind++) {
232 update_user(argv[optind], (struct timeval){ 0, 0 });
234 Flags |= AC_U; /* freeze user list */
244 * print login time in decimal hours
247 show(const char *user, struct timeval secs)
249 (void)printf("\t%-*s %8.2f\n",
250 (int)sizeof(((struct user_entry *)0)->user), user,
251 (double)secs.tv_sec / 3600);
257 struct user_entry *lp;
259 SLIST_FOREACH(lp, &Users, next)
260 show(lp->user, lp->time);
264 * print total login time for 24hr period in decimal hours
267 show_today(struct timeval today)
269 struct user_entry *up;
270 struct utmpx_entry *lp;
272 struct timeval diff, total = { 0, 0 }, usec = { 0, 1 }, yesterday;
273 static int d_first = -1;
276 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
277 timersub(&today, &usec, &yesterday);
278 (void)strftime(date, sizeof(date),
279 d_first ? "%e %b total" : "%b %e total",
280 localtime(&yesterday.tv_sec));
282 SLIST_FOREACH(lp, &CurUtmpx, next) {
283 timersub(&today, &lp->time, &diff);
284 update_user(lp->user, diff);
285 /* As if they just logged in. */
288 SLIST_FOREACH(up, &Users, next) {
289 timeradd(&total, &up->time, &total);
291 timerclear(&up->time);
293 if (timerisset(&total))
294 (void)printf("%s %11.2f\n", date, (double)total.tv_sec / 3600);
298 * Log a user out and update their times.
299 * If ut_type is BOOT_TIME or SHUTDOWN_TIME, we log all users out as the
300 * system has been shut down.
303 log_out(const struct utmpx *up)
305 struct utmpx_entry *lp, *lp2, *tlp;
308 for (lp = SLIST_FIRST(&CurUtmpx), lp2 = NULL; lp != NULL;)
309 if (up->ut_type == BOOT_TIME || up->ut_type == SHUTDOWN_TIME ||
310 (up->ut_type == DEAD_PROCESS &&
311 memcmp(lp->id, up->ut_id, sizeof(up->ut_id)) == 0)) {
312 timersub(&up->ut_tv, &lp->time, &secs);
313 update_user(lp->user, secs);
318 lp = SLIST_NEXT(lp, next);
320 SLIST_REMOVE_HEAD(&CurUtmpx, next);
322 SLIST_REMOVE_AFTER(lp2, next);
326 lp = SLIST_NEXT(lp, next);
331 * if do_tty says ok, login a user
334 log_in(struct utmpx *up)
336 struct utmpx_entry *lp;
339 * this could be a login. if we're not dealing with
340 * the console name, say it is.
342 * If we are, and if ut_host==":0.0" we know that it
343 * isn't a real login. _But_ if we have not yet recorded
344 * someone being logged in on Console - due to the wtmp
345 * file starting after they logged in, we'll pretend they
346 * logged in, at the start of the wtmp file.
350 if (up->ut_host[0] == ':') {
352 * SunOS 4.0.2 does not treat ":0.0" as special but we
358 * ok, no recorded login, so they were here when wtmp
359 * started! Adjust ut_time!
361 up->ut_tv = FirstTime;
363 * this allows us to pick the right logout
365 strlcpy(up->ut_line, Console, sizeof(up->ut_line));
369 * If we are doing specified ttys only, we ignore
372 if (Flags & AC_T && !do_tty(up->ut_line))
376 * go ahead and log them in
378 if ((lp = malloc(sizeof(*lp))) == NULL)
379 errx(1, "malloc failed");
380 SLIST_INSERT_HEAD(&CurUtmpx, lp, next);
381 strlcpy(lp->user, up->ut_user, sizeof(lp->user));
382 memcpy(lp->id, up->ut_id, sizeof(lp->id));
384 memcpy(lp->line, up->ut_line, sizeof(lp->line));
386 lp->time = up->ut_tv;
392 struct utmpx_entry *lp;
393 struct utmpx *usr, usht;
395 struct timeval prev_secs, ut_timecopy, secs, clock_shift, now;
399 timerclear(&prev_secs); /* Minimum acceptable date == 1970. */
401 timerclear(&clock_shift);
403 if (setutxdb(UTXDB_LOG, file) != 0)
405 while ((usr = getutxent()) != NULL) {
407 ut_timecopy = usr->ut_tv;
408 /* Don't let the time run backwards. */
409 if (timercmp(&ut_timecopy, &prev_secs, <))
410 ut_timecopy = prev_secs;
411 prev_secs = ut_timecopy;
413 if (!timerisset(&FirstTime))
414 FirstTime = ut_timecopy;
416 ltm = localtime(&ut_timecopy.tv_sec);
417 if (day >= 0 && day != ltm->tm_yday) {
420 * print yesterday's total
423 secs.tv_sec -= ltm->tm_sec;
424 secs.tv_sec -= 60 * ltm->tm_min;
425 secs.tv_sec -= 3600 * ltm->tm_hour;
431 switch(usr->ut_type) {
433 clock_shift = ut_timecopy;
436 timersub(&clock_shift, &ut_timecopy, &clock_shift);
438 * adjust time for those logged in
440 SLIST_FOREACH(lp, &CurUtmpx, next)
441 timersub(&lp->time, &clock_shift, &lp->time);
446 FirstTime = ut_timecopy; /* shouldn't be needed */
450 * If they came in on pts/..., then it is only
451 * a login session if the ut_host field is non-empty.
453 if (strncmp(usr->ut_line, "pts/", 4) != 0 ||
454 *usr->ut_host != '\0')
463 (void)gettimeofday(&now, NULL);
465 usht.ut_tv = ut_timecopy;
468 usht.ut_type = SHUTDOWN_TIME;
471 ltm = localtime(&ut_timecopy.tv_sec);
472 if (day >= 0 && day != ltm->tm_yday) {
474 * print yesterday's total
477 secs.tv_sec -= ltm->tm_sec;
478 secs.tv_sec -= 60 * ltm->tm_min;
479 secs.tv_sec -= 3600 * ltm->tm_hour;
485 * anyone still logged in gets time up to now
494 show("total", Total);
501 (void)fprintf(stderr,
503 "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
505 "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");