8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / lastcomm / lc_pacct.c
blob735b4c3d2fea0ba9e3e9f6289baaed380993e5c0
1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
6 * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
7 * All rights reserved.
10 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
11 * Use is subject to license terms.
14 #pragma ident "%Z%%M% %I% %E% SMI"
16 #include "lastcomm.h"
19 * lc_pacct() provides the functionality of lastcomm when applied to the basic
20 * SVR4 accounting file, /var/adm/pacct. Definitions for this accounting file
21 * format are given in <sys/acct.h>.
24 extern ulong_t expand(comp_t);
26 static int
27 ok(int argc, char *argv[], int index, struct acct *acp)
29 int j;
31 for (j = index; j < argc; j++)
32 if (strcmp(getname(acp->ac_uid), argv[j]) &&
33 strcmp(getdev(acp->ac_tty), argv[j]) &&
34 strncmp(acp->ac_comm, argv[j], fldsiz(acct, ac_comm)))
35 break;
36 return (j == argc);
39 int
40 lc_pacct(char *name, int argc, char *argv[], int index)
42 struct acct buf[NACCT];
43 int bn, cc;
44 struct acct *acp;
45 struct stat sb;
46 time_t t;
47 int fd;
49 if ((fd = open(name, O_RDONLY)) < 0) {
50 perror(name);
51 return (1);
54 (void) fstat(fd, &sb);
56 if (sb.st_size % sizeof (struct acct)) {
57 (void) fprintf(stderr, gettext("lastcomm: accounting file"
58 " is corrupted\n"));
59 return (1);
62 for (bn = ((unsigned)sb.st_size / BUF_SIZ) + 1; bn >= 0; bn--) {
63 if (lseek(fd, (unsigned)bn * BUF_SIZ, 0) == -1) {
64 perror("lseek");
65 return (1);
67 cc = read(fd, buf, BUF_SIZ);
68 if (cc < 0) {
69 perror("read");
70 return (1);
72 acp = buf + (cc / sizeof (buf[0])) - 1;
73 for (; acp >= buf; acp--) {
74 char *cp;
75 ulong_t x;
77 if (acp->ac_flag > 0100) {
78 (void) fprintf(stderr, gettext("lastcomm: "
79 "accounting file is corrupted\n"));
80 return (1);
82 if (acp->ac_comm[0] == '\0')
83 (void) strcpy(acp->ac_comm, "?");
84 for (cp = &acp->ac_comm[0];
85 cp < &acp->ac_comm[fldsiz(acct, ac_comm)] && *cp;
86 cp++)
87 if (!isascii(*cp) || iscntrl(*cp))
88 *cp = '?';
89 if (argc > index && !ok(argc, argv, index, acp))
90 continue;
91 x = expand(acp->ac_utime) + expand(acp->ac_stime);
92 t = acp->ac_btime;
93 (void) printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
94 fldsiz(acct, ac_comm), fldsiz(acct, ac_comm),
95 acp->ac_comm,
96 flagbits(acp->ac_flag),
97 NMAX, getname(acp->ac_uid),
98 LMAX, getdev(acp->ac_tty),
99 x / (double)HZ, ctime(&t));
102 return (0);