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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download) (as text)
Sun Oct 26 23:42:10 2003 UTC (11 years ago) by solar
Branch: MAIN
Changes since 1.2: +8 -17 lines
File MIME type: text/x-csrc
header cleanups, send all usage to stderr. add scanelf to README

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.3 * $Header: /home/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.2 2003/10/24 22:29:06 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.3 static const char *rcsid = "$Id: scanelf.c,v 1.2 2003/10/24 22:29:06 solar Exp $";
36 solar 1.1
37     #define PARSE_FLAGS "hvlp"
38     static struct option const long_options[] = {
39     {"help", no_argument, 0, 'h'},
40     {"version", no_argument, 0, 'v'},
41     {"path", no_argument, 0, 'p'},
42     {"ldpath", no_argument, 0, 'l'},
43     {NULL, no_argument, NULL, 0}
44     };
45    
46     /* scan a directory for ET_EXEC files and print when we find one */
47     void scanelf(const char *path)
48     {
49     elfobj *elf = NULL;
50     register DIR *dir;
51     register struct dirent *dentry;
52    
53     if (chdir(path) == 0) {
54     if ((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    
77     /* display usage and exit */
78     int usage(char **argv)
79     {
80 solar 1.3 fprintf(stderr,
81     "Usage: %s [options] dir1 dir2 dirN...\n",
82 solar 1.1 (*argv != NULL) ? argv[0] : __FILE__ "\b\b");
83     exit(EXIT_FAILURE);
84     }
85    
86    
87     void showopt(int c, char *data)
88     {
89     int i;
90     for (i = 0; long_options[i].name; i++)
91     if (long_options[i].val == c)
92 solar 1.3 fprintf(stderr, " -%c, --%s\t: %s\n", c, long_options[i].name, data);
93 solar 1.1 }
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 solar 1.3 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 solar 1.1 (*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 solar 1.2 scanelf(path);
130 solar 1.1 }
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