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