]> git.saurik.com Git - apple/system_cmds.git/blob - ac.tproj/ac.c
system_cmds-880.100.5.tar.gz
[apple/system_cmds.git] / ac.tproj / ac.c
1 /*
2 * Copyright (c) 1999-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1994 Christopher G. Demetriou.
26 * @(#)Copyright (c) 1994, Simon J. Gerraty.
27 *
28 * This is free software. It comes with NO WARRANTY.
29 * Permission to use, modify and distribute this source code
30 * is granted subject to the following conditions.
31 * 1/ that the above copyright notice and this notice
32 * are preserved in all copies and that due credit be given
33 * to the author.
34 * 2/ that any changes to this code are clearly commented
35 * as such so that the author does not get blamed for bugs
36 * other than his own.
37 */
38
39 #ifndef lint
40 #include <sys/cdefs.h>
41 __unused static char rcsid[] = "$Id: ac.c,v 1.2 2006/02/07 05:51:22 lindak Exp $";
42 #endif
43
44 #include <sys/types.h>
45 #include <sys/file.h>
46 #include <sys/time.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <utmpx.h>
54 #include <unistd.h>
55
56 #define UT_NAMESIZE 8 /* from utmp.h; only for formatting */
57
58 /*
59 * this is for our list of currently logged in sessions
60 */
61 struct utmp_list {
62 struct utmp_list *next;
63 struct utmpx usr;
64 };
65
66 /*
67 * this is for our list of users that are accumulating time.
68 */
69 struct user_list {
70 struct user_list *next;
71 char name[_UTX_USERSIZE+1];
72 time_t secs;
73 };
74
75 /*
76 * this is for chosing whether to ignore a login
77 */
78 struct tty_list {
79 struct tty_list *next;
80 char name[_UTX_USERSIZE+3];
81 int len;
82 int ret;
83 };
84
85 /*
86 * globals - yes yuk
87 */
88 #ifdef CONSOLE_TTY
89 static char *Console = CONSOLE_TTY;
90 #endif
91 static time_t Total = 0;
92 static time_t FirstTime = 0;
93 static int Flags = 0;
94 static struct user_list *Users = NULL;
95 static struct tty_list *Ttys = NULL;
96
97 #define NEW(type) (type *)malloc(sizeof (type))
98
99 #define AC_W 1 /* not _PATH_WTMP */
100 #define AC_D 2 /* daily totals (ignore -p) */
101 #define AC_P 4 /* per-user totals */
102 #define AC_U 8 /* specified users only */
103 #define AC_T 16 /* specified ttys only */
104
105 #ifdef DEBUG
106 static int Debug = 0;
107 #endif
108
109 int main __P((int, char **));
110 int ac __P((void));
111 struct tty_list *add_tty __P((char *));
112 int do_tty __P((char *));
113 struct utmp_list *log_in __P((struct utmp_list *, struct utmpx *));
114 struct utmp_list *log_out __P((struct utmp_list *, struct utmpx *));
115 int on_console __P((struct utmp_list *));
116 void show __P((char *, time_t));
117 void show_today __P((struct user_list *, struct utmp_list *,
118 time_t));
119 void show_users __P((struct user_list *));
120 struct user_list *update_user __P((struct user_list *, char *, time_t));
121 void usage __P((void));
122
123 struct tty_list *
124 add_tty(char *name)
125 {
126 struct tty_list *tp;
127 register char *rcp;
128
129 Flags |= AC_T;
130
131 if ((tp = NEW(struct tty_list)) == NULL)
132 err(1, "malloc");
133 tp->len = 0; /* full match */
134 tp->ret = 1; /* do if match */
135 if (*name == '!') { /* don't do if match */
136 tp->ret = 0;
137 name++;
138 }
139 (void)strncpy(tp->name, name, sizeof (tp->name) - 1);
140 tp->name[sizeof (tp->name) - 1] = '\0';
141 if ((rcp = strchr(tp->name, '*')) != NULL) { /* wild card */
142 *rcp = '\0';
143 tp->len = (int)strlen(tp->name); /* match len bytes only */
144 }
145 tp->next = Ttys;
146 Ttys = tp;
147 return Ttys;
148 }
149
150 /*
151 * should we process the named tty?
152 */
153 int
154 do_tty(char *name)
155 {
156 struct tty_list *tp;
157 int def_ret = 0;
158
159 for (tp = Ttys; tp != NULL; tp = tp->next) {
160 if (tp->ret == 0) /* specific don't */
161 def_ret = 1; /* default do */
162 if (tp->len != 0) {
163 if (strncmp(name, tp->name, tp->len) == 0)
164 return tp->ret;
165 } else {
166 if (strncmp(name, tp->name, sizeof (tp->name)) == 0)
167 return tp->ret;
168 }
169 }
170 return def_ret;
171 }
172
173 #ifdef CONSOLE_TTY
174 /*
175 * is someone logged in on Console?
176 */
177 int
178 on_console(struct utmp_list *head)
179 {
180 struct utmp_list *up;
181
182 for (up = head; up; up = up->next) {
183 if (strncmp(up->usr.ut_line, Console,
184 sizeof (up->usr.ut_line)) == 0)
185 return 1;
186 }
187 return 0;
188 }
189 #endif
190
191 /*
192 * update user's login time
193 */
194 struct user_list *
195 update_user(struct user_list *head, char *name, time_t secs)
196 {
197 struct user_list *up;
198
199 for (up = head; up != NULL; up = up->next) {
200 if (strncmp(up->name, name, sizeof (up->name)) == 0) {
201 up->secs += secs;
202 Total += secs;
203 return head;
204 }
205 }
206 /*
207 * not found so add new user unless specified users only
208 */
209 if (Flags & AC_U)
210 return head;
211
212 if ((up = NEW(struct user_list)) == NULL)
213 err(1, "malloc");
214 up->next = head;
215 (void)strncpy(up->name, name, sizeof (up->name) - 1);
216 up->name[sizeof (up->name) - 1] = '\0'; /* paranoid! */
217 up->secs = secs;
218 Total += secs;
219 return up;
220 }
221
222 int
223 main(int argc, char **argv)
224 {
225 FILE *fp;
226 int c;
227
228 fp = NULL;
229 while ((c = getopt(argc, argv, "Dc:dpt:w:")) != EOF) {
230 switch (c) {
231 #ifdef DEBUG
232 case 'D':
233 Debug++;
234 break;
235 #endif
236 case 'c':
237 #ifdef CONSOLE_TTY
238 Console = optarg;
239 #else
240 usage(); /* XXX */
241 #endif
242 break;
243 case 'd':
244 Flags |= AC_D;
245 break;
246 case 'p':
247 Flags |= AC_P;
248 break;
249 case 't': /* only do specified ttys */
250 add_tty(optarg);
251 break;
252 case 'w':
253 wtmpxname(optarg);
254 break;
255 case '?':
256 default:
257 usage();
258 break;
259 }
260 }
261 if (optind < argc) {
262 /*
263 * initialize user list
264 */
265 for (; optind < argc; optind++) {
266 Users = update_user(Users, argv[optind], 0L);
267 }
268 Flags |= AC_U; /* freeze user list */
269 }
270 if (Flags & AC_D)
271 Flags &= ~AC_P;
272 ac();
273
274 return 0;
275 }
276
277 /*
278 * print login time in decimal hours
279 */
280 void
281 show(char *name, time_t secs)
282 {
283 (void)printf("\t%-*s %8.2f\n", UT_NAMESIZE, name,
284 ((double)secs / 3600));
285 }
286
287 void
288 show_users(struct user_list *list)
289 {
290 struct user_list *lp;
291
292 for (lp = list; lp; lp = lp->next)
293 show(lp->name, lp->secs);
294 }
295
296 /*
297 * print total login time for 24hr period in decimal hours
298 */
299 void
300 show_today(struct user_list *users, struct utmp_list *logins, time_t secs)
301 {
302 struct user_list *up;
303 struct utmp_list *lp;
304 char date[64];
305 time_t yesterday = secs - 1;
306
307 (void)strftime(date, sizeof (date), "%b %e total",
308 localtime(&yesterday));
309
310 /* restore the missing second */
311 yesterday++;
312
313 for (lp = logins; lp != NULL; lp = lp->next) {
314 secs = yesterday - lp->usr.ut_tv.tv_sec;
315 Users = update_user(Users, lp->usr.ut_user, secs);
316 lp->usr.ut_tv.tv_sec = yesterday; /* as if they just logged in */
317 }
318 secs = 0;
319 for (up = users; up != NULL; up = up->next) {
320 secs += up->secs;
321 up->secs = 0; /* for next day */
322 }
323 if (secs)
324 (void)printf("%s %11.2f\n", date, ((double)secs / 3600));
325 }
326
327 /*
328 * log a user out and update their times.
329 * if ut_line is "~", we log all users out as the system has
330 * been shut down.
331 */
332 struct utmp_list *
333 log_out(struct utmp_list *head, struct utmpx *up)
334 {
335 struct utmp_list *lp, *lp2, *tlp;
336 time_t secs;
337
338 for (lp = head, lp2 = NULL; lp != NULL; )
339 if (up->ut_type == BOOT_TIME || up->ut_type == SHUTDOWN_TIME || strncmp(lp->usr.ut_line, up->ut_line,
340 sizeof (up->ut_line)) == 0) {
341 secs = up->ut_tv.tv_sec - lp->usr.ut_tv.tv_sec;
342 Users = update_user(Users, lp->usr.ut_user, secs);
343 #ifdef DEBUG
344 if (Debug)
345 printf("%-.*s %-.*s: %-.*s logged out (%2d:%02d:%02d)\n",
346 19, ctime(&up->ut_tv.tv_sec),
347 (int)sizeof (lp->usr.ut_line), lp->usr.ut_line,
348 (int)sizeof (lp->usr.ut_user), lp->usr.ut_user,
349 secs / 3600, (secs % 3600) / 60, secs % 60);
350 #endif
351 /*
352 * now lose it
353 */
354 tlp = lp;
355 lp = lp->next;
356 if (tlp == head)
357 head = lp;
358 else if (lp2 != NULL)
359 lp2->next = lp;
360 free(tlp);
361 } else {
362 lp2 = lp;
363 lp = lp->next;
364 }
365 return head;
366 }
367
368
369 /*
370 * if do_tty says ok, login a user
371 */
372 struct utmp_list *
373 log_in(struct utmp_list *head, struct utmpx *up)
374 {
375 struct utmp_list *lp;
376
377 /*
378 * this could be a login. if we're not dealing with
379 * the console name, say it is.
380 *
381 * If we are, and if ut_host==":0.0" we know that it
382 * isn't a real login. _But_ if we have not yet recorded
383 * someone being logged in on Console - due to the wtmp
384 * file starting after they logged in, we'll pretend they
385 * logged in, at the start of the wtmp file.
386 */
387
388 #ifdef CONSOLE_TTY
389 if (up->ut_host[0] == ':') {
390 /*
391 * SunOS 4.0.2 does not treat ":0.0" as special but we
392 * do.
393 */
394 if (on_console(head))
395 return head;
396 /*
397 * ok, no recorded login, so they were here when wtmp
398 * started! Adjust ut_tv.tv_sec!
399 */
400 up->ut_tv.tv_sec = FirstTime;
401 /*
402 * this allows us to pick the right logout
403 */
404 (void)strncpy(up->ut_line, Console, sizeof (up->ut_line) - 1);
405 up->ut_line[sizeof (up->ut_line) - 1] = '\0'; /* paranoid! */
406 }
407 #endif
408 /*
409 * If we are doing specified ttys only, we ignore
410 * anything else.
411 */
412 if (Flags & AC_T)
413 if (!do_tty(up->ut_line))
414 return head;
415
416 /*
417 * go ahead and log them in
418 */
419 if ((lp = NEW(struct utmp_list)) == NULL)
420 err(1, "malloc");
421 lp->next = head;
422 head = lp;
423 memmove((char *)&lp->usr, (char *)up, sizeof (struct utmpx));
424 #ifdef DEBUG
425 if (Debug) {
426 printf("%-.*s %-.*s: %-.*s logged in", 19,
427 ctime(&lp->usr.ut_tv.tv_sec), (int)sizeof (up->ut_line),
428 up->ut_line, (int)sizeof (up->ut_user), up->ut_user);
429 if (*up->ut_host)
430 printf(" (%-.*s)", (int)sizeof (up->ut_host), up->ut_host);
431 putchar('\n');
432 }
433 #endif
434 return head;
435 }
436
437 int
438 ac(void)
439 {
440 struct utmp_list *lp, *head = NULL;
441 struct utmpx *u, end;
442 struct tm *ltm;
443 time_t secs = 0;
444 int day = -1;
445
446 setutxent_wtmp(1); /* read in forward direction */
447 while ((u = getutxent_wtmp()) != NULL) {
448 if (!FirstTime)
449 FirstTime = u->ut_tv.tv_sec;
450 if (Flags & AC_D) {
451 ltm = localtime(&u->ut_tv.tv_sec);
452 if (day >= 0 && day != ltm->tm_yday) {
453 day = ltm->tm_yday;
454 /*
455 * print yesterday's total
456 */
457 secs = u->ut_tv.tv_sec;
458 secs -= ltm->tm_sec;
459 secs -= 60 * ltm->tm_min;
460 secs -= 3600 * ltm->tm_hour;
461 show_today(Users, head, secs);
462 } else
463 day = ltm->tm_yday;
464 }
465 switch(u->ut_type) {
466 case OLD_TIME:
467 secs = u->ut_tv.tv_sec;
468 break;
469 case NEW_TIME:
470 secs -= u->ut_tv.tv_sec;
471 /*
472 * adjust time for those logged in
473 */
474 for (lp = head; lp != NULL; lp = lp->next)
475 lp->usr.ut_tv.tv_sec -= secs;
476 break;
477 case BOOT_TIME: /* reboot or shutdown */
478 case SHUTDOWN_TIME:
479 head = log_out(head, u);
480 FirstTime = u->ut_tv.tv_sec; /* shouldn't be needed */
481 break;
482 case USER_PROCESS:
483 /*
484 * if they came in on tty[p-y]*, then it is only
485 * a login session if the ut_host field is non-empty
486 */
487 if (strncmp(u->ut_line, "tty", 3) != 0 ||
488 strchr("pqrstuvwxy", u->ut_line[3]) == 0 ||
489 *u->ut_host != '\0')
490 head = log_in(head, u);
491 break;
492 case DEAD_PROCESS:
493 head = log_out(head, u);
494 break;
495 }
496 }
497 endutxent_wtmp();
498 bzero(&end, sizeof(end));
499 end.ut_tv.tv_sec = time((time_t *)0);
500 end.ut_type = SHUTDOWN_TIME;
501
502 if (Flags & AC_D) {
503 ltm = localtime(&end.ut_tv.tv_sec);
504 if (day >= 0 && day != ltm->tm_yday) {
505 /*
506 * print yesterday's total
507 */
508 secs = end.ut_tv.tv_sec;
509 secs -= ltm->tm_sec;
510 secs -= 60 * ltm->tm_min;
511 secs -= 3600 * ltm->tm_hour;
512 show_today(Users, head, secs);
513 }
514 }
515 /*
516 * anyone still logged in gets time up to now
517 */
518 head = log_out(head, &end);
519
520 if (Flags & AC_D)
521 show_today(Users, head, time((time_t *)0));
522 else {
523 if (Flags & AC_P)
524 show_users(Users);
525 show("total", Total);
526 }
527 return 0;
528 }
529
530 void
531 usage(void)
532 {
533 (void)fprintf(stderr,
534 #ifdef CONSOLE_TTY
535 "ac [-dp] [-c console] [-t tty] [-w wtmp] [users ...]\n");
536 #else
537 "ac [-dp] [-t tty] [-w wtmp] [users ...]\n");
538 #endif
539 exit(1);
540 }