#include <sys/cdefs.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <err.h>
#include <errno.h>
#include <langinfo.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <timeconv.h>
#include <unistd.h>
#include <utmpx.h>
struct utmpx_entry {
SLIST_ENTRY(utmpx_entry) next;
char user[sizeof(((struct utmpx *)0)->ut_user)];
char id[sizeof(((struct utmpx *)0)->ut_id)];
#ifdef CONSOLE_TTY
char line[sizeof(((struct utmpx *)0)->ut_line)];
#endif
struct timeval time;
};
struct user_entry {
SLIST_ENTRY(user_entry) next;
char user[sizeof(((struct utmpx *)0)->ut_user)];
struct timeval time;
};
struct tty_entry {
SLIST_ENTRY(tty_entry) next;
char line[sizeof(((struct utmpx *)0)->ut_line) + 2];
size_t len;
int ret;
};
#ifdef CONSOLE_TTY
static const char *Console = CONSOLE_TTY;
#endif
static struct timeval Total = { 0, 0 };
static struct timeval FirstTime = { 0, 0 };
static int Flags = 0;
static SLIST_HEAD(, utmpx_entry) CurUtmpx = SLIST_HEAD_INITIALIZER(CurUtmpx);
static SLIST_HEAD(, user_entry) Users = SLIST_HEAD_INITIALIZER(Users);
static SLIST_HEAD(, tty_entry) Ttys = SLIST_HEAD_INITIALIZER(Ttys);
#define AC_W 1
#define AC_D 2
#define AC_P 4
#define AC_U 8
#define AC_T 16
static void ac(const char *);
static void usage(void);
static void
add_tty(const char *line)
{
struct tty_entry *tp;
char *rcp;
Flags |= AC_T;
if ((tp = malloc(sizeof(*tp))) == NULL)
errx(1, "malloc failed");
tp->len = 0;
tp->ret = 1;
if (*line == '!') {
tp->ret = 0;
line++;
}
strlcpy(tp->line, line, sizeof(tp->line));
if ((rcp = strchr(tp->line, '*')) != NULL) {
*rcp = '\0';
tp->len = strlen(tp->line);
}
SLIST_INSERT_HEAD(&Ttys, tp, next);
}
static int
do_tty(const char *line)
{
struct tty_entry *tp;
int def_ret = 0;
SLIST_FOREACH(tp, &Ttys, next) {
if (tp->ret == 0)
def_ret = 1;
if (tp->len != 0) {
if (strncmp(line, tp->line, tp->len) == 0)
return tp->ret;
} else {
if (strncmp(line, tp->line, sizeof(tp->line)) == 0)
return tp->ret;
}
}
return (def_ret);
}
#ifdef CONSOLE_TTY
static int
on_console(void)
{
struct utmpx_entry *up;
SLIST_FOREACH(up, &CurUtmpx, next)
if (strcmp(up->line, Console) == 0)
return (1);
return (0);
}
#endif
static void
update_user(const char *user, struct timeval secs)
{
struct user_entry *up, *aup;
int c;
aup = NULL;
SLIST_FOREACH(up, &Users, next) {
c = strcmp(up->user, user);
if (c == 0) {
timeradd(&up->time, &secs, &up->time);
timeradd(&Total, &secs, &Total);
return;
} else if (c > 0)
break;
aup = up;
}
if (Flags & AC_U)
return;
if ((up = malloc(sizeof(*up))) == NULL)
errx(1, "malloc failed");
if (aup == NULL)
SLIST_INSERT_HEAD(&Users, up, next);
else
SLIST_INSERT_AFTER(aup, up, next);
strlcpy(up->user, user, sizeof(up->user));
up->time = secs;
timeradd(&Total, &secs, &Total);
}
int
main(int argc, char *argv[])
{
const char *wtmpf = NULL;
int c;
(void) setlocale(LC_TIME, "");
while ((c = getopt(argc, argv, "c:dpt:w:")) != -1) {
switch (c) {
case 'c':
#ifdef CONSOLE_TTY
Console = optarg;
#else
usage();
#endif
break;
case 'd':
Flags |= AC_D;
break;
case 'p':
Flags |= AC_P;
break;
case 't':
add_tty(optarg);
break;
case 'w':
Flags |= AC_W;
wtmpf = optarg;
break;
case '?':
default:
usage();
break;
}
}
if (optind < argc) {
for (; optind < argc; optind++) {
update_user(argv[optind], (struct timeval){ 0, 0 });
}
Flags |= AC_U;
}
if (Flags & AC_D)
Flags &= ~AC_P;
ac(wtmpf);
return (0);
}
static void
show(const char *user, struct timeval secs)
{
(void)printf("\t%-*s %8.2f\n",
(int)sizeof(((struct user_entry *)0)->user), user,
(double)secs.tv_sec / 3600);
}
static void
show_users(void)
{
struct user_entry *lp;
SLIST_FOREACH(lp, &Users, next)
show(lp->user, lp->time);
}
static void
show_today(struct timeval today)
{
struct user_entry *up;
struct utmpx_entry *lp;
char date[64];
struct timeval diff, total = { 0, 0 }, usec = { 0, 1 }, yesterday;
static int d_first = -1;
if (d_first < 0)
d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
timersub(&today, &usec, &yesterday);
(void)strftime(date, sizeof(date),
d_first ? "%e %b total" : "%b %e total",
localtime(&yesterday.tv_sec));
SLIST_FOREACH(lp, &CurUtmpx, next) {
timersub(&today, &lp->time, &diff);
update_user(lp->user, diff);
lp->time = today;
}
SLIST_FOREACH(up, &Users, next) {
timeradd(&total, &up->time, &total);
timerclear(&up->time);
}
if (timerisset(&total))
(void)printf("%s %11.2f\n", date, (double)total.tv_sec / 3600);
}
static void
log_out(const struct utmpx *up)
{
struct utmpx_entry *lp, *lp2, *tlp;
struct timeval secs;
for (lp = SLIST_FIRST(&CurUtmpx), lp2 = NULL; lp != NULL;)
if (up->ut_type == BOOT_TIME || up->ut_type == SHUTDOWN_TIME ||
(up->ut_type == DEAD_PROCESS &&
memcmp(lp->id, up->ut_id, sizeof(up->ut_id)) == 0)) {
timersub(&up->ut_tv, &lp->time, &secs);
update_user(lp->user, secs);
tlp = lp;
lp = SLIST_NEXT(lp, next);
if (lp2 == NULL)
SLIST_REMOVE_HEAD(&CurUtmpx, next);
else
SLIST_REMOVE_AFTER(lp2, next);
free(tlp);
} else {
lp2 = lp;
lp = SLIST_NEXT(lp, next);
}
}
static void
log_in(struct utmpx *up)
{
struct utmpx_entry *lp;
#ifdef CONSOLE_TTY
if (up->ut_host[0] == ':') {
if (on_console())
return;
up->ut_tv = FirstTime;
strlcpy(up->ut_line, Console, sizeof(up->ut_line));
}
#endif
if (Flags & AC_T && !do_tty(up->ut_line))
return;
if ((lp = malloc(sizeof(*lp))) == NULL)
errx(1, "malloc failed");
SLIST_INSERT_HEAD(&CurUtmpx, lp, next);
strlcpy(lp->user, up->ut_user, sizeof(lp->user));
memcpy(lp->id, up->ut_id, sizeof(lp->id));
#ifdef CONSOLE_TTY
memcpy(lp->line, up->ut_line, sizeof(lp->line));
#endif
lp->time = up->ut_tv;
}
static void
ac(const char *file)
{
struct utmpx_entry *lp;
struct utmpx *usr, usht;
struct tm *ltm;
struct timeval prev_secs, ut_timecopy, secs, clock_shift, now;
int day;
day = -1;
timerclear(&prev_secs);
timerclear(&secs);
timerclear(&clock_shift);
if (setutxdb(UTXDB_LOG, file) != 0)
err(1, "%s", file);
while ((usr = getutxent()) != NULL) {
ut_timecopy = usr->ut_tv;
if (timercmp(&ut_timecopy, &prev_secs, <))
ut_timecopy = prev_secs;
prev_secs = ut_timecopy;
if (!timerisset(&FirstTime))
FirstTime = ut_timecopy;
if (Flags & AC_D) {
ltm = localtime(&ut_timecopy.tv_sec);
if (day >= 0 && day != ltm->tm_yday) {
day = ltm->tm_yday;
secs = ut_timecopy;
secs.tv_sec -= ltm->tm_sec;
secs.tv_sec -= 60 * ltm->tm_min;
secs.tv_sec -= 3600 * ltm->tm_hour;
secs.tv_usec = 0;
show_today(secs);
} else
day = ltm->tm_yday;
}
switch(usr->ut_type) {
case OLD_TIME:
clock_shift = ut_timecopy;
break;
case NEW_TIME:
timersub(&clock_shift, &ut_timecopy, &clock_shift);
SLIST_FOREACH(lp, &CurUtmpx, next)
timersub(&lp->time, &clock_shift, &lp->time);
break;
case BOOT_TIME:
case SHUTDOWN_TIME:
log_out(usr);
FirstTime = ut_timecopy;
break;
case USER_PROCESS:
if (strncmp(usr->ut_line, "pts/", 4) != 0 ||
*usr->ut_host != '\0')
log_in(usr);
break;
case DEAD_PROCESS:
log_out(usr);
break;
}
}
endutxent();
(void)gettimeofday(&now, NULL);
if (Flags & AC_W)
usht.ut_tv = ut_timecopy;
else
usht.ut_tv = now;
usht.ut_type = SHUTDOWN_TIME;
if (Flags & AC_D) {
ltm = localtime(&ut_timecopy.tv_sec);
if (day >= 0 && day != ltm->tm_yday) {
secs = ut_timecopy;
secs.tv_sec -= ltm->tm_sec;
secs.tv_sec -= 60 * ltm->tm_min;
secs.tv_sec -= 3600 * ltm->tm_hour;
secs.tv_usec = 0;
show_today(secs);
}
}
log_out(&usht);
if (Flags & AC_D)
show_today(now);
else {
if (Flags & AC_P)
show_users();
show("total", Total);
}
}
static void
usage(void)
{
(void)fprintf(stderr,
#ifdef CONSOLE_TTY
"ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
#else
"ac [-dp] [-t tty] [-w wtmp] [users ...]\n");
#endif
exit(1);
}