1 /* 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1980, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)lastcomm.c 8.1 (Berkeley) 6/6/93"; 41 #endif 42 #endif /* not lint */ 43 #include <sys/cdefs.h> 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 #include <sys/acct.h> 47 48 #include <ctype.h> 49 #include <err.h> 50 #include <errno.h> 51 #include <pwd.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <time.h> 56 #include <unistd.h> 57 #include "pathnames.h" 58 59 /*XXX*/#include <inttypes.h> 60 61 time_t expand(u_int); 62 char *flagbits(int); 63 const char *getdev(dev_t); 64 int readrec_forward(FILE *f, struct acctv3 *av3); 65 int readrec_backward(FILE *f, struct acctv3 *av3); 66 int requested(char *[], struct acctv3 *); 67 static void usage(void); 68 69 #define AC_UTIME 1 /* user */ 70 #define AC_STIME 2 /* system */ 71 #define AC_ETIME 4 /* elapsed */ 72 #define AC_CTIME 8 /* user + system time, default */ 73 74 #define AC_BTIME 16 /* starting time */ 75 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/ 76 77 int 78 main(int argc, char *argv[]) 79 { 80 struct acctv3 ab; 81 char *p; 82 FILE *fp; 83 int (*readrec)(FILE *f, struct acctv3 *av3); 84 time_t t; 85 int ch, rv; 86 const char *acctfile, *format; 87 char buf[1024]; 88 int flags = 0; 89 90 acctfile = _PATH_ACCT; 91 format = NULL; 92 while ((ch = getopt(argc, argv, "f:usecSE")) != -1) 93 switch((char)ch) { 94 case 'f': 95 acctfile = optarg; 96 break; 97 98 case 'u': 99 flags |= AC_UTIME; /* user time */ 100 break; 101 case 's': 102 flags |= AC_STIME; /* system time */ 103 break; 104 case 'e': 105 flags |= AC_ETIME; /* elapsed time */ 106 break; 107 case 'c': 108 flags |= AC_CTIME; /* user + system time */ 109 break; 110 111 case 'S': 112 flags |= AC_BTIME; /* starting time */ 113 break; 114 case 'E': 115 /* exit time (starting time + elapsed time )*/ 116 flags |= AC_FTIME; 117 break; 118 119 case '?': 120 default: 121 usage(); 122 } 123 124 /* default user + system time and starting time */ 125 if (!flags) { 126 flags = AC_CTIME | AC_BTIME; 127 } 128 129 argc -= optind; 130 argv += optind; 131 132 if (argc > 0 && **argv == '+') { 133 format = *argv + 1; /* skip + */ 134 argc--; 135 argv++; 136 } 137 138 if (strcmp(acctfile, "-") == 0) { 139 fp = stdin; 140 readrec = readrec_forward; 141 } else { 142 /* Open the file. */ 143 if ((fp = fopen(acctfile, "r")) == NULL) 144 err(1, "could not open %s", acctfile); 145 if (fseek(fp, 0l, SEEK_END) == -1) 146 err(1, "seek to end of %s failed", acctfile); 147 readrec = readrec_backward; 148 } 149 150 while ((rv = readrec(fp, &ab)) == 1) { 151 for (p = &ab.ac_comm[0]; 152 p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p) 153 if (!isprint(*p)) 154 *p = '?'; 155 156 if (*argv && !requested(argv, &ab)) 157 continue; 158 159 (void)printf("%-*.*s %-7s %-*s %-8s", 160 AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm, 161 flagbits(ab.ac_flagx), 162 MAXLOGNAME - 1, user_from_uid(ab.ac_uid, 0), 163 getdev(ab.ac_tty)); 164 165 166 /* user + system time */ 167 if (flags & AC_CTIME) { 168 (void)printf(" %6.3f secs", 169 (ab.ac_utime + ab.ac_stime) / 1000000); 170 } 171 172 /* usr time */ 173 if (flags & AC_UTIME) { 174 (void)printf(" %6.3f us", ab.ac_utime / 1000000); 175 } 176 177 /* system time */ 178 if (flags & AC_STIME) { 179 (void)printf(" %6.3f sy", ab.ac_stime / 1000000); 180 } 181 182 /* elapsed time */ 183 if (flags & AC_ETIME) { 184 (void)printf(" %8.3f es", ab.ac_etime / 1000000); 185 } 186 187 /* starting time */ 188 if (flags & AC_BTIME) { 189 if (format != NULL) { 190 (void)strftime(buf, sizeof(buf), format, 191 localtime(&ab.ac_btime)); 192 (void)printf(" %s", buf); 193 } else 194 (void)printf(" %.16s", ctime(&ab.ac_btime)); 195 } 196 197 /* exit time (starting time + elapsed time )*/ 198 if (flags & AC_FTIME) { 199 t = ab.ac_btime; 200 t += (time_t)(ab.ac_etime / 1000000); 201 if (format != NULL) { 202 (void)strftime(buf, sizeof(buf), format, 203 localtime(&t)); 204 (void)printf(" %s", buf); 205 } else 206 (void)printf(" %.16s", ctime(&t)); 207 } 208 printf("\n"); 209 } 210 if (rv == EOF) 211 err(1, "read record from %s failed", acctfile); 212 213 if (fflush(stdout)) 214 err(1, "stdout"); 215 exit(0); 216 } 217 218 char * 219 flagbits(int f) 220 { 221 static char flags[20] = "-"; 222 char *p; 223 224 #define BIT(flag, ch) if (f & flag) *p++ = ch 225 226 p = flags + 1; 227 BIT(ASU, 'S'); 228 BIT(AFORK, 'F'); 229 BIT(ACOMPAT, 'C'); 230 BIT(ACORE, 'D'); 231 BIT(AXSIG, 'X'); 232 *p = '\0'; 233 return (flags); 234 } 235 236 int 237 requested(char *argv[], struct acctv3 *acp) 238 { 239 const char *p; 240 241 do { 242 p = user_from_uid(acp->ac_uid, 0); 243 if (!strcmp(p, *argv)) 244 return (1); 245 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv)) 246 return (1); 247 if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN)) 248 return (1); 249 } while (*++argv); 250 return (0); 251 } 252 253 const char * 254 getdev(dev_t dev) 255 { 256 static dev_t lastdev = (dev_t)-1; 257 static const char *lastname; 258 259 if (dev == NODEV) /* Special case. */ 260 return ("__"); 261 if (dev == lastdev) /* One-element cache. */ 262 return (lastname); 263 lastdev = dev; 264 lastname = devname(dev, S_IFCHR); 265 return (lastname); 266 } 267 268 static void 269 usage(void) 270 { 271 (void)fprintf(stderr, 272 "usage: lastcomm [-EScesu] [-f file] [+format] [command ...] " 273 "[user ...] [terminal ...]\n"); 274 exit(1); 275 } 276