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 * $Id$ 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1980, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 static char sccsid[] = "@(#)lastcomm.c 8.1 (Berkeley) 6/6/93"; 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/stat.h> 48 #include <sys/acct.h> 49 50 #include <ctype.h> 51 #include <err.h> 52 #include <fcntl.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <struct.h> 57 #include <unistd.h> 58 #include <utmp.h> 59 #include "pathnames.h" 60 61 time_t expand __P((u_int)); 62 char *flagbits __P((int)); 63 char *getdev __P((dev_t)); 64 int requested __P((char *[], struct acct *)); 65 void usage __P((void)); 66 char *user_from_uid(); 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 #define AC_HZ ((double)AHZ) 77 78 int 79 main(argc, argv) 80 int argc; 81 char *argv[]; 82 { 83 register char *p; 84 struct acct ab; 85 struct stat sb; 86 FILE *fp; 87 off_t size; 88 time_t t; 89 int ch; 90 char *acctfile; 91 int time = 0; 92 93 acctfile = _PATH_ACCT; 94 while ((ch = getopt(argc, argv, "f:usecSE")) != EOF) 95 switch((char)ch) { 96 case 'f': 97 acctfile = optarg; 98 break; 99 100 case 'u': 101 time |= AC_UTIME; /* user time */ 102 break; 103 case 's': 104 time |= AC_STIME; /* system time */ 105 break; 106 case 'e': 107 time |= AC_ETIME; /* elapsed time */ 108 break; 109 case 'c': 110 time |= AC_CTIME; /* user + system time */ 111 break; 112 113 case 'S': 114 time |= AC_BTIME; /* starting time */ 115 break; 116 case 'E': 117 /* exit time (starting time + elapsed time )*/ 118 time |= AC_FTIME; 119 break; 120 121 case '?': 122 default: 123 usage(); 124 } 125 126 /* default user + system time and starting time */ 127 if (!time) { 128 time = AC_CTIME | AC_BTIME; 129 } 130 131 argc -= optind; 132 argv += optind; 133 134 /* Open the file. */ 135 if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb)) 136 err(1, "%s", acctfile); 137 138 /* 139 * Round off to integral number of accounting records, probably 140 * not necessary, but it doesn't hurt. 141 */ 142 size = sb.st_size - sb.st_size % sizeof(struct acct); 143 144 /* Check if any records to display. */ 145 if (size < sizeof(struct acct)) 146 exit(0); 147 148 /* 149 * Seek to before the last entry in the file; use lseek(2) in case 150 * the file is bigger than a "long". 151 */ 152 size -= sizeof(struct acct); 153 if (lseek(fileno(fp), size, SEEK_SET) == -1) 154 err(1, "%s", acctfile); 155 156 for (;;) { 157 if (fread(&ab, sizeof(struct acct), 1, fp) != 1) 158 err(1, "%s", acctfile); 159 160 if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1) 161 err(1, "%s", acctfile); 162 163 if (size == 0) 164 break; 165 size -= sizeof(struct acct); 166 167 if (ab.ac_comm[0] == '\0') { 168 ab.ac_comm[0] = '?'; 169 ab.ac_comm[1] = '\0'; 170 } else 171 for (p = &ab.ac_comm[0]; 172 p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p) 173 if (!isprint(*p)) 174 *p = '?'; 175 if (*argv && !requested(argv, &ab)) 176 continue; 177 178 (void)printf("%-*s %-7s %-*s %-*s ", 179 fldsiz(acct, ac_comm), ab.ac_comm, 180 flagbits(ab.ac_flag), 181 UT_NAMESIZE, user_from_uid(ab.ac_uid, 0), 182 UT_LINESIZE, getdev(ab.ac_tty)); 183 184 185 /* user + system time */ 186 if (time & AC_CTIME) { 187 (void)printf("%6.2f secs ", 188 (expand(ab.ac_utime) + 189 expand(ab.ac_stime))/AC_HZ); 190 } 191 192 /* usr time */ 193 if (time & AC_UTIME) { 194 (void)printf("%6.2f us ", expand(ab.ac_utime)/AC_HZ); 195 } 196 197 /* system time */ 198 if (time & AC_STIME) { 199 (void)printf("%6.2f sy ", expand(ab.ac_stime)/AC_HZ); 200 } 201 202 /* elapsed time */ 203 if (time & AC_ETIME) { 204 (void)printf("%8.2f es ", expand(ab.ac_etime)/AC_HZ); 205 } 206 207 /* starting time */ 208 if (time & AC_BTIME) { 209 (void)printf("%.16s ", ctime(&ab.ac_btime)); 210 } 211 212 /* exit time (starting time + elapsed time )*/ 213 if (time & AC_FTIME) { 214 t = ab.ac_btime; 215 t += (time_t)(expand(ab.ac_etime)/AC_HZ); 216 (void)printf("%.16s ", 217 ctime(&t)); 218 } 219 printf("\n"); 220 } 221 exit(0); 222 } 223 224 time_t 225 expand(t) 226 u_int t; 227 { 228 register time_t nt; 229 230 nt = t & 017777; 231 t >>= 13; 232 while (t) { 233 t--; 234 nt <<= 3; 235 } 236 return (nt); 237 } 238 239 char * 240 flagbits(f) 241 register int f; 242 { 243 static char flags[20] = "-"; 244 char *p; 245 246 #define BIT(flag, ch) if (f & flag) *p++ = ch 247 248 p = flags + 1; 249 BIT(ASU, 'S'); 250 BIT(AFORK, 'F'); 251 BIT(ACOMPAT, 'C'); 252 BIT(ACORE, 'D'); 253 BIT(AXSIG, 'X'); 254 *p = '\0'; 255 return (flags); 256 } 257 258 int 259 requested(argv, acp) 260 register char *argv[]; 261 register struct acct *acp; 262 { 263 register char *p; 264 265 do { 266 p = user_from_uid(acp->ac_uid, 0); 267 if (!strcmp(p, *argv)) 268 return (1); 269 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv)) 270 return (1); 271 if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm))) 272 return (1); 273 } while (*++argv); 274 return (0); 275 } 276 277 char * 278 getdev(dev) 279 dev_t dev; 280 { 281 static dev_t lastdev = (dev_t)-1; 282 static char *lastname; 283 284 if (dev == NODEV) /* Special case. */ 285 return ("__"); 286 if (dev == lastdev) /* One-element cache. */ 287 return (lastname); 288 lastdev = dev; 289 lastname = devname(dev, S_IFCHR); 290 return (lastname); 291 } 292 293 void 294 usage() 295 { 296 (void)fprintf(stderr, 297 "lastcomm [-EScesu] [ -f file ] [command ...] [user ...] [tty ...]\n"); 298 exit(1); 299 } 300