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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations) (download) (as text)
Tue Oct 19 16:54:48 2004 UTC (9 years, 10 months 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 solar 1.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 solar 1.7 * $Header: /home/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.6 2004/01/10 08:20:07 solar Exp $
6 solar 1.1 *
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 solar 1.4 static const char *rcsid =
36 solar 1.7 "$Id: scanelf.c,v 1.6 2004/01/10 08:20:07 solar Exp $";
37 solar 1.1
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 solar 1.6 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 solar 1.1 /* 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 solar 1.7 struct stat st;
72 solar 1.6 char *p;
73     int len = 0;
74 solar 1.1
75 solar 1.4 if ((chdir(path) == 0) && ((dir = opendir(path)))) {
76     while ((dentry = readdir(dir))) {
77 solar 1.6 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 solar 1.7 if (lstat(p, &st) != -1)
83     if (S_ISREG(st.st_mode))
84     scanelf_file(p);
85 solar 1.6 free(p);
86 solar 1.1 }
87 solar 1.4 closedir(dir);
88 solar 1.1 }
89     }
90    
91    
92     /* display usage and exit */
93 solar 1.5 void usage(char **argv)
94 solar 1.1 {
95 solar 1.3 fprintf(stderr,
96 solar 1.4 "Usage: %s [options] dir1 dir2 dirN...\n",
97     (*argv != NULL) ? argv[0] : __FILE__ "\b\b");
98 solar 1.1 }
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 solar 1.4 fprintf(stderr, " -%c, --%s\t: %s\n", c, long_options[i].name,
107     data);
108 solar 1.1 }
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 solar 1.5 usage(argv);
124 solar 1.1 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 solar 1.3 fprintf(stderr, "%s compiled %s\n", __FILE__, __DATE__);
131     fprintf(stderr,
132 solar 1.4 "%s written for Gentoo Linux <solar@gentoo.org>\n\t%s\n",
133     (*argv != NULL) ? argv[0] : __FILE__ "\b\b", rcsid);
134 solar 1.1 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 solar 1.2 scanelf(path);
146 solar 1.1 }
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 solar 1.5 if (argc < 2) {
176 solar 1.1 usage(argv);
177 solar 1.5 exit(EXIT_FAILURE);
178     }
179 solar 1.1 parseargs(argc, argv);
180     return 0;
181     }

  ViewVC Help
Powered by ViewVC 1.1.20