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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1980, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)lastcomm.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 #endif /* not lint */ 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 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 <fcntl.h> 55 #include <pwd.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 #include <utmp.h> 61 #include "pathnames.h" 62 63 time_t expand(u_int); 64 char *flagbits(int); 65 const char *getdev(dev_t); 66 int requested(char *[], struct acct *); 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 #define AC_HZ ((double)AHZ) 78 79 int 80 main(int argc, char *argv[]) 81 { 82 char *p; 83 struct acct ab; 84 struct stat sb; 85 FILE *fp; 86 off_t size; 87 time_t t; 88 int ch; 89 const char *acctfile; 90 int flags = 0; 91 92 acctfile = _PATH_ACCT; 93 while ((ch = getopt(argc, argv, "f:usecSE")) != -1) 94 switch((char)ch) { 95 case 'f': 96 acctfile = optarg; 97 break; 98 99 case 'u': 100 flags |= AC_UTIME; /* user time */ 101 break; 102 case 's': 103 flags |= AC_STIME; /* system time */ 104 break; 105 case 'e': 106 flags |= AC_ETIME; /* elapsed time */ 107 break; 108 case 'c': 109 flags |= AC_CTIME; /* user + system time */ 110 break; 111 112 case 'S': 113 flags |= AC_BTIME; /* starting time */ 114 break; 115 case 'E': 116 /* exit time (starting time + elapsed time )*/ 117 flags |= AC_FTIME; 118 break; 119 120 case '?': 121 default: 122 usage(); 123 } 124 125 /* default user + system time and starting time */ 126 if (!flags) { 127 flags = AC_CTIME | AC_BTIME; 128 } 129 130 argc -= optind; 131 argv += optind; 132 133 /* Open the file. */ 134 if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb)) 135 err(1, "%s", acctfile); 136 137 /* 138 * Round off to integral number of accounting records, probably 139 * not necessary, but it doesn't hurt. 140 */ 141 size = sb.st_size - sb.st_size % sizeof(struct acct); 142 143 /* Check if any records to display. */ 144 if ((unsigned)size < sizeof(struct acct)) 145 exit(0); 146 147 /* 148 * Seek to before the last entry in the file; use lseek(2) in case 149 * the file is bigger than a "long". 150 */ 151 size -= sizeof(struct acct); 152 if (lseek(fileno(fp), size, SEEK_SET) == -1) 153 err(1, "%s", acctfile); 154 155 for (;;) { 156 if (fread(&ab, sizeof(struct acct), 1, fp) != 1) 157 err(1, "%s", acctfile); 158 159 if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1) 160 err(1, "%s", acctfile); 161 162 if (size == 0) 163 break; 164 size -= sizeof(struct acct); 165 166 if (ab.ac_comm[0] == '\0') { 167 ab.ac_comm[0] = '?'; 168 ab.ac_comm[1] = '\0'; 169 } else 170 for (p = &ab.ac_comm[0]; 171 p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p) 172 if (!isprint(*p)) 173 *p = '?'; 174 if (*argv && !requested(argv, &ab)) 175 continue; 176 177 (void)printf("%-*.*s %-7s %-*s %-*s", 178 AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm, 179 flagbits(ab.ac_flag), 180 UT_NAMESIZE, user_from_uid(ab.ac_uid, 0), 181 UT_LINESIZE, getdev(ab.ac_tty)); 182 183 184 /* user + system time */ 185 if (flags & AC_CTIME) { 186 (void)printf(" %6.2f secs", 187 (expand(ab.ac_utime) + 188 expand(ab.ac_stime))/AC_HZ); 189 } 190 191 /* usr time */ 192 if (flags & AC_UTIME) { 193 (void)printf(" %6.2f us", expand(ab.ac_utime)/AC_HZ); 194 } 195 196 /* system time */ 197 if (flags & AC_STIME) { 198 (void)printf(" %6.2f sy", expand(ab.ac_stime)/AC_HZ); 199 } 200 201 /* elapsed time */ 202 if (flags & AC_ETIME) { 203 (void)printf(" %8.2f es", expand(ab.ac_etime)/AC_HZ); 204 } 205 206 /* starting time */ 207 if (flags & AC_BTIME) { 208 (void)printf(" %.16s", ctime(&ab.ac_btime)); 209 } 210 211 /* exit time (starting time + elapsed time )*/ 212 if (flags & AC_FTIME) { 213 t = ab.ac_btime; 214 t += (time_t)(expand(ab.ac_etime)/AC_HZ); 215 (void)printf(" %.16s", ctime(&t)); 216 } 217 printf("\n"); 218 } 219 exit(0); 220 } 221 222 time_t 223 expand(u_int t) 224 { 225 time_t nt; 226 227 nt = t & 017777; 228 t >>= 13; 229 while (t) { 230 t--; 231 nt <<= 3; 232 } 233 return (nt); 234 } 235 236 char * 237 flagbits(int f) 238 { 239 static char flags[20] = "-"; 240 char *p; 241 242 #define BIT(flag, ch) if (f & flag) *p++ = ch 243 244 p = flags + 1; 245 BIT(ASU, 'S'); 246 BIT(AFORK, 'F'); 247 BIT(ACOMPAT, 'C'); 248 BIT(ACORE, 'D'); 249 BIT(AXSIG, 'X'); 250 *p = '\0'; 251 return (flags); 252 } 253 254 int 255 requested(char *argv[], struct acct *acp) 256 { 257 const char *p; 258 259 do { 260 p = user_from_uid(acp->ac_uid, 0); 261 if (!strcmp(p, *argv)) 262 return (1); 263 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv)) 264 return (1); 265 if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN)) 266 return (1); 267 } while (*++argv); 268 return (0); 269 } 270 271 const char * 272 getdev(dev_t dev) 273 { 274 static dev_t lastdev = (dev_t)-1; 275 static const char *lastname; 276 277 if (dev == NODEV) /* Special case. */ 278 return ("__"); 279 if (dev == lastdev) /* One-element cache. */ 280 return (lastname); 281 lastdev = dev; 282 lastname = devname(dev, S_IFCHR); 283 return (lastname); 284 } 285 286 static void 287 usage(void) 288 { 289 (void)fprintf(stderr, 290 "usage: lastcomm [-EScesu] [ -f file ] [command ...] [user ...] [tty ...]\n"); 291 exit(1); 292 } 293