X-Git-Url: https://git.saurik.com/apple/system_cmds.git/blobdiff_plain/83f6dbe8135dc38ce4ac497ebea7f0ebc87d9199..a8daac8f698fa38f1ca3aaa2a3ed6ac20e5128e2:/ac.tproj/ac.c diff --git a/ac.tproj/ac.c b/ac.tproj/ac.c index 249b80a..a6fc154 100644 --- a/ac.tproj/ac.c +++ b/ac.tproj/ac.c @@ -3,22 +3,21 @@ * * @APPLE_LICENSE_HEADER_START@ * - * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. - * - * This file contains Original Code and/or Modifications of Original Code - * as defined in and that are subject to the Apple Public Source License - * Version 2.0 (the 'License'). You may not use this file except in - * compliance with the License. Please obtain a copy of the License at - * http://www.opensource.apple.com/apsl/ and read it before using this - * file. + * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights + * Reserved. This file contains Original Code and/or Modifications of + * Original Code as defined in and that are subject to the Apple Public + * Source License Version 1.0 (the 'License'). You may not use this file + * except in compliance with the License. Please obtain a copy of the + * License at http://www.apple.com/publicsource and read it before using + * this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. - * Please see the License for the specific language governing rights and - * limitations under the License. + * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the + * License for the specific language governing rights and limitations + * under the License." * * @APPLE_LICENSE_HEADER_END@ */ @@ -38,7 +37,8 @@ */ #ifndef lint -static char rcsid[] = "$Id: ac.c,v 1.1.1.2 2000/01/11 02:09:57 wsanchez Exp $"; +#include +__unused static char rcsid[] = "$Id: ac.c,v 1.2 2006/02/07 05:51:22 lindak Exp $"; #endif #include @@ -50,15 +50,17 @@ static char rcsid[] = "$Id: ac.c,v 1.1.1.2 2000/01/11 02:09:57 wsanchez Exp $"; #include #include #include -#include +#include #include +#define UT_NAMESIZE 8 /* from utmp.h; only for formatting */ + /* * this is for our list of currently logged in sessions */ struct utmp_list { struct utmp_list *next; - struct utmp usr; + struct utmpx usr; }; /* @@ -66,7 +68,7 @@ struct utmp_list { */ struct user_list { struct user_list *next; - char name[UT_NAMESIZE+1]; + char name[_UTX_USERSIZE+1]; time_t secs; }; @@ -75,7 +77,7 @@ struct user_list { */ struct tty_list { struct tty_list *next; - char name[UT_LINESIZE+3]; + char name[_UTX_USERSIZE+3]; int len; int ret; }; @@ -105,12 +107,11 @@ static int Debug = 0; #endif int main __P((int, char **)); -int ac __P((FILE *)); +int ac __P((void)); struct tty_list *add_tty __P((char *)); int do_tty __P((char *)); -FILE *file __P((char *)); -struct utmp_list *log_in __P((struct utmp_list *, struct utmp *)); -struct utmp_list *log_out __P((struct utmp_list *, struct utmp *)); +struct utmp_list *log_in __P((struct utmp_list *, struct utmpx *)); +struct utmp_list *log_out __P((struct utmp_list *, struct utmpx *)); int on_console __P((struct utmp_list *)); void show __P((char *, time_t)); void show_today __P((struct user_list *, struct utmp_list *, @@ -119,23 +120,6 @@ void show_users __P((struct user_list *)); struct user_list *update_user __P((struct user_list *, char *, time_t)); void usage __P((void)); -/* - * open wtmp or die - */ -FILE * -file(name) - char *name; -{ - FILE *fp; - - if ((fp = fopen(name, "r")) == NULL) - err(1, "%s", name); - /* in case we want to discriminate */ - if (strcmp(_PATH_WTMP, name)) - Flags |= AC_W; - return fp; -} - struct tty_list * add_tty(name) char *name; @@ -274,7 +258,7 @@ main(argc, argv) add_tty(optarg); break; case 'w': - fp = file(optarg); + wtmpxname(optarg); break; case '?': default: @@ -293,16 +277,7 @@ main(argc, argv) } if (Flags & AC_D) Flags &= ~AC_P; - if (fp == NULL) { - /* - * if _PATH_WTMP does not exist, exit quietly - */ - if (access(_PATH_WTMP, 0) != 0 && errno == ENOENT) - return 0; - - fp = file(_PATH_WTMP); - } - ac(fp); + ac(); return 0; } @@ -350,9 +325,9 @@ show_today(users, logins, secs) yesterday++; for (lp = logins; lp != NULL; lp = lp->next) { - secs = yesterday - lp->usr.ut_time; - Users = update_user(Users, lp->usr.ut_name, secs); - lp->usr.ut_time = yesterday; /* as if they just logged in */ + secs = yesterday - lp->usr.ut_tv.tv_sec; + Users = update_user(Users, lp->usr.ut_user, secs); + lp->usr.ut_tv.tv_sec = yesterday; /* as if they just logged in */ } secs = 0; for (up = users; up != NULL; up = up->next) { @@ -371,22 +346,22 @@ show_today(users, logins, secs) struct utmp_list * log_out(head, up) struct utmp_list *head; - struct utmp *up; + struct utmpx *up; { struct utmp_list *lp, *lp2, *tlp; time_t secs; for (lp = head, lp2 = NULL; lp != NULL; ) - if (*up->ut_line == '~' || strncmp(lp->usr.ut_line, up->ut_line, + if (up->ut_type == BOOT_TIME || up->ut_type == SHUTDOWN_TIME || strncmp(lp->usr.ut_line, up->ut_line, sizeof (up->ut_line)) == 0) { - secs = up->ut_time - lp->usr.ut_time; - Users = update_user(Users, lp->usr.ut_name, secs); + secs = up->ut_tv.tv_sec - lp->usr.ut_tv.tv_sec; + Users = update_user(Users, lp->usr.ut_user, secs); #ifdef DEBUG if (Debug) printf("%-.*s %-.*s: %-.*s logged out (%2d:%02d:%02d)\n", - 19, ctime(&up->ut_time), - sizeof (lp->usr.ut_line), lp->usr.ut_line, - sizeof (lp->usr.ut_name), lp->usr.ut_name, + 19, ctime(&up->ut_tv.tv_sec), + (int)sizeof (lp->usr.ut_line), lp->usr.ut_line, + (int)sizeof (lp->usr.ut_user), lp->usr.ut_user, secs / 3600, (secs % 3600) / 60, secs % 60); #endif /* @@ -413,7 +388,7 @@ log_out(head, up) struct utmp_list * log_in(head, up) struct utmp_list *head; - struct utmp *up; + struct utmpx *up; { struct utmp_list *lp; @@ -438,9 +413,9 @@ log_in(head, up) return head; /* * ok, no recorded login, so they were here when wtmp - * started! Adjust ut_time! + * started! Adjust ut_tv.tv_sec! */ - up->ut_time = FirstTime; + up->ut_tv.tv_sec = FirstTime; /* * this allows us to pick the right logout */ @@ -463,14 +438,14 @@ log_in(head, up) err(1, "malloc"); lp->next = head; head = lp; - memmove((char *)&lp->usr, (char *)up, sizeof (struct utmp)); + memmove((char *)&lp->usr, (char *)up, sizeof (struct utmpx)); #ifdef DEBUG if (Debug) { printf("%-.*s %-.*s: %-.*s logged in", 19, - ctime(&lp->usr.ut_time), sizeof (up->ut_line), - up->ut_line, sizeof (up->ut_name), up->ut_name); + ctime(&lp->usr.ut_tv.tv_sec), (int)sizeof (up->ut_line), + up->ut_line, (int)sizeof (up->ut_user), up->ut_user); if (*up->ut_host) - printf(" (%-.*s)", sizeof (up->ut_host), up->ut_host); + printf(" (%-.*s)", (int)sizeof (up->ut_host), up->ut_host); putchar('\n'); } #endif @@ -478,26 +453,26 @@ log_in(head, up) } int -ac(fp) - FILE *fp; +ac() { struct utmp_list *lp, *head = NULL; - struct utmp usr; + struct utmpx *u, end; struct tm *ltm; time_t secs = 0; int day = -1; - while (fread((char *)&usr, sizeof(usr), 1, fp) == 1) { + setutxent_wtmp(1); /* read in forward direction */ + while ((u = getutxent_wtmp()) != NULL) { if (!FirstTime) - FirstTime = usr.ut_time; + FirstTime = u->ut_tv.tv_sec; if (Flags & AC_D) { - ltm = localtime(&usr.ut_time); + ltm = localtime(&u->ut_tv.tv_sec); if (day >= 0 && day != ltm->tm_yday) { day = ltm->tm_yday; /* * print yesterday's total */ - secs = usr.ut_time; + secs = u->ut_tv.tv_sec; secs -= ltm->tm_sec; secs -= 60 * ltm->tm_min; secs -= 3600 * ltm->tm_hour; @@ -505,48 +480,50 @@ ac(fp) } else day = ltm->tm_yday; } - switch(*usr.ut_line) { - case '|': - secs = usr.ut_time; + switch(u->ut_type) { + case OLD_TIME: + secs = u->ut_tv.tv_sec; break; - case '{': - secs -= usr.ut_time; + case NEW_TIME: + secs -= u->ut_tv.tv_sec; /* * adjust time for those logged in */ for (lp = head; lp != NULL; lp = lp->next) - lp->usr.ut_time -= secs; + lp->usr.ut_tv.tv_sec -= secs; break; - case '~': /* reboot or shutdown */ - head = log_out(head, &usr); - FirstTime = usr.ut_time; /* shouldn't be needed */ + case BOOT_TIME: /* reboot or shutdown */ + case SHUTDOWN_TIME: + head = log_out(head, u); + FirstTime = u->ut_tv.tv_sec; /* shouldn't be needed */ break; - default: + case USER_PROCESS: /* * if they came in on tty[p-y]*, then it is only * a login session if the ut_host field is non-empty */ - if (*usr.ut_name) { - if (strncmp(usr.ut_line, "tty", 3) != 0 || - strchr("pqrstuvwxy", usr.ut_line[3]) == 0 || - *usr.ut_host != '\0') - head = log_in(head, &usr); - } else - head = log_out(head, &usr); + if (strncmp(u->ut_line, "tty", 3) != 0 || + strchr("pqrstuvwxy", u->ut_line[3]) == 0 || + *u->ut_host != '\0') + head = log_in(head, u); + break; + case DEAD_PROCESS: + head = log_out(head, u); break; } } - (void)fclose(fp); - usr.ut_time = time((time_t *)0); - (void)strcpy(usr.ut_line, "~"); + endutxent_wtmp(); + bzero(&end, sizeof(end)); + end.ut_tv.tv_sec = time((time_t *)0); + end.ut_type = SHUTDOWN_TIME; if (Flags & AC_D) { - ltm = localtime(&usr.ut_time); + ltm = localtime(&end.ut_tv.tv_sec); if (day >= 0 && day != ltm->tm_yday) { /* * print yesterday's total */ - secs = usr.ut_time; + secs = end.ut_tv.tv_sec; secs -= ltm->tm_sec; secs -= 60 * ltm->tm_min; secs -= 3600 * ltm->tm_hour; @@ -556,7 +533,7 @@ ac(fp) /* * anyone still logged in gets time up to now */ - head = log_out(head, &usr); + head = log_out(head, &end); if (Flags & AC_D) show_today(Users, head, time((time_t *)0));