/[gentoo-projects]/pax-utils/scanelf.c
Gentoo

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download) (as text)
Sat Nov 1 08:58:23 2003 UTC (11 years, 1 month ago) by solar
Branch: MAIN
Changes since 1.3: +24 -24 lines
File MIME type: text/x-csrc
small touchups

1 /*
2 * Copyright 2003 Ned Ludd <solar@gentoo.org>
3 * Copyright 1999-2003 Gentoo Technologies, Inc.
4 * Distributed under the terms of the GNU General Public License v2
5 * $Header: /home/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.3 2003/10/26 23:42:10 solar Exp $
6 *
7 ********************************************************************
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * MA 02111-1307, USA.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <dirent.h>
31 #include <getopt.h>
32
33 #include "paxelf.h"
34
35 static const char *rcsid =
36 "$Id: scanelf.c,v 1.3 2003/10/26 23:42:10 solar Exp $";
37
38 #define PARSE_FLAGS "hvlp"
39 static struct option const long_options[] = {
40 {"help", no_argument, 0, 'h'},
41 {"version", no_argument, 0, 'v'},
42 {"path", no_argument, 0, 'p'},
43 {"ldpath", no_argument, 0, 'l'},
44 {NULL, no_argument, NULL, 0}
45 };
46
47 /* scan a directory for ET_EXEC files and print when we find one */
48 void scanelf(const char *path)
49 {
50 elfobj *elf = NULL;
51 register DIR *dir;
52 register struct dirent *dentry;
53
54 if ((chdir(path) == 0) && ((dir = opendir(path)))) {
55 while ((dentry = readdir(dir))) {
56 /* verify this is real ELF */
57 if ((elf = readelf(dentry->d_name)) != NULL) {
58 if (!check_elf_header(elf->ehdr))
59 if (IS_ELF(elf))
60 printf("%s %s %s/%s\n",
61 pax_short_flags(PAX_FLAGS(elf)),
62 get_elfetype(elf->ehdr->e_type), path,
63 dentry->d_name);
64 if (elf != NULL) {
65 munmap(elf->data, elf->len);
66 free(elf);
67 elf = NULL;
68 }
69 }
70 }
71 closedir(dir);
72 }
73 }
74
75
76 /* display usage and exit */
77 int usage(char **argv)
78 {
79 fprintf(stderr,
80 "Usage: %s [options] dir1 dir2 dirN...\n",
81 (*argv != NULL) ? argv[0] : __FILE__ "\b\b");
82 exit(EXIT_FAILURE);
83 }
84
85
86 void showopt(int c, char *data)
87 {
88 int i;
89 for (i = 0; long_options[i].name; i++)
90 if (long_options[i].val == c)
91 fprintf(stderr, " -%c, --%s\t: %s\n", c, long_options[i].name,
92 data);
93 }
94
95 /* parse command line arguments and preform needed actions */
96 void parseargs(int argc, char **argv)
97 {
98 int flag;
99 char *p, *path;
100 FILE *fp;
101
102 opterr = 0;
103 while ((flag =
104 (int) getopt_long(argc, argv,
105 PARSE_FLAGS, long_options, NULL)) != EOF) {
106 switch (flag) {
107 case 'h':
108 showopt('p', "Scan all directories in PATH environment.");
109 showopt('l', "Scan all directories in /etc/ld.so.conf");
110 showopt('h', "Print this help and exit.");
111 showopt('v', "Print version and exit.");
112 exit(EXIT_SUCCESS);
113 case 'v':
114 fprintf(stderr, "%s compiled %s\n", __FILE__, __DATE__);
115 fprintf(stderr,
116 "%s written for Gentoo Linux <solar@gentoo.org>\n\t%s\n",
117 (*argv != NULL) ? argv[0] : __FILE__ "\b\b", rcsid);
118 exit(EXIT_SUCCESS);
119 case 'l':
120 /* scan ld.so.conf for ldpath */
121 if ((fp = fopen("/etc/ld.so.conf", "r")) != NULL) {
122 path = malloc(_POSIX_PATH_MAX);
123 while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL) {
124 if (*path == '/') {
125 if ((p = strrchr(path, '\r')) != NULL)
126 *p = 0;
127 if ((p = strrchr(path, '\n')) != NULL)
128 *p = 0;
129 scanelf(path);
130 }
131 }
132 free(path);
133 }
134 break;
135 case 'p':
136 if ((path = strdup(getenv("PATH"))) == NULL) {
137 perror("strdup");
138 exit(EXIT_FAILURE);
139 }
140 /* split string into dirs */
141 while ((p = strrchr(path, ':')) != NULL) {
142 scanelf(p + 1);
143 *p = 0;
144 }
145 if (path != NULL)
146 free(path);
147 break;
148 case '?':
149 default:
150 break;
151 }
152 }
153 while (optind < argc)
154 scanelf(argv[optind++]);
155 }
156
157 int main(int argc, char **argv)
158 {
159 if (argc < 2)
160 usage(argv);
161 parseargs(argc, argv);
162 return 0;
163 }

  ViewVC Help
Powered by ViewVC 1.1.20