1 /* $OpenBSD: lastcomm.c,v 1.17 2009/10/27 23:59:39 deraadt Exp $ */ 2 /* $NetBSD: lastcomm.c,v 1.9 1995/10/22 01:43:42 ghudson Exp $ */ 3 4 /* 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/stat.h> 35 #include <sys/acct.h> 36 37 #include <ctype.h> 38 #include <err.h> 39 #include <fcntl.h> 40 #include <math.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <struct.h> 45 #include <tzfile.h> 46 #include <unistd.h> 47 #include <utmp.h> 48 #include <pwd.h> 49 #include "pathnames.h" 50 51 time_t expand(u_int); 52 char *flagbits(int); 53 char *getdev(dev_t); 54 int requested(char *[], struct acct *); 55 void usage(void); 56 57 int 58 main(int argc, char *argv[]) 59 { 60 char *p; 61 struct acct ab; 62 struct stat sb; 63 FILE *fp; 64 off_t size; 65 time_t t; 66 double delta; 67 int ch; 68 char *acctfile; 69 70 acctfile = _PATH_ACCT; 71 while ((ch = getopt(argc, argv, "f:")) != -1) 72 switch((char)ch) { 73 case 'f': 74 acctfile = optarg; 75 break; 76 case '?': 77 default: 78 usage(); 79 } 80 argc -= optind; 81 argv += optind; 82 83 /* Open the file. */ 84 if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb)) 85 err(1, "%s", acctfile); 86 87 /* 88 * Round off to integral number of accounting records, probably 89 * not necessary, but it doesn't hurt. 90 */ 91 size = sb.st_size - sb.st_size % sizeof(struct acct); 92 93 /* Check if any records to display. */ 94 if (size < sizeof(struct acct)) 95 exit(0); 96 97 /* 98 * Seek to before the last entry in the file; use lseek(2) in case 99 * the file is bigger than a "long". 100 */ 101 size -= sizeof(struct acct); 102 if (lseek(fileno(fp), size, SEEK_SET) == -1) 103 err(1, "%s", acctfile); 104 105 for (;;) { 106 if (fread(&ab, sizeof(struct acct), 1, fp) != 1) 107 err(1, "%s", acctfile); 108 109 if (ab.ac_comm[0] == '\0') { 110 ab.ac_comm[0] = '?'; 111 ab.ac_comm[1] = '\0'; 112 } else 113 for (p = &ab.ac_comm[0]; 114 p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p) 115 if (!isprint(*p)) 116 *p = '?'; 117 if (!*argv || requested(argv, &ab)) 118 { 119 t = expand(ab.ac_utime) + expand(ab.ac_stime); 120 (void)printf("%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s", 121 (int)fldsiz(acct, ac_comm), 122 (int)fldsiz(acct, ac_comm), 123 ab.ac_comm, flagbits(ab.ac_flag), UT_NAMESIZE, 124 UT_NAMESIZE, user_from_uid(ab.ac_uid, 0), 125 UT_LINESIZE, UT_LINESIZE, getdev(ab.ac_tty), 126 t / (double)AHZ, ctime(&ab.ac_btime)); 127 delta = expand(ab.ac_etime) / (double)AHZ; 128 printf(" (%1.0f:%02.0f:%05.2f)\n", 129 delta / SECSPERHOUR, 130 fmod(delta, (double)SECSPERHOUR) / SECSPERMIN, 131 fmod(delta, (double)SECSPERMIN)); 132 } 133 134 if (size == 0) 135 break; 136 /* seek to previous entry */ 137 if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1) 138 err(1, "%s", acctfile); 139 size -= sizeof(struct acct); 140 } 141 exit(0); 142 } 143 144 time_t 145 expand(u_int t) 146 { 147 time_t nt; 148 149 nt = t & 017777; 150 t >>= 13; 151 while (t) { 152 t--; 153 nt <<= 3; 154 } 155 return (nt); 156 } 157 158 char * 159 flagbits(int f) 160 { 161 static char flags[20] = "-"; 162 char *p; 163 164 #define BIT(flag, ch) if (f & flag) *p++ = ch 165 166 p = flags + 1; 167 BIT(ASU, 'S'); 168 BIT(AFORK, 'F'); 169 BIT(ACOMPAT, 'C'); 170 BIT(ACORE, 'D'); 171 BIT(AXSIG, 'X'); 172 *p = '\0'; 173 return (flags); 174 } 175 176 int 177 requested(char *argv[], struct acct *acp) 178 { 179 do { 180 if (!strcmp(user_from_uid(acp->ac_uid, 0), *argv)) 181 return (1); 182 if (!strcmp(getdev(acp->ac_tty), *argv)) 183 return (1); 184 if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm))) 185 return (1); 186 } while (*++argv); 187 return (0); 188 } 189 190 char * 191 getdev(dev_t dev) 192 { 193 static dev_t lastdev = (dev_t)-1; 194 static char *lastname; 195 196 if (dev == NODEV) /* Special case. */ 197 return ("__"); 198 if (dev == lastdev) /* One-element cache. */ 199 return (lastname); 200 lastdev = dev; 201 if ((lastname = devname(dev, S_IFCHR)) == NULL) 202 lastname = "??"; 203 return (lastname); 204 } 205 206 void 207 usage(void) 208 { 209 (void)fprintf(stderr, 210 "usage: lastcomm [-f file] [command ...] [user ...] [terminal ...]\n"); 211 exit(1); 212 } 213