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