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