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>
36 * this is for our list of currently logged in sessions
39 struct utmp_list *next;
44 * this is for our list of users that are accumulating time.
47 struct user_list *next;
48 char name[UT_NAMESIZE+1];
53 * this is for chosing whether to ignore a login
56 struct tty_list *next;
57 char name[UT_LINESIZE+3];
66 static char *Console = CONSOLE_TTY;
68 static time_t Total = 0;
69 static time_t FirstTime = 0;
71 static struct user_list *Users = NULL;
72 static struct tty_list *Ttys = NULL;
74 #define NEW(type) (type *)malloc(sizeof (type))
76 #define AC_W 1 /* not _PATH_WTMP */
77 #define AC_D 2 /* daily totals (ignore -p) */
78 #define AC_P 4 /* per-user totals */
79 #define AC_U 8 /* specified users only */
80 #define AC_T 16 /* specified ttys only */
86 int main __P((int, char **));
88 struct tty_list *add_tty __P((char *));
89 int do_tty __P((char *));
90 FILE *file __P((const char *));
91 struct utmp_list *log_in __P((struct utmp_list *, struct utmp *));
92 struct utmp_list *log_out __P((struct utmp_list *, struct utmp *));
93 int on_console __P((struct utmp_list *));
94 void show __P((const char *, time_t));
95 void show_today __P((struct user_list *, struct utmp_list *,
97 void show_users __P((struct user_list *));
98 struct user_list *update_user __P((struct user_list *, char *, time_t));
99 void usage __P((void));
110 if ((fp = fopen(name, "r")) == NULL)
112 /* in case we want to discriminate */
113 if (strcmp(_PATH_WTMP, name))
127 if ((tp = NEW(struct tty_list)) == NULL)
128 errx(1, "malloc failed");
129 tp->len = 0; /* full match */
130 tp->ret = 1; /* do if match */
131 if (*name == '!') { /* don't do if match */
135 strlcpy(tp->name, name, sizeof (tp->name));
136 if ((rcp = strchr(tp->name, '*')) != NULL) { /* wild card */
138 tp->len = strlen(tp->name); /* match len bytes only */
146 * should we process the named tty?
155 for (tp = Ttys; tp != NULL; tp = tp->next) {
156 if (tp->ret == 0) /* specific don't */
157 def_ret = 1; /* default do */
159 if (strncmp(name, tp->name, tp->len) == 0)
162 if (strncmp(name, tp->name, sizeof (tp->name)) == 0)
171 * is someone logged in on Console?
175 struct utmp_list *head;
177 struct utmp_list *up;
179 for (up = head; up; up = up->next) {
180 if (strncmp(up->usr.ut_line, Console,
181 sizeof (up->usr.ut_line)) == 0)
189 * update user's login time
192 update_user(head, name, secs)
193 struct user_list *head;
197 struct user_list *up;
199 for (up = head; up != NULL; up = up->next) {
200 if (strncmp(up->name, name, UT_NAMESIZE) == 0) {
207 * not found so add new user unless specified users only
212 if ((up = NEW(struct user_list)) == NULL)
213 errx(1, "malloc failed");
215 strlcpy(up->name, name, sizeof (up->name));
229 (void) setlocale(LC_TIME, "");
232 while ((c = getopt(argc, argv, "Dc:dpt:w:")) != -1) {
252 case 't': /* only do specified ttys */
266 * initialize user list
268 for (; optind < argc; optind++) {
269 Users = update_user(Users, argv[optind], 0L);
271 Flags |= AC_U; /* freeze user list */
277 * if _PATH_WTMP does not exist, exit quietly
279 if (access(_PATH_WTMP, 0) != 0 && errno == ENOENT)
282 fp = file(_PATH_WTMP);
290 * print login time in decimal hours
297 (void)printf("\t%-*s %8.2f\n", UT_NAMESIZE, name,
298 ((double)secs / 3600));
303 struct user_list *list;
305 struct user_list *lp;
307 for (lp = list; lp; lp = lp->next)
308 show(lp->name, lp->secs);
312 * print total login time for 24hr period in decimal hours
315 show_today(users, logins, secs)
316 struct user_list *users;
317 struct utmp_list *logins;
320 struct user_list *up;
321 struct utmp_list *lp;
323 time_t yesterday = secs - 1;
324 static int d_first = -1;
327 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
328 (void)strftime(date, sizeof (date),
329 d_first ? "%e %b total" : "%b %e total",
330 localtime(&yesterday));
332 /* restore the missing second */
335 for (lp = logins; lp != NULL; lp = lp->next) {
336 secs = yesterday - lp->usr.ut_time;
337 Users = update_user(Users, lp->usr.ut_name, secs);
338 lp->usr.ut_time = yesterday; /* as if they just logged in */
341 for (up = users; up != NULL; up = up->next) {
343 up->secs = 0; /* for next day */
346 (void)printf("%s %11.2f\n", date, ((double)secs / 3600));
350 * log a user out and update their times.
351 * if ut_line is "~", we log all users out as the system has
356 struct utmp_list *head;
359 struct utmp_list *lp, *lp2, *tlp;
362 for (lp = head, lp2 = NULL; lp != NULL; )
363 if (*up->ut_line == '~' || strncmp(lp->usr.ut_line, up->ut_line,
364 sizeof (up->ut_line)) == 0) {
365 secs = up->ut_time - lp->usr.ut_time;
366 Users = update_user(Users, lp->usr.ut_name, secs);
369 printf("%-.*s %-.*s: %-.*s logged out (%2d:%02d:%02d)\n",
370 19, ctime(&up->ut_time),
371 sizeof (lp->usr.ut_line), lp->usr.ut_line,
372 sizeof (lp->usr.ut_name), lp->usr.ut_name,
373 secs / 3600, (secs % 3600) / 60, secs % 60);
382 else if (lp2 != NULL)
394 * if do_tty says ok, login a user
398 struct utmp_list *head;
401 struct utmp_list *lp;
404 * this could be a login. if we're not dealing with
405 * the console name, say it is.
407 * If we are, and if ut_host==":0.0" we know that it
408 * isn't a real login. _But_ if we have not yet recorded
409 * someone being logged in on Console - due to the wtmp
410 * file starting after they logged in, we'll pretend they
411 * logged in, at the start of the wtmp file.
415 if (up->ut_host[0] == ':') {
417 * SunOS 4.0.2 does not treat ":0.0" as special but we
420 if (on_console(head))
423 * ok, no recorded login, so they were here when wtmp
424 * started! Adjust ut_time!
426 up->ut_time = FirstTime;
428 * this allows us to pick the right logout
430 strlcpy(up->ut_line, Console, sizeof (up->ut_line));
434 * If we are doing specified ttys only, we ignore
438 if (!do_tty(up->ut_line))
442 * go ahead and log them in
444 if ((lp = NEW(struct utmp_list)) == NULL)
445 errx(1, "malloc failed");
448 memmove((char *)&lp->usr, (char *)up, sizeof (struct utmp));
451 printf("%-.*s %-.*s: %-.*s logged in", 19,
452 ctime(&lp->usr.ut_time), sizeof (up->ut_line),
453 up->ut_line, sizeof (up->ut_name), up->ut_name);
455 printf(" (%-.*s)", sizeof (up->ut_host), up->ut_host);
466 struct utmp_list *lp, *head = NULL;
472 while (fread((char *)&usr, sizeof(usr), 1, fp) == 1) {
474 FirstTime = usr.ut_time;
476 time_t t = int_to_time(usr.ut_time);
478 if (day >= 0 && day != ltm->tm_yday) {
481 * print yesterday's total
485 secs -= 60 * ltm->tm_min;
486 secs -= 3600 * ltm->tm_hour;
487 show_today(Users, head, secs);
491 switch(*usr.ut_line) {
498 * adjust time for those logged in
500 for (lp = head; lp != NULL; lp = lp->next)
501 lp->usr.ut_time -= secs;
503 case '~': /* reboot or shutdown */
504 head = log_out(head, &usr);
505 FirstTime = usr.ut_time; /* shouldn't be needed */
509 * if they came in on tty[p-sP-S]*, then it is only
510 * a login session if the ut_host field is non-empty
513 if (strncmp(usr.ut_line, "tty", 3) == 0 ||
514 strchr("pqrsPQRS", usr.ut_line[3]) != 0 ||
515 *usr.ut_host != '\0')
516 head = log_in(head, &usr);
518 head = log_out(head, &usr);
524 usr.ut_time = time((time_t *)0);
525 (void)strcpy(usr.ut_line, "~");
528 time_t t = int_to_time(usr.ut_time);
530 if (day >= 0 && day != ltm->tm_yday) {
532 * print yesterday's total
536 secs -= 60 * ltm->tm_min;
537 secs -= 3600 * ltm->tm_hour;
538 show_today(Users, head, secs);
542 * anyone still logged in gets time up to now
544 head = log_out(head, &usr);
547 show_today(Users, head, time((time_t *)0));
551 show("total", Total);
559 (void)fprintf(stderr,
561 "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
563 "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");