📄 ac.c
字号:
#ifndef lintstatic char *sccsid = "@(#)ac.c 4.2 (ULTRIX) 2/21/91";#endif/************************************************************************ * * * Copyright (c) 1991 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//* * Based on: * static char *sccsid = "@(#)ac.c 4.7 (Berkeley) 7/2/83"; *//* * ac [ -w wtmp ] [ -d ] [ -p ] [ people ] */#include <stdio.h>#include <ctype.h>#include <utmp.h>#include <sys/time.h>#include <sys/types.h>#include <sys/timeb.h>#define NMAX sizeof(ibuf.ut_name)#define LMAX sizeof(ibuf.ut_line)/*#define TSIZE 1000*/#define TSIZE 6242struct utmp ibuf;struct ubuf { char uname[NMAX]; long utime; struct ubuf *u_next;} *ubuf;struct tbuf { struct ubuf *userp; long ttime;} tbuf[TSIZE];char *wtmp;int pflag, byday;long dtime;long midnight;long lastime;long day = 86400L;int pcount;char **pptr;main(argc, argv) char **argv;{ int c, fl; register i; FILE *wf; wtmp = "/usr/adm/wtmp"; while (--argc > 0 && **++argv == '-') switch(*++*argv) { case 'd': byday++; continue; case 'w': if (--argc>0) wtmp = *++argv; else usage(); continue; case 'p': pflag++; continue; } if(argc >= 0) pcount = argc; else pcount = 0; pptr = argv; if ((wf = fopen(wtmp, "r")) == NULL) { printf("No %s\n", wtmp); exit(1); } for(;;) { if (fread((char *)&ibuf, sizeof(ibuf), 1, wf) != 1) break; fl = 0; for (i=0; i<NMAX; i++) { c = ibuf.ut_name[i]; if (isprint(c) && c != ' ') { if (fl) goto skip; continue; } if (c==' ' || c=='\0') { fl++; ibuf.ut_name[i] = '\0'; } else goto skip; } loop(); skip:; } ibuf.ut_name[0] = '\0'; ibuf.ut_line[0] = '~'; time(&ibuf.ut_time); loop(); print(); exit(0);}loop(){ register i; register struct tbuf *tp; register struct ubuf *up; if(ibuf.ut_line[0] == '|') { dtime = ibuf.ut_time; return; } if(ibuf.ut_line[0] == '{') { if(dtime == 0) return; for(tp = tbuf; tp < &tbuf[TSIZE]; tp++) tp->ttime += ibuf.ut_time-dtime; dtime = 0; return; } if (lastime>ibuf.ut_time || lastime+(1.5*day)<ibuf.ut_time) midnight = 0; if (midnight==0) newday(); lastime = ibuf.ut_time; if (byday && ibuf.ut_time > midnight) { upall(1); print(); newday(); for (up=ubuf; up; up = up->u_next) up->utime = 0; } if (ibuf.ut_line[0] == '~') { ibuf.ut_name[0] = '\0'; upall(0); return; } /* if (ibuf.ut_line[0]=='t') i = (ibuf.ut_line[3]-'0')*10 + (ibuf.ut_line[4]-'0'); else i = TSIZE-1; if (i<0 || i>=TSIZE) i = TSIZE-1; */ /* * Correction contributed by Phyllis Kantar @ Rand-unix * * Fixes long standing problem with tty names other than 00-99 */ if (ibuf.ut_line[0]=='t') { i = (ibuf.ut_line[3]-'0'); if(ibuf.ut_line[4]) i = i*79 + (ibuf.ut_line[4]-'0'); } else i = TSIZE-1; if (i<0 || i>=TSIZE) { i = TSIZE-1; printf("ac: Bad tty name: %s\n", ibuf.ut_line); } tp = &tbuf[i]; update(tp, 0);}print(){ int i; long ttime, t; struct ubuf *u; ttime = 0; for(u=ubuf; u; u = u->u_next) { if(!among(u)) continue; t = u->utime; if (t>0) ttime += t; if (pflag && u->utime > 0) { printf("\t%-*.*s%6.2f\n", NMAX, NMAX, u->uname, u->utime/3600.); } } if (ttime > 0) { pdate(); printf("\ttotal%9.2f\n", ttime/3600.); }}upall(f){ register struct tbuf *tp; for (tp=tbuf; tp < &tbuf[TSIZE]; tp++) update(tp, f);}static struct ubuf *newubuf(){ struct ubuf *up, *malloc(); up = malloc(sizeof (struct ubuf)); if(up) { up->u_next = ubuf; ubuf = up; } return up;}update(tp, f)struct tbuf *tp;{ int j; struct ubuf *up; long t, t1; if (f) t = midnight; else t = ibuf.ut_time; if (tp->userp) { t1 = t - tp->ttime; if (t1 > 0) tp->userp->utime += t1; } tp->ttime = t; if (f) return; if (ibuf.ut_name[0]=='\0') { tp->userp = 0; return; } for (up=ubuf; up; up = up->u_next) { if (up->uname[0] == '\0') break; if(!strncmp(up->uname, ibuf.ut_name, NMAX)) break; } if(!up) up = newubuf(); if(up) for (j=0; j<NMAX; j++) up->uname[j] = ibuf.ut_name[j]; tp->userp = up;}among(u)struct ubuf *u;{ register j, k; register char *p; if (pcount==0) return(1); for (j=0; j<pcount; j++) { p = pptr[j]; for (k=0; k<NMAX; k++) { if (*p == u->uname[k]) { if (*p++ == '\0' || k == NMAX-1) return(1); } else break; } } return(0);}newday(){ long ttime; struct timeb tb; struct tm *localtime(); time(&ttime); if (midnight == 0) { ftime(&tb); midnight = 60*(long)tb.timezone; if (localtime(&ttime)->tm_isdst) midnight -= 3600; } while (midnight <= ibuf.ut_time) midnight += day;}pdate(){ long x; char *ctime(); if (byday==0) return; x = midnight-1; printf("%.6s", ctime(&x)+4);}usage(){ printf("usage: ac [-w wtmp] [-p] [-d] [people] ...\n"); exit(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -