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
17 static const char rcsid[] =
21 #include <sys/types.h>
37 * this is for our list of currently logged in sessions
40 struct utmp_list *next;
45 * this is for our list of users that are accumulating time.
48 struct user_list *next;
49 char name[UT_NAMESIZE+1];
54 * this is for chosing whether to ignore a login
57 struct tty_list *next;
58 char name[UT_LINESIZE+3];
67 static char *Console = CONSOLE_TTY;
69 static time_t Total = 0;
70 static time_t FirstTime = 0;
72 static struct user_list *Users = NULL;
73 static struct tty_list *Ttys = NULL;
75 #define NEW(type) (type *)malloc(sizeof (type))
77 #define AC_W 1 /* not _PATH_WTMP */
78 #define AC_D 2 /* daily totals (ignore -p) */
79 #define AC_P 4 /* per-user totals */
80 #define AC_U 8 /* specified users only */
81 #define AC_T 16 /* specified ttys only */
87 int main(int, char **);
89 struct tty_list *add_tty(char *);
91 FILE *file(const char *);
92 struct utmp_list *log_in(struct utmp_list *, struct utmp *);
93 struct utmp_list *log_out(struct utmp_list *, struct utmp *);
94 int on_console(struct utmp_list *);
95 void show(const char *, time_t);
96 void show_today(struct user_list *, struct utmp_list *,
98 void show_users(struct user_list *);
99 struct user_list *update_user(struct user_list *, char *, time_t);
111 if ((fp = fopen(name, "r")) == NULL)
113 /* in case we want to discriminate */
114 if (strcmp(_PATH_WTMP, name))
128 if ((tp = NEW(struct tty_list)) == NULL)
129 errx(1, "malloc failed");
130 tp->len = 0; /* full match */
131 tp->ret = 1; /* do if match */
132 if (*name == '!') { /* don't do if match */
136 strlcpy(tp->name, name, sizeof (tp->name));
137 if ((rcp = strchr(tp->name, '*')) != NULL) { /* wild card */
139 tp->len = strlen(tp->name); /* match len bytes only */
147 * should we process the named tty?
156 for (tp = Ttys; tp != NULL; tp = tp->next) {
157 if (tp->ret == 0) /* specific don't */
158 def_ret = 1; /* default do */
160 if (strncmp(name, tp->name, tp->len) == 0)
163 if (strncmp(name, tp->name, sizeof (tp->name)) == 0)
172 * is someone logged in on Console?
176 struct utmp_list *head;
178 struct utmp_list *up;
180 for (up = head; up; up = up->next) {
181 if (strncmp(up->usr.ut_line, Console,
182 sizeof (up->usr.ut_line)) == 0)
190 * update user's login time
193 update_user(head, name, secs)
194 struct user_list *head;
198 struct user_list *up;
200 for (up = head; up != NULL; up = up->next) {
201 if (strncmp(up->name, name, UT_NAMESIZE) == 0) {
208 * not found so add new user unless specified users only
213 if ((up = NEW(struct user_list)) == NULL)
214 errx(1, "malloc failed");
216 strlcpy(up->name, name, sizeof (up->name));
230 (void) setlocale(LC_TIME, "");
233 while ((c = getopt(argc, argv, "Dc:dpt:w:")) != -1) {
253 case 't': /* only do specified ttys */
267 * initialize user list
269 for (; optind < argc; optind++) {
270 Users = update_user(Users, argv[optind], 0L);
272 Flags |= AC_U; /* freeze user list */
278 * if _PATH_WTMP does not exist, exit quietly
280 if (access(_PATH_WTMP, 0) != 0 && errno == ENOENT)
283 fp = file(_PATH_WTMP);
291 * print login time in decimal hours
298 (void)printf("\t%-*s %8.2f\n", UT_NAMESIZE, name,
299 ((double)secs / 3600));
304 struct user_list *list;
306 struct user_list *lp;
308 for (lp = list; lp; lp = lp->next)
309 show(lp->name, lp->secs);
313 * print total login time for 24hr period in decimal hours
316 show_today(users, logins, secs)
317 struct user_list *users;
318 struct utmp_list *logins;
321 struct user_list *up;
322 struct utmp_list *lp;
324 time_t yesterday = secs - 1;
325 static int d_first = -1;
328 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
329 (void)strftime(date, sizeof (date),
330 d_first ? "%e %b total" : "%b %e total",
331 localtime(&yesterday));
333 /* restore the missing second */
336 for (lp = logins; lp != NULL; lp = lp->next) {
337 secs = yesterday - lp->usr.ut_time;
338 Users = update_user(Users, lp->usr.ut_name, secs);
339 lp->usr.ut_time = yesterday; /* as if they just logged in */
342 for (up = users; up != NULL; up = up->next) {
344 up->secs = 0; /* for next day */
347 (void)printf("%s %11.2f\n", date, ((double)secs / 3600));
351 * log a user out and update their times.
352 * if ut_line is "~", we log all users out as the system has
357 struct utmp_list *head;
360 struct utmp_list *lp, *lp2, *tlp;
363 for (lp = head, lp2 = NULL; lp != NULL; )
364 if (*up->ut_line == '~' || strncmp(lp->usr.ut_line, up->ut_line,
365 sizeof (up->ut_line)) == 0) {
366 secs = up->ut_time - lp->usr.ut_time;
367 Users = update_user(Users, lp->usr.ut_name, secs);
370 printf("%-.*s %-.*s: %-.*s logged out (%2d:%02d:%02d)\n",
371 19, ctime(&up->ut_time),
372 sizeof (lp->usr.ut_line), lp->usr.ut_line,
373 sizeof (lp->usr.ut_name), lp->usr.ut_name,
374 secs / 3600, (secs % 3600) / 60, secs % 60);
383 else if (lp2 != NULL)
395 * if do_tty says ok, login a user
399 struct utmp_list *head;
402 struct utmp_list *lp;
405 * this could be a login. if we're not dealing with
406 * the console name, say it is.
408 * If we are, and if ut_host==":0.0" we know that it
409 * isn't a real login. _But_ if we have not yet recorded
410 * someone being logged in on Console - due to the wtmp
411 * file starting after they logged in, we'll pretend they
412 * logged in, at the start of the wtmp file.
416 if (up->ut_host[0] == ':') {
418 * SunOS 4.0.2 does not treat ":0.0" as special but we
421 if (on_console(head))
424 * ok, no recorded login, so they were here when wtmp
425 * started! Adjust ut_time!
427 up->ut_time = FirstTime;
429 * this allows us to pick the right logout
431 strlcpy(up->ut_line, Console, sizeof (up->ut_line));
435 * If we are doing specified ttys only, we ignore
439 if (!do_tty(up->ut_line))
443 * go ahead and log them in
445 if ((lp = NEW(struct utmp_list)) == NULL)
446 errx(1, "malloc failed");
449 memmove((char *)&lp->usr, (char *)up, sizeof (struct utmp));
452 printf("%-.*s %-.*s: %-.*s logged in", 19,
453 ctime(&lp->usr.ut_time), sizeof (up->ut_line),
454 up->ut_line, sizeof (up->ut_name), up->ut_name);
456 printf(" (%-.*s)", sizeof (up->ut_host), up->ut_host);
467 struct utmp_list *lp, *head = NULL;
473 while (fread((char *)&usr, sizeof(usr), 1, fp) == 1) {
475 FirstTime = usr.ut_time;
477 time_t t = _int_to_time(usr.ut_time);
479 if (day >= 0 && day != ltm->tm_yday) {
482 * print yesterday's total
486 secs -= 60 * ltm->tm_min;
487 secs -= 3600 * ltm->tm_hour;
488 show_today(Users, head, secs);
492 switch(*usr.ut_line) {
499 * adjust time for those logged in
501 for (lp = head; lp != NULL; lp = lp->next)
502 lp->usr.ut_time -= secs;
504 case '~': /* reboot or shutdown */
505 head = log_out(head, &usr);
506 FirstTime = usr.ut_time; /* shouldn't be needed */
510 * if they came in on tty[p-sP-S]*, then it is only
511 * a login session if the ut_host field is non-empty
514 if (strncmp(usr.ut_line, "tty", 3) == 0 ||
515 strchr("pqrsPQRS", usr.ut_line[3]) != 0 ||
516 *usr.ut_host != '\0')
517 head = log_in(head, &usr);
519 head = log_out(head, &usr);
525 usr.ut_time = time((time_t *)0);
526 (void)strcpy(usr.ut_line, "~");
529 time_t t = _int_to_time(usr.ut_time);
531 if (day >= 0 && day != ltm->tm_yday) {
533 * print yesterday's total
537 secs -= 60 * ltm->tm_min;
538 secs -= 3600 * ltm->tm_hour;
539 show_today(Users, head, secs);
543 * anyone still logged in gets time up to now
545 head = log_out(head, &usr);
548 show_today(Users, head, time((time_t *)0));
552 show("total", Total);
560 (void)fprintf(stderr,
562 "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
564 "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");