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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download) (as text)
Sat Jan 10 08:20:07 2004 UTC (10 years, 6 months ago) by solar
Branch: MAIN
Changes since 1.5: +30 -18 lines
File MIME type: text/x-csrc
update scanelf

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.5 2003/11/09 17:46:41 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.5 2003/11/09 17:46:41 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 void scanelf_file(char *filename)
48 {
49 elfobj *elf = NULL;
50 /* verify this is real ELF */
51 if ((elf = readelf(filename)) != NULL) {
52 if (!check_elf_header(elf->ehdr))
53 if (IS_ELF(elf))
54 printf("%s %s %s\n",
55 pax_short_flags(PAX_FLAGS(elf)),
56 get_elfetype(elf->ehdr->e_type), filename);
57
58 if (elf != NULL) {
59 munmap(elf->data, elf->len);
60 free(elf);
61 elf = NULL;
62 }
63 }
64 }
65
66 /* scan a directory for ET_EXEC files and print when we find one */
67 void scanelf(const char *path)
68 {
69 register DIR *dir;
70 register struct dirent *dentry;
71 char *p;
72 int len = 0;
73
74 if ((chdir(path) == 0) && ((dir = opendir(path)))) {
75 while ((dentry = readdir(dir))) {
76 len = (strlen(path) + 2 + strlen(dentry->d_name));
77 p = malloc(len);
78 strncpy(p, path, len);
79 strncat(p, "/", len);
80 strncat(p, dentry->d_name, len);
81 scanelf_file(p);
82 free(p);
83 }
84 closedir(dir);
85 }
86 }
87
88
89 /* display usage and exit */
90 void usage(char **argv)
91 {
92 fprintf(stderr,
93 "Usage: %s [options] dir1 dir2 dirN...\n",
94 (*argv != NULL) ? argv[0] : __FILE__ "\b\b");
95 }
96
97
98 void showopt(int c, char *data)
99 {
100 int i;
101 for (i = 0; long_options[i].name; i++)
102 if (long_options[i].val == c)
103 fprintf(stderr, " -%c, --%s\t: %s\n", c, long_options[i].name,
104 data);
105 }
106
107 /* parse command line arguments and preform needed actions */
108 void parseargs(int argc, char **argv)
109 {
110 int flag;
111 char *p, *path;
112 FILE *fp;
113
114 opterr = 0;
115 while ((flag =
116 (int) getopt_long(argc, argv,
117 PARSE_FLAGS, long_options, NULL)) != EOF) {
118 switch (flag) {
119 case 'h':
120 usage(argv);
121 showopt('p', "Scan all directories in PATH environment.");
122 showopt('l', "Scan all directories in /etc/ld.so.conf");
123 showopt('h', "Print this help and exit.");
124 showopt('v', "Print version and exit.");
125 exit(EXIT_SUCCESS);
126 case 'v':
127 fprintf(stderr, "%s compiled %s\n", __FILE__, __DATE__);
128 fprintf(stderr,
129 "%s written for Gentoo Linux <solar@gentoo.org>\n\t%s\n",
130 (*argv != NULL) ? argv[0] : __FILE__ "\b\b", rcsid);
131 exit(EXIT_SUCCESS);
132 case 'l':
133 /* scan ld.so.conf for ldpath */
134 if ((fp = fopen("/etc/ld.so.conf", "r")) != NULL) {
135 path = malloc(_POSIX_PATH_MAX);
136 while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL) {
137 if (*path == '/') {
138 if ((p = strrchr(path, '\r')) != NULL)
139 *p = 0;
140 if ((p = strrchr(path, '\n')) != NULL)
141 *p = 0;
142 scanelf(path);
143 }
144 }
145 free(path);
146 }
147 break;
148 case 'p':
149 if ((path = strdup(getenv("PATH"))) == NULL) {
150 perror("strdup");
151 exit(EXIT_FAILURE);
152 }
153 /* split string into dirs */
154 while ((p = strrchr(path, ':')) != NULL) {
155 scanelf(p + 1);
156 *p = 0;
157 }
158 if (path != NULL)
159 free(path);
160 break;
161 case '?':
162 default:
163 break;
164 }
165 }
166 while (optind < argc)
167 scanelf(argv[optind++]);
168 }
169
170 int main(int argc, char **argv)
171 {
172 if (argc < 2) {
173 usage(argv);
174 exit(EXIT_FAILURE);
175 }
176 parseargs(argc, argv);
177 return 0;
178 }

  ViewVC Help
Powered by ViewVC 1.1.20