* Copyright (c) 1994 Christopher G. Demetriou.
* @(#)Copyright (c) 1994, Simon J. Gerraty.
* This is free software. It comes with NO WARRANTY.
* Permission to use, modify and distribute this source code
* is granted subject to the following conditions.
* 1/ that the above copyright notice and this notice
* are preserved in all copies and that due credit be given
* 2/ that any changes to this code are clearly commented
* as such so that the author does not get blamed for bugs
static char rcsid
[] = "$Id: ac.c,v 1.3 1994/05/01 04:39:35 cgd Exp $";
* this is for our list of currently logged in sessions
* this is for our list of users that are accumulating time.
char name
[UT_NAMESIZE
+1];
* this is for chosing whether to ignore a login
char name
[UT_LINESIZE
+3];
static char *Console
= CONSOLE_TTY
;
static time_t FirstTime
= 0;
static struct user_list
*Users
= NULL
;
static struct tty_list
*Ttys
= NULL
;
#define NEW(type) (type *)malloc(sizeof (type))
#define AC_W 1 /* not _PATH_WTMP */
#define AC_D 2 /* daily totals (ignore -p) */
#define AC_P 4 /* per-user totals */
#define AC_U 8 /* specified users only */
#define AC_T 16 /* specified ttys only */
int main
__P((int, char **));
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
*));
int on_console
__P((struct utmp_list
*));
void show
__P((char *, time_t));
void show_today
__P((struct user_list
*, struct utmp_list
*,
void show_users
__P((struct user_list
*));
struct user_list
*update_user
__P((struct user_list
*, char *, time_t));
if ((fp
= fopen(name
, "r")) == NULL
)
/* in case we want to discriminate */
if (strcmp(_PATH_WTMP
, name
))
if ((tp
= NEW(struct tty_list
)) == NULL
)
tp
->len
= 0; /* full match */
tp
->ret
= 1; /* do if match */
if (*name
== '!') { /* don't do if match */
(void)strncpy(tp
->name
, name
, sizeof (tp
->name
) - 1);
tp
->name
[sizeof (tp
->name
) - 1] = '\0';
if ((rcp
= strchr(tp
->name
, '*')) != NULL
) { /* wild card */
tp
->len
= strlen(tp
->name
); /* match len bytes only */
* should we process the named tty?
for (tp
= Ttys
; tp
!= NULL
; tp
= tp
->next
) {
if (tp
->ret
== 0) /* specific don't */
def_ret
= 1; /* default do */
if (strncmp(name
, tp
->name
, tp
->len
) == 0)
if (strncmp(name
, tp
->name
, sizeof (tp
->name
)) == 0)
* is someone logged in on Console?
for (up
= head
; up
; up
= up
->next
) {
if (strncmp(up
->usr
.ut_line
, Console
,
sizeof (up
->usr
.ut_line
)) == 0)
* update user's login time
update_user(head
, name
, secs
)
for (up
= head
; up
!= NULL
; up
= up
->next
) {
if (strncmp(up
->name
, name
, sizeof (up
->name
)) == 0) {
* not found so add new user unless specified users only
if ((up
= NEW(struct user_list
)) == NULL
)
(void)strncpy(up
->name
, name
, sizeof (up
->name
) - 1);
up
->name
[sizeof (up
->name
) - 1] = '\0'; /* paranoid! */
while ((c
= getopt(argc
, argv
, "Dc:dpt:w:")) != EOF
) {
case 't': /* only do specified ttys */
for (; optind
< argc
; optind
++) {
Users
= update_user(Users
, argv
[optind
], 0L);
Flags
|= AC_U
; /* freeze user list */
* if _PATH_WTMP does not exist, exit quietly
if (access(_PATH_WTMP
, 0) != 0 && errno
== ENOENT
)
* print login time in decimal hours
(void)printf("\t%-*s %8.2f\n", UT_NAMESIZE
, name
,
for (lp
= list
; lp
; lp
= lp
->next
)
show(lp
->name
, lp
->secs
);
* print total login time for 24hr period in decimal hours
show_today(users
, logins
, secs
)
struct utmp_list
*logins
;
time_t yesterday
= secs
- 1;
(void)strftime(date
, sizeof (date
), "%b %e total",
/* restore the missing second */
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 */
for (up
= users
; up
!= NULL
; up
= up
->next
) {
up
->secs
= 0; /* for next day */
(void)printf("%s %11.2f\n", date
, ((double)secs
/ 3600));
* log a user out and update their times.
* if ut_line is "~", we log all users out as the system has
struct utmp_list
*lp
, *lp2
, *tlp
;
for (lp
= head
, lp2
= NULL
; lp
!= NULL
; )
if (*up
->ut_line
== '~' || 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
);
printf("%-.*s %-.*s: %-.*s logged out (%2d:%02d:%02d)\n",
sizeof (lp
->usr
.ut_line
), lp
->usr
.ut_line
,
sizeof (lp
->usr
.ut_name
), lp
->usr
.ut_name
,
secs
/ 3600, (secs
% 3600) / 60, secs
% 60);
* if do_tty says ok, login a user
* this could be a login. if we're not dealing with
* the console name, say it is.
* If we are, and if ut_host==":0.0" we know that it
* isn't a real login. _But_ if we have not yet recorded
* someone being logged in on Console - due to the wtmp
* file starting after they logged in, we'll pretend they
* logged in, at the start of the wtmp file.
if (up
->ut_host
[0] == ':') {
* SunOS 4.0.2 does not treat ":0.0" as special but we
* ok, no recorded login, so they were here when wtmp
* started! Adjust ut_time!
* this allows us to pick the right logout
(void)strncpy(up
->ut_line
, Console
, sizeof (up
->ut_line
) - 1);
up
->ut_line
[sizeof (up
->ut_line
) - 1] = '\0'; /* paranoid! */
* If we are doing specified ttys only, we ignore
if (!do_tty(up
->ut_line
))
* go ahead and log them in
if ((lp
= NEW(struct utmp_list
)) == NULL
)
memmove((char *)&lp
->usr
, (char *)up
, sizeof (struct utmp
));
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
);
printf(" (%-.*s)", sizeof (up
->ut_host
), up
->ut_host
);
struct utmp_list
*lp
, *head
= NULL
;
while (fread((char *)&usr
, sizeof(usr
), 1, fp
) == 1) {
ltm
= localtime(&usr
.ut_time
);
if (day
>= 0 && day
!= ltm
->tm_yday
) {
* print yesterday's total
secs
-= 60 * ltm
->tm_min
;
secs
-= 3600 * ltm
->tm_hour
;
show_today(Users
, head
, secs
);
* adjust time for those logged in
for (lp
= head
; lp
!= NULL
; lp
= lp
->next
)
case '~': /* reboot or shutdown */
head
= log_out(head
, &usr
);
FirstTime
= usr
.ut_time
; /* shouldn't be needed */
* if they came in on tty[p-y]*, then it is only
* a login session if the ut_host field is non-empty
if (strncmp(usr
.ut_line
, "tty", 3) != 0 ||
strchr("pqrstuvwxy", usr
.ut_line
[3]) == 0 ||
head
= log_in(head
, &usr
);
head
= log_out(head
, &usr
);
usr
.ut_time
= time((time_t *)0);
(void)strcpy(usr
.ut_line
, "~");
ltm
= localtime(&usr
.ut_time
);
if (day
>= 0 && day
!= ltm
->tm_yday
) {
* print yesterday's total
secs
-= 60 * ltm
->tm_min
;
secs
-= 3600 * ltm
->tm_hour
;
show_today(Users
, head
, secs
);
* anyone still logged in gets time up to now
head
= log_out(head
, &usr
);
show_today(Users
, head
, time((time_t *)0));
"ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
"ac [-dp] [-t tty] [-w wtmp] [users ...]\n");