1: /*
2: * acct [ -w wtmp ] [ -d ] [ -p ] [ people ]
3: */
4: static char *sccsid = "@(#)ac.c 4.3 (Berkeley) 7/2/81";
5:
6: #include <stdio.h>
7: #include <ctype.h>
8: #include <time.h>
9: #include <utmp.h>
10: #include <sys/types.h>
11: #include <sys/timeb.h>
12:
13: #define NMAX sizeof(ibuf.ut_name)
14: #define LMAX sizeof(ibuf.ut_line)
15:
16: #define TSIZE 128 /* maximum number of ttys */
17: #define USIZE 500
18: struct utmp ibuf;
19:
20: struct ubuf {
21: char uname[NMAX];
22: time_t utime;
23: } ubuf[USIZE];
24:
25: struct tbuf {
26: struct ubuf *userp;
27: char ttnames[LMAX];
28: time_t ttime;
29: } tbuf[TSIZE];
30:
31: int umax, tmax;
32: char *wtmp;
33: int pflag, byday;
34: time_t dtime;
35: time_t midnight;
36: time_t lastime;
37: time_t day = 86400L;
38: int pcount;
39: char **pptr;
40:
41: main(argc, argv)
42: char **argv;
43: {
44: int c, fl;
45: register i;
46: FILE *wf;
47:
48: wtmp = "/usr/adm/wtmp";
49: while (--argc > 0 && **++argv == '-')
50: switch(*++*argv) {
51: case 'd':
52: byday++;
53: continue;
54:
55: case 'w':
56: if (--argc>0)
57: wtmp = *++argv;
58: continue;
59:
60: case 'p':
61: pflag++;
62: continue;
63: }
64: pcount = argc;
65: pptr = argv;
66: if ((wf = fopen(wtmp, "r")) == NULL) {
67: printf("No %s\n", wtmp);
68: exit(1);
69: }
70: for(;;) {
71: if (fread((char *)&ibuf, sizeof(ibuf), 1, wf) != 1)
72: break;
73: fl = 0;
74: for (i=0; i<NMAX; i++) {
75: c = ibuf.ut_name[i];
76: if (isprint(c) && c != ' ') {
77: if (fl)
78: goto skip;
79: continue;
80: }
81: if (c==' ' || c=='\0') {
82: fl++;
83: ibuf.ut_name[i] = '\0';
84: } else
85: goto skip;
86: }
87: loop();
88: skip:;
89: }
90: ibuf.ut_name[0] = '\0';
91: ibuf.ut_line[0] = '~';
92: time(&ibuf.ut_time);
93: loop();
94: print();
95: exit(0);
96: }
97:
98: loop()
99: {
100: register int i;
101: register struct tbuf *tp;
102: register struct ubuf *up;
103: static int complained = 0;
104:
105: if(ibuf.ut_line[0] == '|') {
106: dtime = ibuf.ut_time;
107: return;
108: }
109: if(ibuf.ut_line[0] == '{') {
110: if(dtime == 0)
111: return;
112: for(tp = tbuf; tp <= tmax; tp++)
113: tp->ttime += ibuf.ut_time-dtime;
114: dtime = 0;
115: return;
116: }
117: if (lastime>ibuf.ut_time || lastime+(1.5*day)<ibuf.ut_time)
118: midnight = 0;
119: if (midnight==0)
120: newday();
121: lastime = ibuf.ut_time;
122: if (byday && ibuf.ut_time > midnight) {
123: upall(1);
124: print();
125: newday();
126: for (up=ubuf; up <= &ubuf[umax]; up++)
127: up->utime = 0;
128: }
129: if (ibuf.ut_line[0] == '~') {
130: ibuf.ut_name[0] = '\0';
131: upall(0);
132: return;
133: }
134: for (i = 0; i < TSIZE; i++) {
135: if (tbuf[i].ttnames[0] == 0) {
136: strncpy(tbuf[i].ttnames, ibuf.ut_line,
137: sizeof(ibuf.ut_line));
138: tmax = i;
139: break;
140: }
141: if (!strncmp(tbuf[i].ttnames, ibuf.ut_line,
142: sizeof(ibuf.ut_line)))
143: break;
144: }
145: if (i == TSIZE) {
146: if (!complained)
147: fprintf(stderr, "Too many ttys, some ignored\n");
148: complained = 1;
149: return;
150: }
151: tp = &tbuf[i];
152: update(tp, 0);
153: }
154:
155: print()
156: {
157: register int i;
158: time_t ttime, t;
159:
160: ttime = 0;
161: for (i=0; i <= umax; i++) {
162: if(!among(i))
163: continue;
164: t = ubuf[i].utime;
165: if (t>0)
166: ttime += t;
167: if (pflag && ubuf[i].utime > 0) {
168: printf("\t%-*.*s%6.2f\n", NMAX, NMAX,
169: ubuf[i].uname, ubuf[i].utime/3600.);
170: }
171: }
172: if (ttime > 0) {
173: pdate();
174: printf("\ttotal%9.2f\n", ttime/3600.);
175: }
176: }
177:
178: upall(f)
179: {
180: register struct tbuf *tp;
181:
182: for (tp=tbuf; tp <= tmax; tp++)
183: update(tp, f);
184: }
185:
186: update(tp, f)
187: struct tbuf *tp;
188: {
189: register int j;
190: register struct ubuf *up;
191: time_t t, t1;
192: static int complained;
193:
194: if (f)
195: t = midnight;
196: else
197: t = ibuf.ut_time;
198: if (tp->userp) {
199: t1 = t - tp->ttime;
200: if (t1 > 0)
201: tp->userp->utime += t1;
202: }
203: tp->ttime = t;
204: if (f)
205: return;
206: if (ibuf.ut_name[0]=='\0') {
207: tp->userp = 0;
208: return;
209: }
210: for (up=ubuf; up < &ubuf[USIZE]; up++) {
211: if (up->uname[0] == '\0') {
212: umax = up - ubuf;
213: break;
214: }
215: for (j=0; j<NMAX && up->uname[j]==ibuf.ut_name[j]; j++);
216: if (j>=NMAX)
217: break;
218: }
219: if (up == &ubuf[USIZE]) {
220: if (!complained)
221: fprintf(stderr, "Too many users, some omitted\n");
222: complained = 1;
223: return;
224: }
225: for (j=0; j<NMAX; j++)
226: up->uname[j] = ibuf.ut_name[j];
227: tp->userp = up;
228: }
229:
230: among(i)
231: {
232: register j, k;
233: register char *p;
234:
235: if (pcount==0)
236: return(1);
237: for (j=0; j<pcount; j++) {
238: p = pptr[j];
239: for (k=0; k<NMAX; k++) {
240: if (*p == ubuf[i].uname[k]) {
241: if (*p++ == '\0' || k == NMAX-1)
242: return(1);
243: } else
244: break;
245: }
246: }
247: return(0);
248: }
249:
250: newday()
251: {
252: time_t ttime;
253: struct timeb tb;
254: struct tm *localtime();
255:
256: time(&ttime);
257: if (midnight == 0) {
258: ftime(&tb);
259: midnight = 60*(time_t)tb.timezone;
260: if (localtime(&ttime)->tm_isdst)
261: midnight -= 3600;
262: }
263: while (midnight <= ibuf.ut_time)
264: midnight += day;
265: }
266:
267: pdate()
268: {
269: time_t x;
270: char *ctime();
271:
272: if (byday==0)
273: return;
274: x = midnight-1;
275: printf("%.6s", ctime(&x)+4);
276: }
Defined functions
loop
defined in line
98; used 2 times
main
defined in line
41;
never used
Defined variables
byday
defined in line
33; used 3 times
day
defined in line
37; used 2 times
dtime
defined in line
34; used 4 times
ibuf
defined in line
18; used 28 times
- in line 13-14(2),
71-75(3),
83,
90-92(3),
105-122(8),
129-130(2),
136-142(4),
197,
206,
215,
226,
263
pflag
defined in line
33; used 2 times
pptr
defined in line
39; used 2 times
sccsid
defined in line
4;
never used
tbuf
defined in line
29; used 6 times
tmax
defined in line
31; used 3 times
ubuf
defined in line
23; used 11 times
umax
defined in line
31; used 3 times
wtmp
defined in line
32; used 4 times
Defined struct's
tbuf
defined in line
25; used 6 times
ubuf
defined in line
20; used 6 times
Defined macros
LMAX
defined in line
14; used 1 times
NMAX
defined in line
13; used 9 times
TSIZE
defined in line
16; used 3 times
USIZE
defined in line
17; used 3 times