1ff9c3a32SDavid Greenman /* 2ff9c3a32SDavid Greenman * Copyright (c) 1994 Christopher G. Demetriou. 3ff9c3a32SDavid Greenman * @(#)Copyright (c) 1994, Simon J. Gerraty. 4ff9c3a32SDavid Greenman * 5ff9c3a32SDavid Greenman * This is free software. It comes with NO WARRANTY. 6ff9c3a32SDavid Greenman * Permission to use, modify and distribute this source code 7ff9c3a32SDavid Greenman * is granted subject to the following conditions. 8ff9c3a32SDavid Greenman * 1/ that the above copyright notice and this notice 9ff9c3a32SDavid Greenman * are preserved in all copies and that due credit be given 10ff9c3a32SDavid Greenman * to the author. 11ff9c3a32SDavid Greenman * 2/ that any changes to this code are clearly commented 12ff9c3a32SDavid Greenman * as such so that the author does not get blamed for bugs 13ff9c3a32SDavid Greenman * other than his own. 14ff9c3a32SDavid Greenman */ 15ff9c3a32SDavid Greenman 16b51547cfSPhilippe Charnier #include <sys/cdefs.h> 17b51547cfSPhilippe Charnier __FBSDID("$FreeBSD$"); 18ff9c3a32SDavid Greenman 19ff9c3a32SDavid Greenman #include <sys/types.h> 20ff9c3a32SDavid Greenman #include <sys/time.h> 21ff9c3a32SDavid Greenman #include <err.h> 22ff9c3a32SDavid Greenman #include <errno.h> 23497c7558SAndrey A. Chernov #include <langinfo.h> 243efa2f58SPhilippe Charnier #include <locale.h> 25ff9c3a32SDavid Greenman #include <stdio.h> 26ff9c3a32SDavid Greenman #include <stdlib.h> 27ff9c3a32SDavid Greenman #include <string.h> 28db774d60SBruce Evans #include <timeconv.h> 29ff9c3a32SDavid Greenman #include <unistd.h> 303efa2f58SPhilippe Charnier #include <utmp.h> 31ff9c3a32SDavid Greenman 32ff9c3a32SDavid Greenman /* 33ff9c3a32SDavid Greenman * this is for our list of currently logged in sessions 34ff9c3a32SDavid Greenman */ 35ff9c3a32SDavid Greenman struct utmp_list { 36ff9c3a32SDavid Greenman struct utmp_list *next; 37ff9c3a32SDavid Greenman struct utmp usr; 38ff9c3a32SDavid Greenman }; 39ff9c3a32SDavid Greenman 40ff9c3a32SDavid Greenman /* 41ff9c3a32SDavid Greenman * this is for our list of users that are accumulating time. 42ff9c3a32SDavid Greenman */ 43ff9c3a32SDavid Greenman struct user_list { 44ff9c3a32SDavid Greenman struct user_list *next; 45ff9c3a32SDavid Greenman char name[UT_NAMESIZE+1]; 46ff9c3a32SDavid Greenman time_t secs; 47ff9c3a32SDavid Greenman }; 48ff9c3a32SDavid Greenman 49ff9c3a32SDavid Greenman /* 50ff9c3a32SDavid Greenman * this is for chosing whether to ignore a login 51ff9c3a32SDavid Greenman */ 52ff9c3a32SDavid Greenman struct tty_list { 53ff9c3a32SDavid Greenman struct tty_list *next; 54ff9c3a32SDavid Greenman char name[UT_LINESIZE+3]; 55b51547cfSPhilippe Charnier size_t len; 56ff9c3a32SDavid Greenman int ret; 57ff9c3a32SDavid Greenman }; 58ff9c3a32SDavid Greenman 59ff9c3a32SDavid Greenman /* 60ff9c3a32SDavid Greenman * globals - yes yuk 61ff9c3a32SDavid Greenman */ 62ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY 63ff9c3a32SDavid Greenman static char *Console = CONSOLE_TTY; 64ff9c3a32SDavid Greenman #endif 65ff9c3a32SDavid Greenman static time_t Total = 0; 66ff9c3a32SDavid Greenman static time_t FirstTime = 0; 67ff9c3a32SDavid Greenman static int Flags = 0; 68ff9c3a32SDavid Greenman static struct user_list *Users = NULL; 69ff9c3a32SDavid Greenman static struct tty_list *Ttys = NULL; 70ff9c3a32SDavid Greenman 71ff9c3a32SDavid Greenman #define NEW(type) (type *)malloc(sizeof (type)) 72ff9c3a32SDavid Greenman 73ff9c3a32SDavid Greenman #define AC_W 1 /* not _PATH_WTMP */ 74ff9c3a32SDavid Greenman #define AC_D 2 /* daily totals (ignore -p) */ 75ff9c3a32SDavid Greenman #define AC_P 4 /* per-user totals */ 76ff9c3a32SDavid Greenman #define AC_U 8 /* specified users only */ 77ff9c3a32SDavid Greenman #define AC_T 16 /* specified ttys only */ 78ff9c3a32SDavid Greenman 79ff9c3a32SDavid Greenman #ifdef DEBUG 80ff9c3a32SDavid Greenman static int Debug = 0; 81ff9c3a32SDavid Greenman #endif 82ff9c3a32SDavid Greenman 83d89167b4SAlfred Perlstein int main(int, char **); 84d89167b4SAlfred Perlstein int ac(FILE *); 85d89167b4SAlfred Perlstein struct tty_list *add_tty(char *); 86d89167b4SAlfred Perlstein int do_tty(char *); 87d89167b4SAlfred Perlstein FILE *file(const char *); 88d89167b4SAlfred Perlstein struct utmp_list *log_in(struct utmp_list *, struct utmp *); 89d89167b4SAlfred Perlstein struct utmp_list *log_out(struct utmp_list *, struct utmp *); 90d89167b4SAlfred Perlstein int on_console(struct utmp_list *); 91d89167b4SAlfred Perlstein void show(const char *, time_t); 9254b60d7eSAlfred Perlstein void show_today(struct user_list *, struct utmp_list *, 9354b60d7eSAlfred Perlstein time_t); 94d89167b4SAlfred Perlstein void show_users(struct user_list *); 95d89167b4SAlfred Perlstein struct user_list *update_user(struct user_list *, char *, time_t); 96d89167b4SAlfred Perlstein void usage(void); 97ff9c3a32SDavid Greenman 98ff9c3a32SDavid Greenman /* 99ff9c3a32SDavid Greenman * open wtmp or die 100ff9c3a32SDavid Greenman */ 101ff9c3a32SDavid Greenman FILE * 1027f761c52SGarance A Drosehn file(const char *name) 103ff9c3a32SDavid Greenman { 104ff9c3a32SDavid Greenman FILE *fp; 105ff9c3a32SDavid Greenman 106ff9c3a32SDavid Greenman if ((fp = fopen(name, "r")) == NULL) 107ff9c3a32SDavid Greenman err(1, "%s", name); 108ff9c3a32SDavid Greenman /* in case we want to discriminate */ 109ff9c3a32SDavid Greenman if (strcmp(_PATH_WTMP, name)) 110ff9c3a32SDavid Greenman Flags |= AC_W; 111ff9c3a32SDavid Greenman return fp; 112ff9c3a32SDavid Greenman } 113ff9c3a32SDavid Greenman 114ff9c3a32SDavid Greenman struct tty_list * 1157f761c52SGarance A Drosehn add_tty(char *name) 116ff9c3a32SDavid Greenman { 117ff9c3a32SDavid Greenman struct tty_list *tp; 1182adaffb0SGarance A Drosehn char *rcp; 119ff9c3a32SDavid Greenman 120ff9c3a32SDavid Greenman Flags |= AC_T; 121ff9c3a32SDavid Greenman 122ff9c3a32SDavid Greenman if ((tp = NEW(struct tty_list)) == NULL) 123c3737d34SPhilippe Charnier errx(1, "malloc failed"); 124ff9c3a32SDavid Greenman tp->len = 0; /* full match */ 125ff9c3a32SDavid Greenman tp->ret = 1; /* do if match */ 126ff9c3a32SDavid Greenman if (*name == '!') { /* don't do if match */ 127ff9c3a32SDavid Greenman tp->ret = 0; 128ff9c3a32SDavid Greenman name++; 129ff9c3a32SDavid Greenman } 130fd96447aSKris Kennaway strlcpy(tp->name, name, sizeof (tp->name)); 131ff9c3a32SDavid Greenman if ((rcp = strchr(tp->name, '*')) != NULL) { /* wild card */ 132ff9c3a32SDavid Greenman *rcp = '\0'; 133ff9c3a32SDavid Greenman tp->len = strlen(tp->name); /* match len bytes only */ 134ff9c3a32SDavid Greenman } 135ff9c3a32SDavid Greenman tp->next = Ttys; 136ff9c3a32SDavid Greenman Ttys = tp; 137ff9c3a32SDavid Greenman return Ttys; 138ff9c3a32SDavid Greenman } 139ff9c3a32SDavid Greenman 140ff9c3a32SDavid Greenman /* 141ff9c3a32SDavid Greenman * should we process the named tty? 142ff9c3a32SDavid Greenman */ 143ff9c3a32SDavid Greenman int 1447f761c52SGarance A Drosehn do_tty(char *name) 145ff9c3a32SDavid Greenman { 146ff9c3a32SDavid Greenman struct tty_list *tp; 147ff9c3a32SDavid Greenman int def_ret = 0; 148ff9c3a32SDavid Greenman 149ff9c3a32SDavid Greenman for (tp = Ttys; tp != NULL; tp = tp->next) { 150ff9c3a32SDavid Greenman if (tp->ret == 0) /* specific don't */ 151ff9c3a32SDavid Greenman def_ret = 1; /* default do */ 152ff9c3a32SDavid Greenman if (tp->len != 0) { 153ff9c3a32SDavid Greenman if (strncmp(name, tp->name, tp->len) == 0) 154ff9c3a32SDavid Greenman return tp->ret; 155ff9c3a32SDavid Greenman } else { 156ff9c3a32SDavid Greenman if (strncmp(name, tp->name, sizeof (tp->name)) == 0) 157ff9c3a32SDavid Greenman return tp->ret; 158ff9c3a32SDavid Greenman } 159ff9c3a32SDavid Greenman } 160ff9c3a32SDavid Greenman return def_ret; 161ff9c3a32SDavid Greenman } 162ff9c3a32SDavid Greenman 163ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY 164ff9c3a32SDavid Greenman /* 165ff9c3a32SDavid Greenman * is someone logged in on Console? 166ff9c3a32SDavid Greenman */ 167ff9c3a32SDavid Greenman int 1687f761c52SGarance A Drosehn on_console(struct utmp_list *head) 169ff9c3a32SDavid Greenman { 170ff9c3a32SDavid Greenman struct utmp_list *up; 171ff9c3a32SDavid Greenman 172ff9c3a32SDavid Greenman for (up = head; up; up = up->next) { 173ff9c3a32SDavid Greenman if (strncmp(up->usr.ut_line, Console, 174ff9c3a32SDavid Greenman sizeof (up->usr.ut_line)) == 0) 175ff9c3a32SDavid Greenman return 1; 176ff9c3a32SDavid Greenman } 177ff9c3a32SDavid Greenman return 0; 178ff9c3a32SDavid Greenman } 179ff9c3a32SDavid Greenman #endif 180ff9c3a32SDavid Greenman 181ff9c3a32SDavid Greenman /* 182ff9c3a32SDavid Greenman * update user's login time 183ff9c3a32SDavid Greenman */ 184ff9c3a32SDavid Greenman struct user_list * 1857f761c52SGarance A Drosehn update_user(struct user_list *head, char *name, time_t secs) 186ff9c3a32SDavid Greenman { 187ff9c3a32SDavid Greenman struct user_list *up; 188ff9c3a32SDavid Greenman 189ff9c3a32SDavid Greenman for (up = head; up != NULL; up = up->next) { 1903f6dabb2SAndrey A. Chernov if (strncmp(up->name, name, UT_NAMESIZE) == 0) { 191ff9c3a32SDavid Greenman up->secs += secs; 192ff9c3a32SDavid Greenman Total += secs; 193ff9c3a32SDavid Greenman return head; 194ff9c3a32SDavid Greenman } 195ff9c3a32SDavid Greenman } 196ff9c3a32SDavid Greenman /* 197ff9c3a32SDavid Greenman * not found so add new user unless specified users only 198ff9c3a32SDavid Greenman */ 199ff9c3a32SDavid Greenman if (Flags & AC_U) 200ff9c3a32SDavid Greenman return head; 201ff9c3a32SDavid Greenman 202ff9c3a32SDavid Greenman if ((up = NEW(struct user_list)) == NULL) 203c3737d34SPhilippe Charnier errx(1, "malloc failed"); 204ff9c3a32SDavid Greenman up->next = head; 205fd96447aSKris Kennaway strlcpy(up->name, name, sizeof (up->name)); 206ff9c3a32SDavid Greenman up->secs = secs; 207ff9c3a32SDavid Greenman Total += secs; 208ff9c3a32SDavid Greenman return up; 209ff9c3a32SDavid Greenman } 210ff9c3a32SDavid Greenman 211ff9c3a32SDavid Greenman int 2127f761c52SGarance A Drosehn main(int argc, char *argv[]) 213ff9c3a32SDavid Greenman { 214ff9c3a32SDavid Greenman FILE *fp; 215ff9c3a32SDavid Greenman int c; 216ff9c3a32SDavid Greenman 2175877948cSAndrey A. Chernov (void) setlocale(LC_TIME, ""); 2185877948cSAndrey A. Chernov 219ff9c3a32SDavid Greenman fp = NULL; 2206c3f552aSWarner Losh while ((c = getopt(argc, argv, "Dc:dpt:w:")) != -1) { 221ff9c3a32SDavid Greenman switch (c) { 222ff9c3a32SDavid Greenman #ifdef DEBUG 223ff9c3a32SDavid Greenman case 'D': 224ff9c3a32SDavid Greenman Debug++; 225ff9c3a32SDavid Greenman break; 226ff9c3a32SDavid Greenman #endif 227ff9c3a32SDavid Greenman case 'c': 228ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY 229ff9c3a32SDavid Greenman Console = optarg; 230ff9c3a32SDavid Greenman #else 231ff9c3a32SDavid Greenman usage(); /* XXX */ 232ff9c3a32SDavid Greenman #endif 233ff9c3a32SDavid Greenman break; 234ff9c3a32SDavid Greenman case 'd': 235ff9c3a32SDavid Greenman Flags |= AC_D; 236ff9c3a32SDavid Greenman break; 237ff9c3a32SDavid Greenman case 'p': 238ff9c3a32SDavid Greenman Flags |= AC_P; 239ff9c3a32SDavid Greenman break; 240ff9c3a32SDavid Greenman case 't': /* only do specified ttys */ 241ff9c3a32SDavid Greenman add_tty(optarg); 242ff9c3a32SDavid Greenman break; 243ff9c3a32SDavid Greenman case 'w': 244ff9c3a32SDavid Greenman fp = file(optarg); 245ff9c3a32SDavid Greenman break; 246ff9c3a32SDavid Greenman case '?': 247ff9c3a32SDavid Greenman default: 248ff9c3a32SDavid Greenman usage(); 249ff9c3a32SDavid Greenman break; 250ff9c3a32SDavid Greenman } 251ff9c3a32SDavid Greenman } 252ff9c3a32SDavid Greenman if (optind < argc) { 253ff9c3a32SDavid Greenman /* 254ff9c3a32SDavid Greenman * initialize user list 255ff9c3a32SDavid Greenman */ 256ff9c3a32SDavid Greenman for (; optind < argc; optind++) { 257b51547cfSPhilippe Charnier Users = update_user(Users, argv[optind], (time_t)0); 258ff9c3a32SDavid Greenman } 259ff9c3a32SDavid Greenman Flags |= AC_U; /* freeze user list */ 260ff9c3a32SDavid Greenman } 261ff9c3a32SDavid Greenman if (Flags & AC_D) 262ff9c3a32SDavid Greenman Flags &= ~AC_P; 263ff9c3a32SDavid Greenman if (fp == NULL) { 264ff9c3a32SDavid Greenman /* 265ff9c3a32SDavid Greenman * if _PATH_WTMP does not exist, exit quietly 266ff9c3a32SDavid Greenman */ 267ff9c3a32SDavid Greenman if (access(_PATH_WTMP, 0) != 0 && errno == ENOENT) 268ff9c3a32SDavid Greenman return 0; 269ff9c3a32SDavid Greenman 270ff9c3a32SDavid Greenman fp = file(_PATH_WTMP); 271ff9c3a32SDavid Greenman } 272ff9c3a32SDavid Greenman ac(fp); 273ff9c3a32SDavid Greenman 274ff9c3a32SDavid Greenman return 0; 275ff9c3a32SDavid Greenman } 276ff9c3a32SDavid Greenman 277ff9c3a32SDavid Greenman /* 278ff9c3a32SDavid Greenman * print login time in decimal hours 279ff9c3a32SDavid Greenman */ 280ff9c3a32SDavid Greenman void 2817f761c52SGarance A Drosehn show(const char *name, time_t secs) 282ff9c3a32SDavid Greenman { 283ff9c3a32SDavid Greenman (void)printf("\t%-*s %8.2f\n", UT_NAMESIZE, name, 284ff9c3a32SDavid Greenman ((double)secs / 3600)); 285ff9c3a32SDavid Greenman } 286ff9c3a32SDavid Greenman 287ff9c3a32SDavid Greenman void 2887f761c52SGarance A Drosehn show_users(struct user_list *list) 289ff9c3a32SDavid Greenman { 290ff9c3a32SDavid Greenman struct user_list *lp; 291ff9c3a32SDavid Greenman 292ff9c3a32SDavid Greenman for (lp = list; lp; lp = lp->next) 293ff9c3a32SDavid Greenman show(lp->name, lp->secs); 294ff9c3a32SDavid Greenman } 295ff9c3a32SDavid Greenman 296ff9c3a32SDavid Greenman /* 297ff9c3a32SDavid Greenman * print total login time for 24hr period in decimal hours 298ff9c3a32SDavid Greenman */ 299ff9c3a32SDavid Greenman void 3007f761c52SGarance A Drosehn show_today(struct user_list *users, struct utmp_list *logins, time_t secs) 301ff9c3a32SDavid Greenman { 302ff9c3a32SDavid Greenman struct user_list *up; 303ff9c3a32SDavid Greenman struct utmp_list *lp; 304ff9c3a32SDavid Greenman char date[64]; 305ff9c3a32SDavid Greenman time_t yesterday = secs - 1; 306497c7558SAndrey A. Chernov static int d_first = -1; 307ff9c3a32SDavid Greenman 308497c7558SAndrey A. Chernov if (d_first < 0) 309497c7558SAndrey A. Chernov d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 310497c7558SAndrey A. Chernov (void)strftime(date, sizeof (date), 311497c7558SAndrey A. Chernov d_first ? "%e %b total" : "%b %e total", 312ff9c3a32SDavid Greenman localtime(&yesterday)); 313ff9c3a32SDavid Greenman 314ff9c3a32SDavid Greenman /* restore the missing second */ 315ff9c3a32SDavid Greenman yesterday++; 316ff9c3a32SDavid Greenman 317ff9c3a32SDavid Greenman for (lp = logins; lp != NULL; lp = lp->next) { 318ff9c3a32SDavid Greenman secs = yesterday - lp->usr.ut_time; 319ff9c3a32SDavid Greenman Users = update_user(Users, lp->usr.ut_name, secs); 320ff9c3a32SDavid Greenman lp->usr.ut_time = yesterday; /* as if they just logged in */ 321ff9c3a32SDavid Greenman } 322ff9c3a32SDavid Greenman secs = 0; 323ff9c3a32SDavid Greenman for (up = users; up != NULL; up = up->next) { 324ff9c3a32SDavid Greenman secs += up->secs; 325ff9c3a32SDavid Greenman up->secs = 0; /* for next day */ 326ff9c3a32SDavid Greenman } 327ff9c3a32SDavid Greenman if (secs) 328ff9c3a32SDavid Greenman (void)printf("%s %11.2f\n", date, ((double)secs / 3600)); 329ff9c3a32SDavid Greenman } 330ff9c3a32SDavid Greenman 331ff9c3a32SDavid Greenman /* 332ff9c3a32SDavid Greenman * log a user out and update their times. 333ff9c3a32SDavid Greenman * if ut_line is "~", we log all users out as the system has 334ff9c3a32SDavid Greenman * been shut down. 335ff9c3a32SDavid Greenman */ 336ff9c3a32SDavid Greenman struct utmp_list * 3377f761c52SGarance A Drosehn log_out(struct utmp_list *head, struct utmp *up) 338ff9c3a32SDavid Greenman { 339ff9c3a32SDavid Greenman struct utmp_list *lp, *lp2, *tlp; 340ff9c3a32SDavid Greenman time_t secs; 341ff9c3a32SDavid Greenman 342ff9c3a32SDavid Greenman for (lp = head, lp2 = NULL; lp != NULL; ) 343ff9c3a32SDavid Greenman if (*up->ut_line == '~' || strncmp(lp->usr.ut_line, up->ut_line, 344ff9c3a32SDavid Greenman sizeof (up->ut_line)) == 0) { 345ff9c3a32SDavid Greenman secs = up->ut_time - lp->usr.ut_time; 346ff9c3a32SDavid Greenman Users = update_user(Users, lp->usr.ut_name, secs); 347ff9c3a32SDavid Greenman #ifdef DEBUG 348ff9c3a32SDavid Greenman if (Debug) 349ff9c3a32SDavid Greenman printf("%-.*s %-.*s: %-.*s logged out (%2d:%02d:%02d)\n", 350ff9c3a32SDavid Greenman 19, ctime(&up->ut_time), 351ff9c3a32SDavid Greenman sizeof (lp->usr.ut_line), lp->usr.ut_line, 352ff9c3a32SDavid Greenman sizeof (lp->usr.ut_name), lp->usr.ut_name, 353ff9c3a32SDavid Greenman secs / 3600, (secs % 3600) / 60, secs % 60); 354ff9c3a32SDavid Greenman #endif 355ff9c3a32SDavid Greenman /* 356ff9c3a32SDavid Greenman * now lose it 357ff9c3a32SDavid Greenman */ 358ff9c3a32SDavid Greenman tlp = lp; 359ff9c3a32SDavid Greenman lp = lp->next; 360ff9c3a32SDavid Greenman if (tlp == head) 361ff9c3a32SDavid Greenman head = lp; 362ff9c3a32SDavid Greenman else if (lp2 != NULL) 363ff9c3a32SDavid Greenman lp2->next = lp; 364ff9c3a32SDavid Greenman free(tlp); 365ff9c3a32SDavid Greenman } else { 366ff9c3a32SDavid Greenman lp2 = lp; 367ff9c3a32SDavid Greenman lp = lp->next; 368ff9c3a32SDavid Greenman } 369ff9c3a32SDavid Greenman return head; 370ff9c3a32SDavid Greenman } 371ff9c3a32SDavid Greenman 372ff9c3a32SDavid Greenman 373ff9c3a32SDavid Greenman /* 374ff9c3a32SDavid Greenman * if do_tty says ok, login a user 375ff9c3a32SDavid Greenman */ 376ff9c3a32SDavid Greenman struct utmp_list * 3777f761c52SGarance A Drosehn log_in(struct utmp_list *head, struct utmp *up) 378ff9c3a32SDavid Greenman { 379ff9c3a32SDavid Greenman struct utmp_list *lp; 380ff9c3a32SDavid Greenman 381ff9c3a32SDavid Greenman /* 382ff9c3a32SDavid Greenman * this could be a login. if we're not dealing with 383ff9c3a32SDavid Greenman * the console name, say it is. 384ff9c3a32SDavid Greenman * 385ff9c3a32SDavid Greenman * If we are, and if ut_host==":0.0" we know that it 386ff9c3a32SDavid Greenman * isn't a real login. _But_ if we have not yet recorded 387ff9c3a32SDavid Greenman * someone being logged in on Console - due to the wtmp 388ff9c3a32SDavid Greenman * file starting after they logged in, we'll pretend they 389ff9c3a32SDavid Greenman * logged in, at the start of the wtmp file. 390ff9c3a32SDavid Greenman */ 391ff9c3a32SDavid Greenman 392ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY 393ff9c3a32SDavid Greenman if (up->ut_host[0] == ':') { 394ff9c3a32SDavid Greenman /* 395ff9c3a32SDavid Greenman * SunOS 4.0.2 does not treat ":0.0" as special but we 396ff9c3a32SDavid Greenman * do. 397ff9c3a32SDavid Greenman */ 398ff9c3a32SDavid Greenman if (on_console(head)) 399ff9c3a32SDavid Greenman return head; 400ff9c3a32SDavid Greenman /* 401ff9c3a32SDavid Greenman * ok, no recorded login, so they were here when wtmp 402ff9c3a32SDavid Greenman * started! Adjust ut_time! 403ff9c3a32SDavid Greenman */ 404ff9c3a32SDavid Greenman up->ut_time = FirstTime; 405ff9c3a32SDavid Greenman /* 406ff9c3a32SDavid Greenman * this allows us to pick the right logout 407ff9c3a32SDavid Greenman */ 408fd96447aSKris Kennaway strlcpy(up->ut_line, Console, sizeof (up->ut_line)); 409ff9c3a32SDavid Greenman } 410ff9c3a32SDavid Greenman #endif 411ff9c3a32SDavid Greenman /* 412ff9c3a32SDavid Greenman * If we are doing specified ttys only, we ignore 413ff9c3a32SDavid Greenman * anything else. 414ff9c3a32SDavid Greenman */ 415ff9c3a32SDavid Greenman if (Flags & AC_T) 416ff9c3a32SDavid Greenman if (!do_tty(up->ut_line)) 417ff9c3a32SDavid Greenman return head; 418ff9c3a32SDavid Greenman 419ff9c3a32SDavid Greenman /* 420ff9c3a32SDavid Greenman * go ahead and log them in 421ff9c3a32SDavid Greenman */ 422ff9c3a32SDavid Greenman if ((lp = NEW(struct utmp_list)) == NULL) 423c3737d34SPhilippe Charnier errx(1, "malloc failed"); 424ff9c3a32SDavid Greenman lp->next = head; 425ff9c3a32SDavid Greenman head = lp; 426ff9c3a32SDavid Greenman memmove((char *)&lp->usr, (char *)up, sizeof (struct utmp)); 427ff9c3a32SDavid Greenman #ifdef DEBUG 428ff9c3a32SDavid Greenman if (Debug) { 429ff9c3a32SDavid Greenman printf("%-.*s %-.*s: %-.*s logged in", 19, 430ff9c3a32SDavid Greenman ctime(&lp->usr.ut_time), sizeof (up->ut_line), 431ff9c3a32SDavid Greenman up->ut_line, sizeof (up->ut_name), up->ut_name); 432ff9c3a32SDavid Greenman if (*up->ut_host) 433ff9c3a32SDavid Greenman printf(" (%-.*s)", sizeof (up->ut_host), up->ut_host); 434ff9c3a32SDavid Greenman putchar('\n'); 435ff9c3a32SDavid Greenman } 436ff9c3a32SDavid Greenman #endif 437ff9c3a32SDavid Greenman return head; 438ff9c3a32SDavid Greenman } 439ff9c3a32SDavid Greenman 440ff9c3a32SDavid Greenman int 4417f761c52SGarance A Drosehn ac(FILE *fp) 442ff9c3a32SDavid Greenman { 443ff9c3a32SDavid Greenman struct utmp_list *lp, *head = NULL; 444ff9c3a32SDavid Greenman struct utmp usr; 445ff9c3a32SDavid Greenman struct tm *ltm; 446ff9c3a32SDavid Greenman time_t secs; 447ff9c3a32SDavid Greenman int day = -1; 448ff9c3a32SDavid Greenman 449ff9c3a32SDavid Greenman while (fread((char *)&usr, sizeof(usr), 1, fp) == 1) { 450ff9c3a32SDavid Greenman if (!FirstTime) 451ff9c3a32SDavid Greenman FirstTime = usr.ut_time; 452ff9c3a32SDavid Greenman if (Flags & AC_D) { 453170ac683SMatthew Dillon time_t t = _int_to_time(usr.ut_time); 4542064ef1cSMatthew Dillon ltm = localtime(&t); 455ff9c3a32SDavid Greenman if (day >= 0 && day != ltm->tm_yday) { 456ff9c3a32SDavid Greenman day = ltm->tm_yday; 457ff9c3a32SDavid Greenman /* 458ff9c3a32SDavid Greenman * print yesterday's total 459ff9c3a32SDavid Greenman */ 460ff9c3a32SDavid Greenman secs = usr.ut_time; 461ff9c3a32SDavid Greenman secs -= ltm->tm_sec; 462ff9c3a32SDavid Greenman secs -= 60 * ltm->tm_min; 463ff9c3a32SDavid Greenman secs -= 3600 * ltm->tm_hour; 464ff9c3a32SDavid Greenman show_today(Users, head, secs); 465ff9c3a32SDavid Greenman } else 466ff9c3a32SDavid Greenman day = ltm->tm_yday; 467ff9c3a32SDavid Greenman } 468ff9c3a32SDavid Greenman switch(*usr.ut_line) { 469ff9c3a32SDavid Greenman case '|': 470ff9c3a32SDavid Greenman secs = usr.ut_time; 471ff9c3a32SDavid Greenman break; 472ff9c3a32SDavid Greenman case '{': 473ff9c3a32SDavid Greenman secs -= usr.ut_time; 474ff9c3a32SDavid Greenman /* 475ff9c3a32SDavid Greenman * adjust time for those logged in 476ff9c3a32SDavid Greenman */ 477ff9c3a32SDavid Greenman for (lp = head; lp != NULL; lp = lp->next) 478ff9c3a32SDavid Greenman lp->usr.ut_time -= secs; 479ff9c3a32SDavid Greenman break; 480ff9c3a32SDavid Greenman case '~': /* reboot or shutdown */ 481ff9c3a32SDavid Greenman head = log_out(head, &usr); 482ff9c3a32SDavid Greenman FirstTime = usr.ut_time; /* shouldn't be needed */ 483ff9c3a32SDavid Greenman break; 484ff9c3a32SDavid Greenman default: 485ff9c3a32SDavid Greenman /* 4861ce46cefSPoul-Henning Kamp * if they came in on tty[p-sP-S]*, then it is only 487ff9c3a32SDavid Greenman * a login session if the ut_host field is non-empty 488ff9c3a32SDavid Greenman */ 489ff9c3a32SDavid Greenman if (*usr.ut_name) { 490974a54a2SJonathan Chen if (strncmp(usr.ut_line, "tty", 3) == 0 || 491974a54a2SJonathan Chen strchr("pqrsPQRS", usr.ut_line[3]) != 0 || 492ff9c3a32SDavid Greenman *usr.ut_host != '\0') 493ff9c3a32SDavid Greenman head = log_in(head, &usr); 494ff9c3a32SDavid Greenman } else 495ff9c3a32SDavid Greenman head = log_out(head, &usr); 496ff9c3a32SDavid Greenman break; 497ff9c3a32SDavid Greenman } 498ff9c3a32SDavid Greenman } 499ff9c3a32SDavid Greenman (void)fclose(fp); 5002a30154fSSteve Price if (!(Flags & AC_W)) 501ff9c3a32SDavid Greenman usr.ut_time = time((time_t *)0); 502ff9c3a32SDavid Greenman (void)strcpy(usr.ut_line, "~"); 503ff9c3a32SDavid Greenman 504ff9c3a32SDavid Greenman if (Flags & AC_D) { 505170ac683SMatthew Dillon time_t t = _int_to_time(usr.ut_time); 5062064ef1cSMatthew Dillon ltm = localtime(&t); 507ff9c3a32SDavid Greenman if (day >= 0 && day != ltm->tm_yday) { 508ff9c3a32SDavid Greenman /* 509ff9c3a32SDavid Greenman * print yesterday's total 510ff9c3a32SDavid Greenman */ 511ff9c3a32SDavid Greenman secs = usr.ut_time; 512ff9c3a32SDavid Greenman secs -= ltm->tm_sec; 513ff9c3a32SDavid Greenman secs -= 60 * ltm->tm_min; 514ff9c3a32SDavid Greenman secs -= 3600 * ltm->tm_hour; 515ff9c3a32SDavid Greenman show_today(Users, head, secs); 516ff9c3a32SDavid Greenman } 517ff9c3a32SDavid Greenman } 518ff9c3a32SDavid Greenman /* 519ff9c3a32SDavid Greenman * anyone still logged in gets time up to now 520ff9c3a32SDavid Greenman */ 521ff9c3a32SDavid Greenman head = log_out(head, &usr); 522ff9c3a32SDavid Greenman 523ff9c3a32SDavid Greenman if (Flags & AC_D) 524ff9c3a32SDavid Greenman show_today(Users, head, time((time_t *)0)); 525ff9c3a32SDavid Greenman else { 526ff9c3a32SDavid Greenman if (Flags & AC_P) 527ff9c3a32SDavid Greenman show_users(Users); 528ff9c3a32SDavid Greenman show("total", Total); 529ff9c3a32SDavid Greenman } 530ff9c3a32SDavid Greenman return 0; 531ff9c3a32SDavid Greenman } 532ff9c3a32SDavid Greenman 533ff9c3a32SDavid Greenman void 5347f761c52SGarance A Drosehn usage(void) 535ff9c3a32SDavid Greenman { 536ff9c3a32SDavid Greenman (void)fprintf(stderr, 537ff9c3a32SDavid Greenman #ifdef CONSOLE_TTY 538ff9c3a32SDavid Greenman "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n"); 539ff9c3a32SDavid Greenman #else 540ff9c3a32SDavid Greenman "ac [-dp] [-t tty] [-w wtmp] [users ...]\n"); 541ff9c3a32SDavid Greenman #endif 542ff9c3a32SDavid Greenman exit(1); 543ff9c3a32SDavid Greenman } 544