xref: /illumos-gate/usr/src/cmd/lastcomm/lastcomm.c (revision 2a8bcb4e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1983-2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <sys/acctctl.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 
33 #include "lastcomm.h"
34 
35 int
main(int argc,char * argv[])36 main(int argc, char *argv[])
37 {
38 	int res;
39 	int opt;
40 	char *filename = NULL;
41 	char buf[PATH_MAX];
42 	ea_file_t ef;
43 	int xflag = 0;
44 
45 	while ((opt = getopt(argc, argv, "f:x")) != EOF) {
46 		switch (opt) {
47 		case 'f':
48 			filename = optarg;
49 			break;
50 		case 'x':
51 			xflag = 1;
52 			break;
53 		default:
54 			(void) fprintf(stderr,
55 			    gettext("Usage:\tlastcomm [-x] [-f filename]"
56 			    " [command] ... [user] ... [terminal] ...\n"));
57 			exit(2);
58 		}
59 	}
60 
61 	if (xflag) {
62 		/*
63 		 * User wants to see extended accounting statistics.
64 		 */
65 		if (filename) {
66 			return (lc_exacct(filename, argc, argv, optind));
67 		} else {
68 			if (acctctl(AC_PROC | AC_FILE_GET, buf, PATH_MAX) < 0) {
69 				(void) fprintf(stderr, gettext("lastcomm: "
70 				    "cannot open extended accounting file: "
71 				    "%s\n"), strerror(errno));
72 				return (1);
73 			} else {
74 				return (lc_exacct(buf, argc, argv, optind));
75 			}
76 		}
77 	}
78 	if (filename == NULL) {
79 		/*
80 		 * If no option is specified, then first try to open current
81 		 * extended process accounting file and then old-style process
82 		 * accounting.
83 		 */
84 		if (acctctl(AC_PROC | AC_FILE_GET, buf, PATH_MAX) < 0)
85 			return (lc_pacct("/var/adm/pacct", argc, argv, optind));
86 		else
87 			return (lc_exacct(buf, argc, argv, optind));
88 	} else {
89 		/*
90 		 * If accounting file was specified and we don't know its
91 		 * format, then first try to open it as an extended accounting
92 		 * file and then as an old-style accounting file.
93 		 */
94 		if ((res = ea_open(&ef, filename, EXACCT_CREATOR,
95 		    EO_TAIL | EO_VALID_HDR, O_RDONLY, 0)) >= 0)
96 			(void) ea_close(&ef);
97 
98 		if (res < 0)
99 			return (lc_pacct(filename, argc, argv, optind));
100 		else
101 			return (lc_exacct(filename, argc, argv, optind));
102 	}
103 }
104