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[UT_NAMESIZE+1];
50 * this is for chosing whether to ignore a login
53 struct tty_list *next;
54 char name[UT_LINESIZE+3];
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 utmp *, const struct utmp *);
90 FILE *file(const char *);
91 struct utmp_list *log_in(struct utmp_list *, struct utmp *);
92 struct utmp_list *log_out(struct utmp_list *, struct utmp *);
93 int on_console(struct utmp_list *);
94 void show(const char *, time_t);
95 void show_today(struct user_list *, struct utmp_list *,
97 void show_users(struct user_list *);
98 struct user_list *update_user(struct user_list *, char *, time_t);
105 file(const char *name)
109 if ((fp = fopen(name, "r")) == NULL)
111 /* in case we want to discriminate */
112 if (strcmp(_PATH_WTMP, name))
125 if ((tp = NEW(struct tty_list)) == NULL)
126 errx(1, "malloc failed");
127 tp->len = 0; /* full match */
128 tp->ret = 1; /* do if match */
129 if (*name == '!') { /* don't do if match */
133 strlcpy(tp->name, name, sizeof (tp->name));
134 if ((rcp = strchr(tp->name, '*')) != NULL) { /* wild card */
136 tp->len = strlen(tp->name); /* match len bytes only */
144 * should we process the named tty?
152 for (tp = Ttys; tp != NULL; tp = tp->next) {
153 if (tp->ret == 0) /* specific don't */
154 def_ret = 1; /* default do */
156 if (strncmp(name, tp->name, tp->len) == 0)
159 if (strncmp(name, tp->name, sizeof (tp->name)) == 0)
168 * is someone logged in on Console?
171 on_console(struct utmp_list *head)
173 struct utmp_list *up;
175 for (up = head; up; up = up->next) {
176 if (strncmp(up->usr.ut_line, Console,
177 sizeof (up->usr.ut_line)) == 0)
185 * update user's login time
188 update_user(struct user_list *head, char *name, time_t secs)
190 struct user_list *up;
192 for (up = head; up != NULL; up = up->next) {
193 if (strncmp(up->name, name, UT_NAMESIZE) == 0) {
200 * not found so add new user unless specified users only
205 if ((up = NEW(struct user_list)) == NULL)
206 errx(1, "malloc failed");
208 strlcpy(up->name, name, sizeof (up->name));
216 * Create a string which is the standard prefix for a debug line. It
217 * includes a timestamp (perhaps with year), device-name, and user-name.
220 debug_pfx(const struct utmp *event_up, const struct utmp *userinf_up)
222 static char str_result[40+UT_LINESIZE+UT_NAMESIZE];
223 static char thisyear[5];
227 if (thisyear[0] == '\0') {
228 /* Figure out what "this year" is. */
230 strlcpy(str_result, ctime(&ut_timecopy), sizeof(str_result));
231 strlcpy(thisyear, &str_result[20], sizeof(thisyear));
234 if (event_up->ut_time == 0)
235 strlcpy(str_result, "*ZeroTime* --:--:-- ", sizeof(str_result));
238 * The type of utmp.ut_time is not necessary type time_t, as
239 * it is explicitly defined as type int32_t. Copy the value
240 * for platforms where sizeof(time_t) != sizeof(int32_t).
242 ut_timecopy = _time32_to_time(event_up->ut_time);
243 strlcpy(str_result, ctime(&ut_timecopy), sizeof(str_result));
245 * Include the year, if it is not the same year as "now".
247 if (strncmp(&str_result[20], thisyear, 4) == 0)
248 str_result[20] = '\0';
250 str_result[24] = ' '; /* Replace a '\n' */
251 str_result[25] = '\0';
255 if (userinf_up->ut_line[0] == '\0')
256 strlcat(str_result, "NoDev", sizeof(str_result));
258 /* ut_line is not necessarily null-terminated. */
259 maxcopy = strlen(str_result) + UT_LINESIZE + 1;
260 if (maxcopy > sizeof(str_result))
261 maxcopy = sizeof(str_result);
262 strlcat(str_result, userinf_up->ut_line, maxcopy);
264 strlcat(str_result, ": ", sizeof(str_result));
266 if (userinf_up->ut_name[0] == '\0')
267 strlcat(str_result, "LogOff", sizeof(str_result));
269 /* ut_name is not necessarily null-terminated. */
270 maxcopy = strlen(str_result) + UT_NAMESIZE + 1;
271 if (maxcopy > sizeof(str_result))
272 maxcopy = sizeof(str_result);
273 strlcat(str_result, userinf_up->ut_name, maxcopy);
281 main(int argc, char *argv[])
286 (void) setlocale(LC_TIME, "");
289 while ((c = getopt(argc, argv, "Dc:dpt:w:")) != -1) {
309 case 't': /* only do specified ttys */
323 * initialize user list
325 for (; optind < argc; optind++) {
326 Users = update_user(Users, argv[optind], (time_t)0);
328 Flags |= AC_U; /* freeze user list */
334 * if _PATH_WTMP does not exist, exit quietly
336 if (access(_PATH_WTMP, 0) != 0 && errno == ENOENT)
339 fp = file(_PATH_WTMP);
347 * print login time in decimal hours
350 show(const char *name, time_t secs)
352 (void)printf("\t%-*s %8.2f\n", UT_NAMESIZE, name,
353 ((double)secs / 3600));
357 show_users(struct user_list *list)
359 struct user_list *lp;
361 for (lp = list; lp; lp = lp->next)
362 show(lp->name, lp->secs);
366 * print total login time for 24hr period in decimal hours
369 show_today(struct user_list *users, struct utmp_list *logins, time_t secs)
371 struct user_list *up;
372 struct utmp_list *lp;
374 time_t yesterday = secs - 1;
375 static int d_first = -1;
378 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
379 (void)strftime(date, sizeof (date),
380 d_first ? "%e %b total" : "%b %e total",
381 localtime(&yesterday));
383 /* restore the missing second */
386 for (lp = logins; lp != NULL; lp = lp->next) {
387 secs = yesterday - lp->usr.ut_time;
388 Users = update_user(Users, lp->usr.ut_name, secs);
389 lp->usr.ut_time = yesterday; /* as if they just logged in */
392 for (up = users; up != NULL; up = up->next) {
394 up->secs = 0; /* for next day */
397 (void)printf("%s %11.2f\n", date, ((double)secs / 3600));
401 * log a user out and update their times.
402 * if ut_line is "~", we log all users out as the system has
406 log_out(struct utmp_list *head, struct utmp *up)
408 struct utmp_list *lp, *lp2, *tlp;
411 for (lp = head, lp2 = NULL; lp != NULL; )
412 if (*up->ut_line == '~' || strncmp(lp->usr.ut_line, up->ut_line,
413 sizeof (up->ut_line)) == 0) {
414 secs = up->ut_time - lp->usr.ut_time;
415 Users = update_user(Users, lp->usr.ut_name, secs);
418 printf("%s logged out (%2d:%02d:%02d)\n",
419 debug_pfx(up, &lp->usr), (int)(secs / 3600),
420 (int)((secs % 3600) / 60),
430 else if (lp2 != NULL)
442 * if do_tty says ok, login a user
445 log_in(struct utmp_list *head, struct utmp *up)
447 struct utmp_list *lp;
450 * this could be a login. if we're not dealing with
451 * the console name, say it is.
453 * If we are, and if ut_host==":0.0" we know that it
454 * isn't a real login. _But_ if we have not yet recorded
455 * someone being logged in on Console - due to the wtmp
456 * file starting after they logged in, we'll pretend they
457 * logged in, at the start of the wtmp file.
461 if (up->ut_host[0] == ':') {
463 * SunOS 4.0.2 does not treat ":0.0" as special but we
466 if (on_console(head))
469 * ok, no recorded login, so they were here when wtmp
470 * started! Adjust ut_time!
472 up->ut_time = FirstTime;
474 * this allows us to pick the right logout
476 strlcpy(up->ut_line, Console, sizeof (up->ut_line));
480 * If we are doing specified ttys only, we ignore
484 if (!do_tty(up->ut_line))
488 * go ahead and log them in
490 if ((lp = NEW(struct utmp_list)) == NULL)
491 errx(1, "malloc failed");
494 memmove((char *)&lp->usr, (char *)up, sizeof (struct utmp));
497 printf("%s logged in", debug_pfx(&lp->usr, up));
499 printf(" (%-.*s)", (int)sizeof(up->ut_host),
510 struct utmp_list *lp, *head = NULL;
513 time_t prev_secs, secs, ut_timecopy;
514 int day, rfound, tchanged, tskipped;
517 prev_secs = 1; /* Minimum acceptable date == 1970 */
518 rfound = tchanged = tskipped = 0;
520 while (fread((char *)&usr, sizeof(usr), 1, fp) == 1) {
523 * The type of utmp.ut_time is not necessary type time_t, as
524 * it is explicitly defined as type int32_t. Copy the value
525 * for platforms where sizeof(time_t) != size(int32_t).
527 ut_timecopy = _time32_to_time(usr.ut_time);
529 * With sparc64 using 64-bit time_t's, there is some system
530 * routine which sets ut_time==0 (the high-order word of a
531 * 64-bit time) instead of a 32-bit time value. For those
532 * wtmp files, it is "more-accurate" to substitute the most-
533 * recent time found, instead of throwing away the entire
534 * record. While it is still just a guess, it is a better
535 * guess than throwing away a log-off record and therefore
536 * counting a session as if it continued to the end of the
537 * month, or the next system-reboot.
539 if (ut_timecopy == 0 && prev_secs > 1) {
542 printf("%s - date changed to: %s",
543 debug_pfx(&usr, &usr), ctime(&prev_secs));
546 usr.ut_time = ut_timecopy = prev_secs;
549 * Skip records where the time goes backwards.
551 if (ut_timecopy < prev_secs) {
554 printf("%s - bad date, record skipped\n",
555 debug_pfx(&usr, &usr));
558 continue; /* Skip this invalid record. */
560 prev_secs = ut_timecopy;
563 FirstTime = ut_timecopy;
565 ltm = localtime(&ut_timecopy);
566 if (day >= 0 && day != ltm->tm_yday) {
569 * print yesterday's total
573 secs -= 60 * ltm->tm_min;
574 secs -= 3600 * ltm->tm_hour;
575 show_today(Users, head, secs);
579 switch(*usr.ut_line) {
586 * adjust time for those logged in
588 for (lp = head; lp != NULL; lp = lp->next)
589 lp->usr.ut_time -= secs;
591 case '~': /* reboot or shutdown */
592 head = log_out(head, &usr);
593 FirstTime = ut_timecopy; /* shouldn't be needed */
597 * if they came in on tty[p-sP-S]*, then it is only
598 * a login session if the ut_host field is non-empty
601 if (strncmp(usr.ut_line, "tty", 3) == 0 ||
602 strchr("pqrsPQRS", usr.ut_line[3]) != 0 ||
603 *usr.ut_host != '\0')
604 head = log_in(head, &usr);
607 /* Things such as 'screen' sessions. */
608 printf("%s - record ignored\n",
609 debug_pfx(&usr, &usr));
612 head = log_out(head, &usr);
618 usr.ut_time = time((time_t *)0);
619 (void)strcpy(usr.ut_line, "~");
622 ut_timecopy = _time32_to_time(usr.ut_time);
623 ltm = localtime(&ut_timecopy);
624 if (day >= 0 && day != ltm->tm_yday) {
626 * print yesterday's total
630 secs -= 60 * ltm->tm_min;
631 secs -= 3600 * ltm->tm_hour;
632 show_today(Users, head, secs);
636 * anyone still logged in gets time up to now
638 head = log_out(head, &usr);
641 show_today(Users, head, time((time_t *)0));
645 show("total", Total);
649 printf("(Skipped %d of %d records due to invalid time values)\n",
652 printf("(Changed %d of %d records to have a more likely time value)\n",
661 (void)fprintf(stderr,
663 "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
665 "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");