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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations) (download) (as text)
Tue Oct 19 16:54:48 2004 UTC (10 years ago) by solar
Branch: MAIN
Changes since 1.6: +6 -3 lines
File MIME type: text/x-csrc
use lstat()/S_ISREG to avoid reporting dups for symlinks. (speeds things up quite a bit)

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.6 2004/01/10 08:20:07 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.6 2004/01/10 08:20:07 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 struct stat st;
72 char *p;
73 int len = 0;
74
75 if ((chdir(path) == 0) && ((dir = opendir(path)))) {
76 while ((dentry = readdir(dir))) {
77 len = (strlen(path) + 2 + strlen(dentry->d_name));
78 p = malloc(len);
79 strncpy(p, path, len);
80 strncat(p, "/", len);
81 strncat(p, dentry->d_name, len);
82 if (lstat(p, &st) != -1)
83 if (S_ISREG(st.st_mode))
84 scanelf_file(p);
85 free(p);
86 }
87 closedir(dir);
88 }
89 }
90
91
92 /* display usage and exit */
93 void usage(char **argv)
94 {
95 fprintf(stderr,
96 "Usage: %s [options] dir1 dir2 dirN...\n",
97 (*argv != NULL) ? argv[0] : __FILE__ "\b\b");
98 }
99
100
101 void showopt(int c, char *data)
102 {
103 int i;
104 for (i = 0; long_options[i].name; i++)
105 if (long_options[i].val == c)
106 fprintf(stderr, " -%c, --%s\t: %s\n", c, long_options[i].name,
107 data);
108 }
109
110 /* parse command line arguments and preform needed actions */
111 void parseargs(int argc, char **argv)
112 {
113 int flag;
114 char *p, *path;
115 FILE *fp;
116
117 opterr = 0;
118 while ((flag =
119 (int) getopt_long(argc, argv,
120 PARSE_FLAGS, long_options, NULL)) != EOF) {
121 switch (flag) {
122 case 'h':
123 usage(argv);
124 showopt('p', "Scan all directories in PATH environment.");
125 showopt('l', "Scan all directories in /etc/ld.so.conf");
126 showopt('h', "Print this help and exit.");
127 showopt('v', "Print version and exit.");
128 exit(EXIT_SUCCESS);
129 case 'v':
130 fprintf(stderr, "%s compiled %s\n", __FILE__, __DATE__);
131 fprintf(stderr,
132 "%s written for Gentoo Linux <solar@gentoo.org>\n\t%s\n",
133 (*argv != NULL) ? argv[0] : __FILE__ "\b\b", rcsid);
134 exit(EXIT_SUCCESS);
135 case 'l':
136 /* scan ld.so.conf for ldpath */
137 if ((fp = fopen("/etc/ld.so.conf", "r")) != NULL) {
138 path = malloc(_POSIX_PATH_MAX);
139 while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL) {
140 if (*path == '/') {
141 if ((p = strrchr(path, '\r')) != NULL)
142 *p = 0;
143 if ((p = strrchr(path, '\n')) != NULL)
144 *p = 0;
145 scanelf(path);
146 }
147 }
148 free(path);
149 }
150 break;
151 case 'p':
152 if ((path = strdup(getenv("PATH"))) == NULL) {
153 perror("strdup");
154 exit(EXIT_FAILURE);
155 }
156 /* split string into dirs */
157 while ((p = strrchr(path, ':')) != NULL) {
158 scanelf(p + 1);
159 *p = 0;
160 }
161 if (path != NULL)
162 free(path);
163 break;
164 case '?':
165 default:
166 break;
167 }
168 }
169 while (optind < argc)
170 scanelf(argv[optind++]);
171 }
172
173 int main(int argc, char **argv)
174 {
175 if (argc < 2) {
176 usage(argv);
177 exit(EXIT_FAILURE);
178 }
179 parseargs(argc, argv);
180 return 0;
181 }

  ViewVC Help
Powered by ViewVC 1.1.20