xref: /titanic_51/usr/src/cmd/lastcomm/lastcomm.c (revision f875b4ebb1dd9fdbeb043557cab38ab3bf7f6e01)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/acctctl.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34 
35 #include "lastcomm.h"
36 
37 int
38 main(int argc, char *argv[])
39 {
40 	int res;
41 	int opt;
42 	char *filename = NULL;
43 	char buf[PATH_MAX];
44 	ea_file_t ef;
45 	int xflag = 0;
46 
47 	while ((opt = getopt(argc, argv, "f:x")) != EOF) {
48 		switch (opt) {
49 		case 'f':
50 			filename = optarg;
51 			break;
52 		case 'x':
53 			xflag = 1;
54 			break;
55 		default:
56 			(void) fprintf(stderr,
57 			    gettext("Usage:\tlastcomm [-x] [-f filename]"
58 			    " [command] ... [user] ... [terminal] ...\n"));
59 			exit(2);
60 		}
61 	}
62 
63 	if (xflag) {
64 		/*
65 		 * User wants to see extended accounting statistics.
66 		 */
67 		if (filename) {
68 			return (lc_exacct(filename, argc, argv, optind));
69 		} else {
70 			if (acctctl(AC_PROC | AC_FILE_GET, buf, PATH_MAX) < 0) {
71 				(void) fprintf(stderr, gettext("lastcomm: "
72 				    "cannot open extended accounting file: "
73 				    "%s\n"), strerror(errno));
74 				return (1);
75 			} else {
76 				return (lc_exacct(buf, argc, argv, optind));
77 			}
78 		}
79 	}
80 	if (filename == NULL) {
81 		/*
82 		 * If no option is specified, then first try to open current
83 		 * extended process accounting file and then old-style process
84 		 * accounting.
85 		 */
86 		if (acctctl(AC_PROC | AC_FILE_GET, buf, PATH_MAX) < 0)
87 			return (lc_pacct("/var/adm/pacct", argc, argv, optind));
88 		else
89 			return (lc_exacct(buf, argc, argv, optind));
90 	} else {
91 		/*
92 		 * If accounting file was specified and we don't know its
93 		 * format, then first try to open it as an extended accounting
94 		 * file and then as an old-style accounting file.
95 		 */
96 		if ((res = ea_open(&ef, filename, EXACCT_CREATOR,
97 		    EO_TAIL | EO_VALID_HDR, O_RDONLY, 0)) >= 0)
98 			(void) ea_close(&ef);
99 
100 		if (res < 0)
101 			return (lc_pacct(filename, argc, argv, optind));
102 		else
103 			return (lc_exacct(filename, argc, argv, optind));
104 	}
105 }
106