Deleted Added
full compact
lastcomm.c (169549) lastcomm.c (169857)
1/*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 29 unchanged lines hidden (view full) ---

38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)lastcomm.c 8.1 (Berkeley) 6/6/93";
43#endif
44#endif /* not lint */
45#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 29 unchanged lines hidden (view full) ---

38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)lastcomm.c 8.1 (Berkeley) 6/6/93";
43#endif
44#endif /* not lint */
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: head/usr.bin/lastcomm/lastcomm.c 169549 2007-05-14 09:05:22Z dds $");
46__FBSDID("$FreeBSD: head/usr.bin/lastcomm/lastcomm.c 169857 2007-05-22 06:51:38Z dds $");
47
48#include <sys/param.h>
49#include <sys/stat.h>
50#include <sys/acct.h>
51
52#include <ctype.h>
53#include <err.h>
47
48#include <sys/param.h>
49#include <sys/stat.h>
50#include <sys/acct.h>
51
52#include <ctype.h>
53#include <err.h>
54#include <errno.h>
54#include <fcntl.h>
55#include <grp.h>
56#include <pwd.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61#include <utmp.h>
62#include "pathnames.h"
63
64/*XXX*/#include <inttypes.h>
65
66time_t expand(u_int);
67char *flagbits(int);
68const char *getdev(dev_t);
55#include <fcntl.h>
56#include <grp.h>
57#include <pwd.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62#include <utmp.h>
63#include "pathnames.h"
64
65/*XXX*/#include <inttypes.h>
66
67time_t expand(u_int);
68char *flagbits(int);
69const char *getdev(dev_t);
69int requested(char *[], struct acct *);
70int readrec_forward(FILE *f, struct acctv2 *av2);
71int readrec_backward(FILE *f, struct acctv2 *av2);
72int requested(char *[], struct acctv2 *);
70static void usage(void);
71
72#define AC_UTIME 1 /* user */
73#define AC_STIME 2 /* system */
74#define AC_ETIME 4 /* elapsed */
75#define AC_CTIME 8 /* user + system time, default */
76
77#define AC_BTIME 16 /* starting time */
78#define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
79
73static void usage(void);
74
75#define AC_UTIME 1 /* user */
76#define AC_STIME 2 /* system */
77#define AC_ETIME 4 /* elapsed */
78#define AC_CTIME 8 /* user + system time, default */
79
80#define AC_BTIME 16 /* starting time */
81#define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
82
80#define AC_HZ ((double)AHZ)
81
82int
83main(int argc, char *argv[])
84{
83int
84main(int argc, char *argv[])
85{
86 struct acctv2 ab;
85 char *p;
87 char *p;
86 struct acct ab;
87 struct stat sb;
88 FILE *fp;
88 FILE *fp;
89 off_t size;
89 int (*readrec)(FILE *f, struct acctv2 *av2);
90 time_t t;
90 time_t t;
91 int ch;
91 int ch, rv;
92 const char *acctfile;
93 int flags = 0;
94
95 acctfile = _PATH_ACCT;
96 while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
97 switch((char)ch) {
98 case 'f':
99 acctfile = optarg;

--- 30 unchanged lines hidden (view full) ---

130 flags = AC_CTIME | AC_BTIME;
131 }
132
133 argc -= optind;
134 argv += optind;
135
136 if (strcmp(acctfile, "-") == 0) {
137 fp = stdin;
92 const char *acctfile;
93 int flags = 0;
94
95 acctfile = _PATH_ACCT;
96 while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
97 switch((char)ch) {
98 case 'f':
99 acctfile = optarg;

--- 30 unchanged lines hidden (view full) ---

130 flags = AC_CTIME | AC_BTIME;
131 }
132
133 argc -= optind;
134 argv += optind;
135
136 if (strcmp(acctfile, "-") == 0) {
137 fp = stdin;
138 size = sizeof(struct acct); /* Always one more to read. */
138 readrec = readrec_forward;
139 } else {
140 /* Open the file. */
139 } else {
140 /* Open the file. */
141 if ((fp = fopen(acctfile, "r")) == NULL ||
142 fstat(fileno(fp), &sb))
141 if ((fp = fopen(acctfile, "r")) == NULL)
143 err(1, "could not open %s", acctfile);
142 err(1, "could not open %s", acctfile);
144
145 /*
146 * Round off to integral number of accounting records,
147 * probably not necessary, but it doesn't hurt.
148 */
149 size = sb.st_size - sb.st_size % sizeof(struct acct);
150
151 /* Check if any records to display. */
152 if ((unsigned)size < sizeof(struct acct))
153 exit(0);
143 if (fseek(fp, 0l, SEEK_END) == -1)
144 err(1, "seek to end of %s failed", acctfile);
145 readrec = readrec_backward;
154 }
155
146 }
147
156 do {
157 int rv;
148 while ((rv = readrec(fp, &ab)) == 1) {
149 for (p = &ab.ac_comm[0];
150 p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
151 if (!isprint(*p))
152 *p = '?';
158
153
159 if (fp != stdin) {
160 size -= sizeof(struct acct);
161 if (fseeko(fp, size, SEEK_SET) == -1)
162 err(1, "seek %s failed", acctfile);
163 }
164
165 if ((rv = fread(&ab, sizeof(struct acct), 1, fp)) != 1) {
166 if (feof(fp))
167 break;
168 else
169 err(1, "read %s returned %d", acctfile, rv);
170 }
171
172 if (ab.ac_comm[0] == '\0') {
173 ab.ac_comm[0] = '?';
174 ab.ac_comm[1] = '\0';
175 } else
176 for (p = &ab.ac_comm[0];
177 p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
178 if (!isprint(*p))
179 *p = '?';
180 if (*argv && !requested(argv, &ab))
181 continue;
182
183 (void)printf("%-*.*s %-7s %-*s %-*s",
184 AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
154 if (*argv && !requested(argv, &ab))
155 continue;
156
157 (void)printf("%-*.*s %-7s %-*s %-*s",
158 AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
185 flagbits(ab.ac_flag),
159 flagbits(ab.ac_flagx),
186 UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
187 UT_LINESIZE, getdev(ab.ac_tty));
188
189
190 /* user + system time */
191 if (flags & AC_CTIME) {
160 UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
161 UT_LINESIZE, getdev(ab.ac_tty));
162
163
164 /* user + system time */
165 if (flags & AC_CTIME) {
192 (void)printf(" %6.2f secs",
193 (expand(ab.ac_utime) +
194 expand(ab.ac_stime))/AC_HZ);
166 (void)printf(" %6.3f secs",
167 (ab.ac_utime + ab.ac_stime) / 1000000);
195 }
196
197 /* usr time */
198 if (flags & AC_UTIME) {
168 }
169
170 /* usr time */
171 if (flags & AC_UTIME) {
199 (void)printf(" %6.2f us", expand(ab.ac_utime)/AC_HZ);
172 (void)printf(" %6.3f us", ab.ac_utime / 1000000);
200 }
201
202 /* system time */
203 if (flags & AC_STIME) {
173 }
174
175 /* system time */
176 if (flags & AC_STIME) {
204 (void)printf(" %6.2f sy", expand(ab.ac_stime)/AC_HZ);
177 (void)printf(" %6.3f sy", ab.ac_stime / 1000000);
205 }
206
207 /* elapsed time */
208 if (flags & AC_ETIME) {
178 }
179
180 /* elapsed time */
181 if (flags & AC_ETIME) {
209 (void)printf(" %8.2f es", expand(ab.ac_etime)/AC_HZ);
182 (void)printf(" %8.3f es", ab.ac_etime / 1000000);
210 }
211
212 /* starting time */
213 if (flags & AC_BTIME) {
214 (void)printf(" %.16s", ctime(&ab.ac_btime));
215 }
216
217 /* exit time (starting time + elapsed time )*/
218 if (flags & AC_FTIME) {
219 t = ab.ac_btime;
183 }
184
185 /* starting time */
186 if (flags & AC_BTIME) {
187 (void)printf(" %.16s", ctime(&ab.ac_btime));
188 }
189
190 /* exit time (starting time + elapsed time )*/
191 if (flags & AC_FTIME) {
192 t = ab.ac_btime;
220 t += (time_t)(expand(ab.ac_etime)/AC_HZ);
193 t += (time_t)(ab.ac_etime / 1000000);
221 (void)printf(" %.16s", ctime(&t));
222 }
223 printf("\n");
194 (void)printf(" %.16s", ctime(&t));
195 }
196 printf("\n");
197 }
198 if (rv == EOF)
199 err(1, "read record from %s failed", acctfile);
224
200
225 } while (size > 0);
226 if (fflush(stdout))
227 err(1, "stdout");
228 exit(0);
229}
230
201 if (fflush(stdout))
202 err(1, "stdout");
203 exit(0);
204}
205
231time_t
232expand(u_int t)
233{
234 time_t nt;
235
236 nt = t & 017777;
237 t >>= 13;
238 while (t) {
239 t--;
240 nt <<= 3;
241 }
242 return (nt);
243}
244
245char *
246flagbits(int f)
247{
248 static char flags[20] = "-";
249 char *p;
250
251#define BIT(flag, ch) if (f & flag) *p++ = ch
252
253 p = flags + 1;
254 BIT(ASU, 'S');
255 BIT(AFORK, 'F');
256 BIT(ACOMPAT, 'C');
257 BIT(ACORE, 'D');
258 BIT(AXSIG, 'X');
259 *p = '\0';
260 return (flags);
261}
262
263int
206char *
207flagbits(int f)
208{
209 static char flags[20] = "-";
210 char *p;
211
212#define BIT(flag, ch) if (f & flag) *p++ = ch
213
214 p = flags + 1;
215 BIT(ASU, 'S');
216 BIT(AFORK, 'F');
217 BIT(ACOMPAT, 'C');
218 BIT(ACORE, 'D');
219 BIT(AXSIG, 'X');
220 *p = '\0';
221 return (flags);
222}
223
224int
264requested(char *argv[], struct acct *acp)
225requested(char *argv[], struct acctv2 *acp)
265{
266 const char *p;
267
268 do {
269 p = user_from_uid(acp->ac_uid, 0);
270 if (!strcmp(p, *argv))
271 return (1);
272 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))

--- 29 unchanged lines hidden ---
226{
227 const char *p;
228
229 do {
230 p = user_from_uid(acp->ac_uid, 0);
231 if (!strcmp(p, *argv))
232 return (1);
233 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))

--- 29 unchanged lines hidden ---