Home | History | Annotate | Line # | Download | only in ac
ac.c revision 1.23
      1  1.23   jnemeth /* $NetBSD: ac.c,v 1.23 2006/05/26 02:16:17 jnemeth Exp $ */
      2   1.4  christos 
      3   1.1       cgd /*
      4  1.11       cgd  * Copyright (c) 1994 Christopher G. Demetriou
      5  1.11       cgd  * All rights reserved.
      6  1.11       cgd  *
      7  1.11       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.11       cgd  * modification, are permitted provided that the following conditions
      9  1.11       cgd  * are met:
     10  1.11       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.11       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.11       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.11       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.11       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.11       cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.11       cgd  *    must display the following acknowledgement:
     17  1.11       cgd  *          This product includes software developed for the
     18  1.17     grant  *          NetBSD Project.  See http://www.NetBSD.org/ for
     19  1.11       cgd  *          information about NetBSD.
     20  1.11       cgd  * 4. The name of the author may not be used to endorse or promote products
     21  1.11       cgd  *    derived from this software without specific prior written permission.
     22  1.11       cgd  *
     23  1.11       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  1.11       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.11       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.11       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  1.11       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  1.11       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  1.11       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  1.11       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  1.11       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  1.11       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.11       cgd  *
     34  1.11       cgd  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
     35  1.11       cgd  *
     36  1.11       cgd  *
     37   1.1       cgd  *      @(#)Copyright (c) 1994, Simon J. Gerraty.
     38   1.1       cgd  *
     39   1.1       cgd  *      This is free software.  It comes with NO WARRANTY.
     40   1.1       cgd  *      Permission to use, modify and distribute this source code
     41   1.1       cgd  *      is granted subject to the following conditions.
     42   1.1       cgd  *      1/ that the above copyright notice and this notice
     43   1.1       cgd  *      are preserved in all copies and that due credit be given
     44   1.1       cgd  *      to the author.
     45   1.1       cgd  *      2/ that any changes to this code are clearly commented
     46   1.1       cgd  *      as such so that the author does not get blamed for bugs
     47   1.1       cgd  *      other than his own.
     48   1.1       cgd  */
     49   1.1       cgd 
     50   1.6     lukem #include <sys/cdefs.h>
     51   1.1       cgd #ifndef lint
     52  1.23   jnemeth __RCSID("$NetBSD: ac.c,v 1.23 2006/05/26 02:16:17 jnemeth Exp $");
     53   1.1       cgd #endif
     54   1.1       cgd 
     55   1.1       cgd #include <sys/types.h>
     56   1.8    kleink 
     57   1.1       cgd #include <err.h>
     58   1.1       cgd #include <errno.h>
     59   1.1       cgd #include <pwd.h>
     60   1.1       cgd #include <stdio.h>
     61   1.4  christos #include <string.h>
     62   1.1       cgd #include <stdlib.h>
     63   1.4  christos #include <unistd.h>
     64   1.8    kleink #include <time.h>
     65   1.1       cgd #include <utmp.h>
     66   1.4  christos #include <ttyent.h>
     67   1.1       cgd 
     68   1.1       cgd /*
     69   1.1       cgd  * this is for our list of currently logged in sessions
     70   1.1       cgd  */
     71   1.1       cgd struct utmp_list {
     72   1.1       cgd 	struct utmp_list *next;
     73   1.1       cgd 	struct utmp usr;
     74   1.1       cgd };
     75   1.1       cgd 
     76   1.1       cgd /*
     77   1.1       cgd  * this is for our list of users that are accumulating time.
     78   1.1       cgd  */
     79   1.1       cgd struct user_list {
     80   1.1       cgd 	struct user_list *next;
     81   1.2       cgd 	char	name[UT_NAMESIZE+1];
     82   1.1       cgd 	time_t	secs;
     83   1.1       cgd };
     84   1.1       cgd 
     85   1.1       cgd /*
     86   1.1       cgd  * this is for chosing whether to ignore a login
     87   1.1       cgd  */
     88   1.1       cgd struct tty_list {
     89   1.1       cgd 	struct tty_list *next;
     90   1.3       cgd 	char	name[UT_LINESIZE+3];
     91   1.1       cgd 	int	len;
     92   1.1       cgd 	int	ret;
     93   1.1       cgd };
     94   1.1       cgd 
     95   1.1       cgd /*
     96   1.1       cgd  * globals - yes yuk
     97   1.1       cgd  */
     98   1.1       cgd static time_t	Total = 0;
     99   1.1       cgd static time_t	FirstTime = 0;
    100   1.1       cgd static int	Flags = 0;
    101   1.1       cgd static struct user_list *Users = NULL;
    102   1.1       cgd static struct tty_list *Ttys = NULL;
    103   1.4  christos static int Maxcon = 0, Ncon = 0;
    104   1.4  christos static char (*Con)[UT_LINESIZE] = NULL;
    105   1.1       cgd 
    106   1.1       cgd #define NEW(type) (type *)malloc(sizeof (type))
    107   1.1       cgd 
    108   1.4  christos #define is_login_tty(line) \
    109   1.4  christos 	(bsearch(line, Con, Ncon, sizeof(Con[0]), compare) != NULL)
    110   1.4  christos 
    111   1.1       cgd #define	AC_W	1				/* not _PATH_WTMP */
    112   1.1       cgd #define	AC_D	2				/* daily totals (ignore -p) */
    113   1.1       cgd #define	AC_P	4				/* per-user totals */
    114   1.1       cgd #define	AC_U	8				/* specified users only */
    115   1.1       cgd #define	AC_T	16				/* specified ttys only */
    116   1.1       cgd 
    117   1.1       cgd #ifdef DEBUG
    118   1.1       cgd static int Debug = 0;
    119   1.1       cgd #endif
    120   1.1       cgd 
    121  1.21   xtraeme static int		ac(FILE *);
    122  1.21   xtraeme static struct tty_list	*add_tty(char *);
    123  1.21   xtraeme static int		do_tty(char *);
    124  1.21   xtraeme static FILE		*file(const char *);
    125  1.21   xtraeme static struct utmp_list	*log_in(struct utmp_list *, struct utmp *);
    126  1.21   xtraeme static struct utmp_list	*log_out(struct utmp_list *, struct utmp *);
    127   1.4  christos #ifdef notdef
    128  1.21   xtraeme static int		on_console(struct utmp_list *);
    129   1.4  christos #endif
    130  1.21   xtraeme static void		find_login_ttys(void);
    131  1.21   xtraeme static void		show(const char *, time_t);
    132  1.21   xtraeme static void		show_today(struct user_list *, struct utmp_list *,
    133  1.21   xtraeme     time_t);
    134  1.21   xtraeme static void		show_users(struct user_list *);
    135  1.21   xtraeme static struct user_list	*update_user(struct user_list *, char *, time_t);
    136  1.21   xtraeme static int		compare(const void *, const void *);
    137  1.21   xtraeme static void		usage(void);
    138   1.1       cgd 
    139   1.1       cgd /*
    140   1.1       cgd  * open wtmp or die
    141   1.1       cgd  */
    142   1.4  christos static FILE *
    143  1.21   xtraeme file(const char *name)
    144   1.1       cgd {
    145   1.1       cgd 	FILE *fp;
    146   1.1       cgd 
    147  1.12        ad 	if (strcmp(name, "-") == 0)
    148  1.12        ad 		fp = stdin;
    149  1.12        ad 	else if ((fp = fopen(name, "r")) == NULL)
    150   1.1       cgd 		err(1, "%s", name);
    151   1.1       cgd 	/* in case we want to discriminate */
    152   1.1       cgd 	if (strcmp(_PATH_WTMP, name))
    153   1.1       cgd 		Flags |= AC_W;
    154   1.1       cgd 	return fp;
    155   1.1       cgd }
    156   1.1       cgd 
    157   1.4  christos static struct tty_list *
    158  1.21   xtraeme add_tty(char *name)
    159   1.1       cgd {
    160   1.1       cgd 	struct tty_list *tp;
    161   1.7     lukem 	char *rcp;
    162   1.1       cgd 
    163   1.1       cgd 	Flags |= AC_T;
    164   1.1       cgd 
    165   1.1       cgd 	if ((tp = NEW(struct tty_list)) == NULL)
    166   1.1       cgd 		err(1, "malloc");
    167   1.1       cgd 	tp->len = 0;				/* full match */
    168   1.1       cgd 	tp->ret = 1;				/* do if match */
    169   1.1       cgd 	if (*name == '!') {			/* don't do if match */
    170   1.1       cgd 		tp->ret = 0;
    171   1.1       cgd 		name++;
    172   1.1       cgd 	}
    173  1.15    itojun 	(void)strlcpy(tp->name, name, sizeof (tp->name));
    174   1.1       cgd 	if ((rcp = strchr(tp->name, '*')) != NULL) {	/* wild card */
    175   1.1       cgd 		*rcp = '\0';
    176   1.1       cgd 		tp->len = strlen(tp->name);	/* match len bytes only */
    177   1.1       cgd 	}
    178   1.1       cgd 	tp->next = Ttys;
    179   1.1       cgd 	Ttys = tp;
    180   1.1       cgd 	return Ttys;
    181   1.1       cgd }
    182   1.1       cgd 
    183   1.1       cgd /*
    184   1.1       cgd  * should we process the named tty?
    185   1.1       cgd  */
    186   1.4  christos static int
    187  1.21   xtraeme do_tty(char *name)
    188   1.1       cgd {
    189   1.1       cgd 	struct tty_list *tp;
    190   1.1       cgd 	int def_ret = 0;
    191   1.1       cgd 
    192   1.1       cgd 	for (tp = Ttys; tp != NULL; tp = tp->next) {
    193   1.1       cgd 		if (tp->ret == 0)		/* specific don't */
    194   1.1       cgd 			def_ret = 1;		/* default do */
    195   1.1       cgd 		if (tp->len != 0) {
    196   1.1       cgd 			if (strncmp(name, tp->name, tp->len) == 0)
    197   1.1       cgd 				return tp->ret;
    198   1.1       cgd 		} else {
    199   1.3       cgd 			if (strncmp(name, tp->name, sizeof (tp->name)) == 0)
    200   1.1       cgd 				return tp->ret;
    201   1.1       cgd 		}
    202   1.1       cgd 	}
    203   1.1       cgd 	return def_ret;
    204   1.1       cgd }
    205   1.1       cgd 
    206   1.4  christos static int
    207  1.21   xtraeme compare(const void *a, const void *b)
    208   1.4  christos {
    209   1.4  christos 	return strncmp(a, b, UT_LINESIZE);
    210   1.4  christos }
    211   1.4  christos 
    212   1.1       cgd /*
    213   1.4  christos  * Deal correctly with multiple virtual consoles/login ttys.
    214   1.4  christos  * We read the ttyent's from /etc/ttys and classify as login
    215   1.4  christos  * ttys ones that are running getty and they are turned on.
    216   1.1       cgd  */
    217   1.4  christos static void
    218  1.21   xtraeme find_login_ttys(void)
    219   1.4  christos {
    220   1.4  christos 	struct ttyent *tty;
    221  1.16    itojun 	char (*nCon)[UT_LINESIZE];
    222   1.4  christos 
    223   1.4  christos 	if ((Con = malloc((Maxcon = 10) * sizeof(Con[0]))) == NULL)
    224   1.4  christos 		err(1, "malloc");
    225   1.4  christos 
    226   1.4  christos 	setttyent();
    227   1.4  christos 	while ((tty = getttyent()) != NULL)
    228   1.4  christos 		if ((tty->ty_status & TTY_ON) != 0 &&
    229   1.4  christos 		    strstr(tty->ty_getty, "getty") != NULL) {
    230  1.16    itojun 			if (Ncon == Maxcon) {
    231  1.16    itojun 				if ((nCon = realloc(Con, (Maxcon + 10) *
    232   1.4  christos 				    sizeof(Con[0]))) == NULL)
    233   1.4  christos 					err(1, "malloc");
    234  1.16    itojun 				Con = nCon;
    235  1.16    itojun 				Maxcon += 10;
    236  1.16    itojun 			}
    237   1.4  christos 			(void)strncpy(Con[Ncon++], tty->ty_name, UT_LINESIZE);
    238   1.4  christos 		}
    239   1.4  christos 	endttyent();
    240   1.4  christos 	qsort(Con, Ncon, sizeof(Con[0]), compare);
    241   1.4  christos }
    242   1.4  christos 
    243   1.4  christos #ifdef notdef
    244   1.4  christos /*
    245   1.4  christos  * is someone logged in on Console/login tty?
    246   1.4  christos  */
    247   1.4  christos static int
    248  1.21   xtraeme on_console(struct utmp_list *head)
    249   1.1       cgd {
    250   1.1       cgd 	struct utmp_list *up;
    251   1.1       cgd 
    252   1.4  christos 	for (up = head; up; up = up->next)
    253   1.4  christos 		if (is_login_tty(up->usr.ut_line))
    254   1.1       cgd 			return 1;
    255   1.4  christos 
    256   1.1       cgd 	return 0;
    257   1.1       cgd }
    258   1.1       cgd #endif
    259   1.1       cgd 
    260   1.1       cgd /*
    261   1.1       cgd  * update user's login time
    262   1.1       cgd  */
    263   1.4  christos static struct user_list *
    264  1.21   xtraeme update_user(struct user_list *head, char *name, time_t secs)
    265   1.1       cgd {
    266   1.1       cgd 	struct user_list *up;
    267   1.1       cgd 
    268   1.1       cgd 	for (up = head; up != NULL; up = up->next) {
    269   1.5   hubertf 		if (strncmp(up->name, name, sizeof (up->name) - 1) == 0) {
    270   1.1       cgd 			up->secs += secs;
    271   1.1       cgd 			Total += secs;
    272   1.1       cgd 			return head;
    273   1.1       cgd 		}
    274   1.1       cgd 	}
    275   1.1       cgd 	/*
    276   1.1       cgd 	 * not found so add new user unless specified users only
    277   1.1       cgd 	 */
    278   1.1       cgd 	if (Flags & AC_U)
    279   1.1       cgd 		return head;
    280   1.1       cgd 
    281   1.1       cgd 	if ((up = NEW(struct user_list)) == NULL)
    282   1.1       cgd 		err(1, "malloc");
    283   1.1       cgd 	up->next = head;
    284  1.15    itojun 	(void)strlcpy(up->name, name, sizeof (up->name));
    285   1.1       cgd 	up->secs = secs;
    286   1.1       cgd 	Total += secs;
    287   1.1       cgd 	return up;
    288   1.1       cgd }
    289   1.1       cgd 
    290   1.1       cgd int
    291  1.21   xtraeme main(int argc, char **argv)
    292   1.1       cgd {
    293   1.1       cgd 	FILE *fp;
    294   1.1       cgd 	int c;
    295   1.1       cgd 
    296   1.1       cgd 	fp = NULL;
    297  1.19       wiz 	while ((c = getopt(argc, argv, "Ddpt:w:")) != -1) {
    298   1.1       cgd 		switch (c) {
    299   1.1       cgd #ifdef DEBUG
    300   1.1       cgd 		case 'D':
    301   1.1       cgd 			Debug++;
    302   1.1       cgd 			break;
    303   1.1       cgd #endif
    304   1.1       cgd 		case 'd':
    305   1.1       cgd 			Flags |= AC_D;
    306   1.1       cgd 			break;
    307   1.1       cgd 		case 'p':
    308   1.1       cgd 			Flags |= AC_P;
    309   1.1       cgd 			break;
    310   1.1       cgd 		case 't':			/* only do specified ttys */
    311   1.1       cgd 			add_tty(optarg);
    312   1.1       cgd 			break;
    313   1.1       cgd 		case 'w':
    314   1.1       cgd 			fp = file(optarg);
    315   1.1       cgd 			break;
    316   1.1       cgd 		case '?':
    317   1.1       cgd 		default:
    318   1.1       cgd 			usage();
    319   1.1       cgd 		}
    320   1.1       cgd 	}
    321   1.4  christos 
    322   1.4  christos 	find_login_ttys();
    323   1.4  christos 
    324   1.1       cgd 	if (optind < argc) {
    325   1.1       cgd 		/*
    326   1.1       cgd 		 * initialize user list
    327   1.1       cgd 		 */
    328   1.1       cgd 		for (; optind < argc; optind++) {
    329   1.1       cgd 			Users = update_user(Users, argv[optind], 0L);
    330   1.1       cgd 		}
    331   1.1       cgd 		Flags |= AC_U;			/* freeze user list */
    332   1.1       cgd 	}
    333   1.1       cgd 	if (Flags & AC_D)
    334   1.1       cgd 		Flags &= ~AC_P;
    335   1.1       cgd 	if (fp == NULL) {
    336   1.1       cgd 		/*
    337   1.1       cgd 		 * if _PATH_WTMP does not exist, exit quietly
    338   1.1       cgd 		 */
    339   1.1       cgd 		if (access(_PATH_WTMP, 0) != 0 && errno == ENOENT)
    340   1.1       cgd 			return 0;
    341   1.1       cgd 
    342   1.1       cgd 		fp = file(_PATH_WTMP);
    343   1.1       cgd 	}
    344   1.1       cgd 	ac(fp);
    345   1.1       cgd 
    346   1.1       cgd 	return 0;
    347   1.1       cgd }
    348   1.1       cgd 
    349   1.1       cgd /*
    350   1.1       cgd  * print login time in decimal hours
    351   1.1       cgd  */
    352   1.4  christos static void
    353  1.21   xtraeme show(const char *name, time_t secs)
    354   1.1       cgd {
    355   1.1       cgd 	(void)printf("\t%-*s %8.2f\n", UT_NAMESIZE, name,
    356   1.1       cgd 	    ((double)secs / 3600));
    357   1.1       cgd }
    358   1.1       cgd 
    359   1.4  christos static void
    360  1.21   xtraeme show_users(struct user_list *list)
    361   1.1       cgd {
    362   1.1       cgd 	struct user_list *lp;
    363   1.1       cgd 
    364   1.1       cgd 	for (lp = list; lp; lp = lp->next)
    365   1.1       cgd 		show(lp->name, lp->secs);
    366   1.1       cgd }
    367   1.1       cgd 
    368   1.1       cgd /*
    369   1.1       cgd  * print total login time for 24hr period in decimal hours
    370   1.1       cgd  */
    371   1.4  christos static void
    372  1.21   xtraeme show_today(struct user_list *users, struct utmp_list *logins, time_t secs)
    373   1.1       cgd {
    374   1.1       cgd 	struct user_list *up;
    375   1.1       cgd 	struct utmp_list *lp;
    376   1.1       cgd 	char date[64];
    377   1.1       cgd 	time_t yesterday = secs - 1;
    378   1.1       cgd 
    379   1.1       cgd 	(void)strftime(date, sizeof (date), "%b %e  total",
    380   1.1       cgd 	    localtime(&yesterday));
    381   1.1       cgd 
    382   1.1       cgd 	/* restore the missing second */
    383   1.1       cgd 	yesterday++;
    384   1.1       cgd 
    385   1.1       cgd 	for (lp = logins; lp != NULL; lp = lp->next) {
    386   1.1       cgd 		secs = yesterday - lp->usr.ut_time;
    387   1.1       cgd 		Users = update_user(Users, lp->usr.ut_name, secs);
    388   1.1       cgd 		lp->usr.ut_time = yesterday;	/* as if they just logged in */
    389   1.1       cgd 	}
    390   1.1       cgd 	secs = 0;
    391   1.1       cgd 	for (up = users; up != NULL; up = up->next) {
    392   1.1       cgd 		secs += up->secs;
    393   1.1       cgd 		up->secs = 0;			/* for next day */
    394   1.1       cgd 	}
    395   1.1       cgd  	if (secs)
    396   1.1       cgd 		(void)printf("%s %11.2f\n", date, ((double)secs / 3600));
    397   1.1       cgd }
    398   1.1       cgd 
    399   1.1       cgd /*
    400   1.1       cgd  * log a user out and update their times.
    401   1.1       cgd  * if ut_line is "~", we log all users out as the system has
    402   1.1       cgd  * been shut down.
    403   1.1       cgd  */
    404   1.4  christos static struct utmp_list *
    405  1.21   xtraeme log_out(struct utmp_list *head, struct utmp *up)
    406   1.1       cgd {
    407   1.1       cgd 	struct utmp_list *lp, *lp2, *tlp;
    408   1.1       cgd 	time_t secs;
    409   1.1       cgd 
    410   1.1       cgd 	for (lp = head, lp2 = NULL; lp != NULL; )
    411   1.3       cgd 		if (*up->ut_line == '~' || strncmp(lp->usr.ut_line, up->ut_line,
    412   1.3       cgd 		    sizeof (up->ut_line)) == 0) {
    413   1.1       cgd 			secs = up->ut_time - lp->usr.ut_time;
    414   1.1       cgd 			Users = update_user(Users, lp->usr.ut_name, secs);
    415   1.1       cgd #ifdef DEBUG
    416   1.1       cgd 			if (Debug)
    417   1.3       cgd 				printf("%-.*s %-.*s: %-.*s logged out (%2d:%02d:%02d)\n",
    418   1.3       cgd 				    19, ctime(&up->ut_time),
    419   1.3       cgd 				    sizeof (lp->usr.ut_line), lp->usr.ut_line,
    420   1.3       cgd 				    sizeof (lp->usr.ut_name), lp->usr.ut_name,
    421   1.3       cgd 				    secs / 3600, (secs % 3600) / 60, secs % 60);
    422   1.1       cgd #endif
    423   1.1       cgd 			/*
    424   1.1       cgd 			 * now lose it
    425   1.1       cgd 			 */
    426   1.1       cgd 			tlp = lp;
    427   1.1       cgd 			lp = lp->next;
    428  1.22  christos 			if (tlp == head) {
    429   1.1       cgd 				head = lp;
    430  1.23   jnemeth 			} else if (lp2 != NULL)
    431   1.1       cgd 				lp2->next = lp;
    432   1.1       cgd 			free(tlp);
    433   1.1       cgd 		} else {
    434   1.1       cgd 			lp2 = lp;
    435   1.1       cgd 			lp = lp->next;
    436   1.1       cgd 		}
    437   1.1       cgd 	return head;
    438   1.1       cgd }
    439   1.1       cgd 
    440   1.1       cgd 
    441   1.1       cgd /*
    442   1.1       cgd  * if do_tty says ok, login a user
    443   1.1       cgd  */
    444   1.1       cgd struct utmp_list *
    445  1.21   xtraeme log_in(struct utmp_list *head, struct utmp *up)
    446   1.1       cgd {
    447   1.1       cgd 	struct utmp_list *lp;
    448   1.1       cgd 
    449   1.1       cgd 	/*
    450   1.1       cgd 	 * If we are doing specified ttys only, we ignore
    451   1.1       cgd 	 * anything else.
    452   1.1       cgd 	 */
    453   1.1       cgd 	if (Flags & AC_T)
    454   1.1       cgd 		if (!do_tty(up->ut_line))
    455   1.1       cgd 			return head;
    456   1.1       cgd 
    457   1.1       cgd 	/*
    458   1.1       cgd 	 * go ahead and log them in
    459   1.1       cgd 	 */
    460   1.1       cgd 	if ((lp = NEW(struct utmp_list)) == NULL)
    461   1.1       cgd 		err(1, "malloc");
    462   1.1       cgd 	lp->next = head;
    463   1.1       cgd 	head = lp;
    464   1.1       cgd 	memmove((char *)&lp->usr, (char *)up, sizeof (struct utmp));
    465   1.1       cgd #ifdef DEBUG
    466   1.1       cgd 	if (Debug) {
    467   1.3       cgd 		printf("%-.*s %-.*s: %-.*s logged in", 19,
    468   1.3       cgd 		    ctime(&lp->usr.ut_time), sizeof (up->ut_line),
    469   1.3       cgd 		       up->ut_line, sizeof (up->ut_name), up->ut_name);
    470   1.1       cgd 		if (*up->ut_host)
    471   1.3       cgd 			printf(" (%-.*s)", sizeof (up->ut_host), up->ut_host);
    472   1.1       cgd 		putchar('\n');
    473   1.1       cgd 	}
    474   1.1       cgd #endif
    475   1.1       cgd 	return head;
    476   1.1       cgd }
    477   1.1       cgd 
    478   1.4  christos static int
    479  1.21   xtraeme ac(FILE *fp)
    480   1.1       cgd {
    481   1.1       cgd 	struct utmp_list *lp, *head = NULL;
    482   1.1       cgd 	struct utmp usr;
    483   1.1       cgd 	struct tm *ltm;
    484   1.4  christos 	time_t secs = 0;
    485   1.1       cgd 	int day = -1;
    486   1.1       cgd 
    487   1.1       cgd 	while (fread((char *)&usr, sizeof(usr), 1, fp) == 1) {
    488   1.1       cgd 		if (!FirstTime)
    489   1.1       cgd 			FirstTime = usr.ut_time;
    490   1.1       cgd 		if (Flags & AC_D) {
    491   1.1       cgd 			ltm = localtime(&usr.ut_time);
    492   1.1       cgd 			if (day >= 0 && day != ltm->tm_yday) {
    493   1.1       cgd 				day = ltm->tm_yday;
    494   1.1       cgd 				/*
    495   1.1       cgd 				 * print yesterday's total
    496   1.1       cgd 				 */
    497   1.1       cgd 				secs = usr.ut_time;
    498   1.1       cgd 				secs -= ltm->tm_sec;
    499   1.1       cgd 				secs -= 60 * ltm->tm_min;
    500   1.1       cgd 				secs -= 3600 * ltm->tm_hour;
    501   1.1       cgd 				show_today(Users, head, secs);
    502   1.1       cgd 			} else
    503   1.1       cgd 				day = ltm->tm_yday;
    504   1.1       cgd 		}
    505   1.1       cgd 		switch(*usr.ut_line) {
    506   1.1       cgd 		case '|':
    507   1.1       cgd 			secs = usr.ut_time;
    508   1.1       cgd 			break;
    509   1.1       cgd 		case '{':
    510   1.1       cgd 			secs -= usr.ut_time;
    511   1.1       cgd 			/*
    512   1.1       cgd 			 * adjust time for those logged in
    513   1.1       cgd 			 */
    514   1.1       cgd 			for (lp = head; lp != NULL; lp = lp->next)
    515   1.1       cgd 				lp->usr.ut_time -= secs;
    516   1.1       cgd 			break;
    517   1.1       cgd 		case '~':			/* reboot or shutdown */
    518   1.1       cgd 			head = log_out(head, &usr);
    519   1.3       cgd 			FirstTime = usr.ut_time; /* shouldn't be needed */
    520   1.1       cgd 			break;
    521   1.1       cgd 		default:
    522   1.1       cgd 			/*
    523   1.1       cgd 			 * if they came in on tty[p-y]*, then it is only
    524   1.4  christos 			 * a login session if the ut_host field is non-empty,
    525   1.4  christos 			 * or this tty is a login tty [eg. a console]
    526   1.1       cgd 			 */
    527   1.1       cgd 			if (*usr.ut_name) {
    528   1.9       mrg 				if ((strncmp(usr.ut_line, "tty", 3) != 0 &&
    529   1.9       mrg 				    strncmp(usr.ut_line, "dty", 3) != 0) ||
    530   1.4  christos 				    strchr("pqrstuvwxyzPQRST", usr.ut_line[3]) == 0 ||
    531   1.4  christos 				    *usr.ut_host != '\0' ||
    532   1.4  christos 				    is_login_tty(usr.ut_line))
    533   1.1       cgd 					head = log_in(head, &usr);
    534   1.4  christos #ifdef DEBUG
    535   1.4  christos 				else if (Debug)
    536   1.4  christos 					printf("%-.*s %-.*s: %-.*s ignored\n",
    537   1.4  christos 					    19, ctime(&usr.ut_time),
    538   1.4  christos 					    sizeof (usr.ut_line), usr.ut_line,
    539   1.4  christos 					    sizeof (usr.ut_name), usr.ut_name);
    540   1.4  christos #endif
    541   1.1       cgd 			} else
    542   1.1       cgd 				head = log_out(head, &usr);
    543   1.1       cgd 			break;
    544   1.1       cgd 		}
    545   1.1       cgd 	}
    546   1.1       cgd 	(void)fclose(fp);
    547   1.1       cgd 	usr.ut_time = time((time_t *)0);
    548   1.1       cgd 	(void)strcpy(usr.ut_line, "~");
    549   1.1       cgd 
    550   1.1       cgd 	if (Flags & AC_D) {
    551   1.1       cgd 		ltm = localtime(&usr.ut_time);
    552   1.1       cgd 		if (day >= 0 && day != ltm->tm_yday) {
    553   1.1       cgd 			/*
    554   1.1       cgd 			 * print yesterday's total
    555   1.1       cgd 			 */
    556   1.1       cgd 			secs = usr.ut_time;
    557   1.1       cgd 			secs -= ltm->tm_sec;
    558   1.1       cgd 			secs -= 60 * ltm->tm_min;
    559   1.1       cgd 			secs -= 3600 * ltm->tm_hour;
    560   1.1       cgd 			show_today(Users, head, secs);
    561   1.1       cgd 		}
    562   1.1       cgd 	}
    563   1.1       cgd 	/*
    564   1.1       cgd 	 * anyone still logged in gets time up to now
    565   1.1       cgd 	 */
    566   1.1       cgd 	head = log_out(head, &usr);
    567   1.1       cgd 
    568   1.1       cgd 	if (Flags & AC_D)
    569   1.1       cgd 		show_today(Users, head, time((time_t *)0));
    570   1.1       cgd 	else {
    571   1.1       cgd 		if (Flags & AC_P)
    572   1.1       cgd 			show_users(Users);
    573   1.1       cgd 		show("total", Total);
    574   1.1       cgd 	}
    575   1.1       cgd 	return 0;
    576   1.1       cgd }
    577   1.1       cgd 
    578   1.4  christos static void
    579  1.21   xtraeme usage(void)
    580   1.1       cgd {
    581   1.4  christos 
    582   1.1       cgd 	(void)fprintf(stderr,
    583  1.18      jmmv 	    "usage: %s [-d | -p] [-t tty] [-w wtmp] [users ...]\n",
    584  1.14       cgd 	    getprogname());
    585   1.1       cgd 	exit(1);
    586   1.1       cgd }
    587