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/types.h>
33 * this is for our list of currently logged in sessions
36 struct utmp_list *next;
41 * this is for our list of users that are accumulating time.
44 struct user_list *next;
45 char name[sizeof(((struct utmpx *)0)->ut_user)];
50 * this is for chosing whether to ignore a login
53 struct tty_list *next;
54 char name[sizeof(((struct utmpx *)0)->ut_host) + 2];
63 static char *Console = CONSOLE_TTY;
65 static time_t Total = 0;
66 static time_t FirstTime = 0;
68 static struct user_list *Users = NULL;
69 static struct tty_list *Ttys = NULL;
71 #define NEW(type) (type *)malloc(sizeof (type))
73 #define AC_W 1 /* not _PATH_WTMP */
74 #define AC_D 2 /* daily totals (ignore -p) */
75 #define AC_P 4 /* per-user totals */
76 #define AC_U 8 /* specified users only */
77 #define AC_T 16 /* specified ttys only */
83 int main(int, char **);
85 struct tty_list *add_tty(char *);
87 const char *debug_pfx(const struct utmpx *, const struct utmpx *);
90 struct utmp_list *log_in(struct utmp_list *, struct utmpx *);
91 struct utmp_list *log_out(struct utmp_list *, struct utmpx *);
92 int on_console(struct utmp_list *);
93 void show(const char *, time_t);
94 void show_today(struct user_list *, struct utmp_list *,
96 void show_users(struct user_list *);
97 struct user_list *update_user(struct user_list *, char *, time_t);
108 if ((tp = NEW(struct tty_list)) == NULL)
109 errx(1, "malloc failed");
110 tp->len = 0; /* full match */
111 tp->ret = 1; /* do if match */
112 if (*name == '!') { /* don't do if match */
116 strlcpy(tp->name, name, sizeof (tp->name));
117 if ((rcp = strchr(tp->name, '*')) != NULL) { /* wild card */
119 tp->len = strlen(tp->name); /* match len bytes only */
127 * should we process the named tty?
135 for (tp = Ttys; tp != NULL; tp = tp->next) {
136 if (tp->ret == 0) /* specific don't */
137 def_ret = 1; /* default do */
139 if (strncmp(name, tp->name, tp->len) == 0)
142 if (strncmp(name, tp->name, sizeof (tp->name)) == 0)
151 * is someone logged in on Console?
154 on_console(struct utmp_list *head)
156 struct utmp_list *up;
158 for (up = head; up; up = up->next) {
159 if (strcmp(up->usr.ut_line, Console) == 0)
167 * update user's login time
170 update_user(struct user_list *head, char *name, time_t secs)
172 struct user_list *up;
174 for (up = head; up != NULL; up = up->next) {
175 if (strcmp(up->name, name) == 0) {
182 * not found so add new user unless specified users only
187 if ((up = NEW(struct user_list)) == NULL)
188 errx(1, "malloc failed");
190 strlcpy(up->name, name, sizeof (up->name));
198 * Create a string which is the standard prefix for a debug line. It
199 * includes a timestamp (perhaps with year), device-name, and user-name.
202 debug_pfx(const struct utmpx *event_up, const struct utmpx *userinf_up)
204 static char str_result[40 + sizeof(userinf_up->ut_line) +
205 sizeof(userinf_up->ut_user)];
206 static char thisyear[5];
210 if (thisyear[0] == '\0') {
211 /* Figure out what "this year" is. */
213 strlcpy(str_result, ctime(&ut_timecopy), sizeof(str_result));
214 strlcpy(thisyear, &str_result[20], sizeof(thisyear));
217 if (event_up->ut_tv.tv_sec == 0)
218 strlcpy(str_result, "*ZeroTime* --:--:-- ", sizeof(str_result));
220 ut_timecopy = event_up->ut_tv.tv_sec;
221 strlcpy(str_result, ctime(&ut_timecopy), sizeof(str_result));
223 * Include the year, if it is not the same year as "now".
225 if (strncmp(&str_result[20], thisyear, 4) == 0)
226 str_result[20] = '\0';
228 str_result[24] = ' '; /* Replace a '\n' */
229 str_result[25] = '\0';
233 if (userinf_up->ut_line[0] == '\0')
234 strlcat(str_result, "NoDev", sizeof(str_result));
236 maxcopy = strlen(str_result) + sizeof(userinf_up->ut_line);
237 if (maxcopy > sizeof(str_result))
238 maxcopy = sizeof(str_result);
239 strlcat(str_result, userinf_up->ut_line, maxcopy);
241 strlcat(str_result, ": ", sizeof(str_result));
243 if (userinf_up->ut_user[0] == '\0')
244 strlcat(str_result, "LogOff", sizeof(str_result));
246 maxcopy = strlen(str_result) + sizeof(userinf_up->ut_user);
247 if (maxcopy > sizeof(str_result))
248 maxcopy = sizeof(str_result);
249 strlcat(str_result, userinf_up->ut_user, maxcopy);
257 main(int argc, char *argv[])
259 const char *wtmpf = NULL;
262 (void) setlocale(LC_TIME, "");
264 while ((c = getopt(argc, argv, "Dc:dpt:w:")) != -1) {
284 case 't': /* only do specified ttys */
299 * initialize user list
301 for (; optind < argc; optind++) {
302 Users = update_user(Users, argv[optind], (time_t)0);
304 Flags |= AC_U; /* freeze user list */
314 * print login time in decimal hours
317 show(const char *name, time_t secs)
319 (void)printf("\t%-*s %8.2f\n",
320 (int)sizeof(((struct utmpx *)0)->ut_user), name,
321 ((double)secs / 3600));
325 show_users(struct user_list *list)
327 struct user_list *lp;
329 for (lp = list; lp; lp = lp->next)
330 show(lp->name, lp->secs);
334 * print total login time for 24hr period in decimal hours
337 show_today(struct user_list *users, struct utmp_list *logins, time_t secs)
339 struct user_list *up;
340 struct utmp_list *lp;
342 time_t yesterday = secs - 1;
343 static int d_first = -1;
346 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
347 (void)strftime(date, sizeof (date),
348 d_first ? "%e %b total" : "%b %e total",
349 localtime(&yesterday));
351 /* restore the missing second */
354 for (lp = logins; lp != NULL; lp = lp->next) {
355 secs = yesterday - lp->usr.ut_tv.tv_sec;
356 Users = update_user(Users, lp->usr.ut_user, secs);
357 lp->usr.ut_tv.tv_sec = yesterday; /* as if they just logged in */
360 for (up = users; up != NULL; up = up->next) {
362 up->secs = 0; /* for next day */
365 (void)printf("%s %11.2f\n", date, ((double)secs / 3600));
369 * log a user out and update their times.
370 * if ut_line is "~", we log all users out as the system has
374 log_out(struct utmp_list *head, struct utmpx *up)
376 struct utmp_list *lp, *lp2, *tlp;
379 for (lp = head, lp2 = NULL; lp != NULL; )
380 if (up->ut_type == BOOT_TIME || up->ut_type == SHUTDOWN_TIME ||
381 (up->ut_type == DEAD_PROCESS &&
382 memcmp(lp->usr.ut_id, up->ut_id, sizeof up->ut_id) == 0)) {
383 secs = up->ut_tv.tv_sec - lp->usr.ut_tv.tv_sec;
384 Users = update_user(Users, lp->usr.ut_user, secs);
387 printf("%s logged out (%2d:%02d:%02d)\n",
388 debug_pfx(up, &lp->usr), (int)(secs / 3600),
389 (int)((secs % 3600) / 60),
399 else if (lp2 != NULL)
411 * if do_tty says ok, login a user
414 log_in(struct utmp_list *head, struct utmpx *up)
416 struct utmp_list *lp;
419 * this could be a login. if we're not dealing with
420 * the console name, say it is.
422 * If we are, and if ut_host==":0.0" we know that it
423 * isn't a real login. _But_ if we have not yet recorded
424 * someone being logged in on Console - due to the wtmp
425 * file starting after they logged in, we'll pretend they
426 * logged in, at the start of the wtmp file.
430 if (up->ut_host[0] == ':') {
432 * SunOS 4.0.2 does not treat ":0.0" as special but we
435 if (on_console(head))
438 * ok, no recorded login, so they were here when wtmp
439 * started! Adjust ut_time!
441 up->ut_time = FirstTime;
443 * this allows us to pick the right logout
445 strlcpy(up->ut_line, Console, sizeof (up->ut_line));
449 * If we are doing specified ttys only, we ignore
453 if (!do_tty(up->ut_line))
457 * go ahead and log them in
459 if ((lp = NEW(struct utmp_list)) == NULL)
460 errx(1, "malloc failed");
463 memmove(&lp->usr, up, sizeof *up);
466 printf("%s logged in", debug_pfx(&lp->usr, up));
468 printf(" (%-.*s)", (int)sizeof(up->ut_host),
479 struct utmp_list *lp, *head = NULL;
480 struct utmpx *usr, usht;
482 time_t prev_secs, secs, ut_timecopy;
483 int day, rfound, tchanged, tskipped;
486 prev_secs = 1; /* Minimum acceptable date == 1970 */
487 rfound = tchanged = tskipped = 0;
489 if (setutxdb(UTXDB_LOG, file) != 0)
491 while ((usr = getutxent()) != NULL) {
493 ut_timecopy = usr->ut_tv.tv_sec;
495 * With sparc64 using 64-bit time_t's, there is some system
496 * routine which sets ut_time==0 (the high-order word of a
497 * 64-bit time) instead of a 32-bit time value. For those
498 * wtmp files, it is "more-accurate" to substitute the most-
499 * recent time found, instead of throwing away the entire
500 * record. While it is still just a guess, it is a better
501 * guess than throwing away a log-off record and therefore
502 * counting a session as if it continued to the end of the
503 * month, or the next system-reboot.
505 if (ut_timecopy == 0 && prev_secs > 1) {
508 printf("%s - date changed to: %s",
509 debug_pfx(usr, usr), ctime(&prev_secs));
512 usr->ut_tv.tv_sec = ut_timecopy = prev_secs;
515 * Skip records where the time goes backwards.
517 if (ut_timecopy < prev_secs) {
520 printf("%s - bad date, record skipped\n",
521 debug_pfx(usr, usr));
524 continue; /* Skip this invalid record. */
526 prev_secs = ut_timecopy;
529 FirstTime = ut_timecopy;
531 ltm = localtime(&ut_timecopy);
532 if (day >= 0 && day != ltm->tm_yday) {
535 * print yesterday's total
539 secs -= 60 * ltm->tm_min;
540 secs -= 3600 * ltm->tm_hour;
541 show_today(Users, head, secs);
545 switch(usr->ut_type) {
552 * adjust time for those logged in
554 for (lp = head; lp != NULL; lp = lp->next)
555 lp->usr.ut_tv.tv_sec -= secs;
559 head = log_out(head, usr);
560 FirstTime = ut_timecopy; /* shouldn't be needed */
564 * if they came in on tty[p-sP-S]*, then it is only
565 * a login session if the ut_host field is non-empty
567 if (strncmp(usr->ut_line, "tty", 3) != 0 ||
568 strchr("pqrsPQRS", usr->ut_line[3]) == NULL ||
569 *usr->ut_host != '\0')
570 head = log_in(head, usr);
573 /* Things such as 'screen' sessions. */
574 printf("%s - record ignored\n",
575 debug_pfx(usr, usr));
579 head = log_out(head, usr);
585 usht.ut_tv.tv_sec = time(NULL);
587 usht.ut_tv.tv_sec = ut_timecopy;
588 usht.ut_type = SHUTDOWN_TIME;
591 ltm = localtime(&ut_timecopy);
592 if (day >= 0 && day != ltm->tm_yday) {
594 * print yesterday's total
598 secs -= 60 * ltm->tm_min;
599 secs -= 3600 * ltm->tm_hour;
600 show_today(Users, head, secs);
604 * anyone still logged in gets time up to now
606 head = log_out(head, &usht);
609 show_today(Users, head, time((time_t *)0));
613 show("total", Total);
617 printf("(Skipped %d of %d records due to invalid time values)\n",
620 printf("(Changed %d of %d records to have a more likely time value)\n",
629 (void)fprintf(stderr,
631 "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
633 "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");