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 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 static char sccsid[] = "@(#)lastcomm.c 8.1 (Berkeley) 6/6/93"; 42 #endif /* not lint */ 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 <fcntl.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <struct.h> 55 #include <unistd.h> 56 #include <utmp.h> 57 #include "pathnames.h" 58 59 time_t expand __P((u_int)); 60 char *flagbits __P((int)); 61 char *getdev __P((dev_t)); 62 int requested __P((char *[], struct acct *)); 63 void usage __P((void)); 64 char *user_from_uid(); 65 66 int 67 main(argc, argv) 68 int argc; 69 char *argv[]; 70 { 71 register char *p; 72 struct acct ab; 73 struct stat sb; 74 FILE *fp; 75 off_t size; 76 time_t t; 77 int ch; 78 char *acctfile; 79 80 acctfile = _PATH_ACCT; 81 while ((ch = getopt(argc, argv, "f:")) != EOF) 82 switch((char)ch) { 83 case 'f': 84 acctfile = optarg; 85 break; 86 case '?': 87 default: 88 usage(); 89 } 90 argc -= optind; 91 argv += optind; 92 93 /* Open the file. */ 94 if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb)) 95 err(1, "%s", acctfile); 96 97 /* 98 * Round off to integral number of accounting records, probably 99 * not necessary, but it doesn't hurt. 100 */ 101 size = sb.st_size - sb.st_size % sizeof(struct acct); 102 103 /* Check if any records to display. */ 104 if (size < sizeof(struct acct)) 105 exit(0); 106 107 /* 108 * Seek to before the last entry in the file; use lseek(2) in case 109 * the file is bigger than a "long". 110 */ 111 size -= sizeof(struct acct); 112 if (lseek(fileno(fp), size, SEEK_SET) == -1) 113 err(1, "%s", acctfile); 114 115 for (;;) { 116 if (fread(&ab, sizeof(struct acct), 1, fp) != 1) 117 err(1, "%s", acctfile); 118 119 if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1) 120 err(1, "%s", acctfile); 121 122 if (size == 0) 123 break; 124 size -= sizeof(struct acct); 125 126 if (ab.ac_comm[0] == '\0') { 127 ab.ac_comm[0] = '?'; 128 ab.ac_comm[1] = '\0'; 129 } else 130 for (p = &ab.ac_comm[0]; 131 p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p) 132 if (!isprint(*p)) 133 *p = '?'; 134 if (*argv && !requested(argv, &ab)) 135 continue; 136 137 t = expand(ab.ac_utime) + expand(ab.ac_stime); 138 (void)printf("%-*s %-7s %-*s %-*s %6.2f secs %.16s\n", 139 fldsiz(acct, ac_comm), ab.ac_comm, flagbits(ab.ac_flag), 140 UT_NAMESIZE, user_from_uid(ab.ac_uid, 0), 141 UT_LINESIZE, getdev(ab.ac_tty), 142 t / (double)AHZ, ctime(&ab.ac_btime)); 143 } 144 exit(0); 145 } 146 147 time_t 148 expand(t) 149 u_int t; 150 { 151 register time_t nt; 152 153 nt = t & 017777; 154 t >>= 13; 155 while (t) { 156 t--; 157 nt <<= 3; 158 } 159 return (nt); 160 } 161 162 char * 163 flagbits(f) 164 register int f; 165 { 166 static char flags[20] = "-"; 167 char *p; 168 169 #define BIT(flag, ch) if (f & flag) *p++ = ch 170 171 p = flags + 1; 172 BIT(ASU, 'S'); 173 BIT(AFORK, 'F'); 174 BIT(ACOMPAT, 'C'); 175 BIT(ACORE, 'D'); 176 BIT(AXSIG, 'X'); 177 *p = '\0'; 178 return (flags); 179 } 180 181 int 182 requested(argv, acp) 183 register char *argv[]; 184 register struct acct *acp; 185 { 186 register char *p; 187 188 do { 189 p = user_from_uid(acp->ac_uid, 0); 190 if (!strcmp(p, *argv)) 191 return (1); 192 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv)) 193 return (1); 194 if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm))) 195 return (1); 196 } while (*++argv); 197 return (0); 198 } 199 200 char * 201 getdev(dev) 202 dev_t dev; 203 { 204 static dev_t lastdev = (dev_t)-1; 205 static char *lastname; 206 207 if (dev == NODEV) /* Special case. */ 208 return ("__"); 209 if (dev == lastdev) /* One-element cache. */ 210 return (lastname); 211 lastdev = dev; 212 lastname = devname(dev, S_IFCHR); 213 return (lastname); 214 } 215 216 void 217 usage() 218 { 219 (void)fprintf(stderr, 220 "lastcomm [ -f file ] [command ...] [user ...] [tty ...]\n"); 221 exit(1); 222 } 223