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

Contents of /pax-utils/scanelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download) (as text)
Fri Oct 24 22:29:06 2003 UTC (10 years, 8 months ago) by solar
Branch: MAIN
Changes since 1.1: +3 -3 lines
File MIME type: text/x-csrc
change puts to scanelf

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.2 * $Header: /home/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.1 2003/10/24 22:19:20 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     * This program was written for the hcc suite by (solar|pappy)@g.o.
25     * visit http://www.gentoo.org/proj/en/hardened/etdyn-ssp.xml for more
26     * information on the Gentoo Hardened gcc suite
27     * Also of interest is the pax site http://pageexec.virtualave.net/
28     * but you should know about that already.
29     */
30    
31     #include <stdio.h>
32     #include <stdlib.h>
33     #include <sys/types.h>
34     #include <string.h>
35     #include <unistd.h>
36     #include <sys/stat.h>
37     #include <dirent.h>
38     #include <getopt.h>
39    
40     #include "paxelf.h"
41    
42 solar 1.2 static const char *rcsid = "$Id: scanelf.c,v 1.1 2003/10/24 22:19:20 solar Exp $";
43 solar 1.1
44     int display_pax_flags = 1;
45    
46     #define PARSE_FLAGS "hvlp"
47     static struct option const long_options[] = {
48     {"help", no_argument, 0, 'h'},
49     {"version", no_argument, 0, 'v'},
50     {"path", no_argument, 0, 'p'},
51     {"ldpath", no_argument, 0, 'l'},
52     {NULL, no_argument, NULL, 0}
53     };
54    
55     /* scan a directory for ET_EXEC files and print when we find one */
56     void scanelf(const char *path)
57     {
58     elfobj *elf = NULL;
59     register DIR *dir;
60     register struct dirent *dentry;
61    
62     if (chdir(path) == 0) {
63     if ((dir = opendir(path))) {
64     while ((dentry = readdir(dir))) {
65     /* verify this is real ELF */
66     if ((elf = readelf(dentry->d_name)) != NULL) {
67     if (!check_elf_header(elf->ehdr))
68     if (IS_ELF(elf))
69     printf("%s %s %s/%s\n",
70     pax_short_flags(PAX_FLAGS(elf)),
71     get_elfetype(elf->ehdr->e_type), path,
72     dentry->d_name);
73     if (elf != NULL) {
74     munmap(elf->data, elf->len);
75     free(elf);
76     elf = NULL;
77     }
78     }
79     }
80     closedir(dir);
81     }
82     }
83     }
84    
85    
86     /* display usage and exit */
87     int usage(char **argv)
88     {
89     printf
90     ("Usage: %s [options] dir1 dir2 dirN...\n",
91     (*argv != NULL) ? argv[0] : __FILE__ "\b\b");
92     exit(EXIT_FAILURE);
93     }
94    
95    
96     void showopt(int c, char *data)
97     {
98     int i;
99     for (i = 0; long_options[i].name; i++)
100     if (long_options[i].val == c)
101     printf(" -%c, --%s\t: %s\n", c, long_options[i].name, data);
102     }
103    
104     /* parse command line arguments and preform needed actions */
105     void parseargs(int argc, char **argv)
106     {
107     int flag;
108     char *p, *path;
109     FILE *fp;
110    
111     opterr = 0;
112     while ((flag =
113     (int) getopt_long(argc, argv,
114     PARSE_FLAGS, long_options, NULL)) != EOF) {
115     switch (flag) {
116     case 'h':
117     showopt('p', "Scan all directories in PATH environment.");
118     showopt('l', "Scan all directories in /etc/ld.so.conf");
119     showopt('h', "Print this help and exit.");
120     showopt('v', "Print version and exit.");
121     exit(EXIT_SUCCESS);
122     case 'v':
123     printf("%s compiled %s\n", __FILE__, __DATE__);
124     printf
125     ("%s written for Gentoo Linux <solar@gentoo.org>\n\t%s\n",
126     (*argv != NULL) ? argv[0] : __FILE__ "\b\b", rcsid);
127     exit(EXIT_SUCCESS);
128     case 'l':
129     /* scan ld.so.conf for ldpath */
130     if ((fp = fopen("/etc/ld.so.conf", "r")) != NULL) {
131     path = malloc(_POSIX_PATH_MAX);
132     while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL) {
133     if (*path == '/') {
134     if ((p = strrchr(path, '\r')) != NULL)
135     *p = 0;
136     if ((p = strrchr(path, '\n')) != NULL)
137     *p = 0;
138 solar 1.2 scanelf(path);
139 solar 1.1 }
140     }
141     free(path);
142     }
143     break;
144     case 'p':
145     if ((path = strdup(getenv("PATH"))) == NULL) {
146     perror("strdup");
147     exit(EXIT_FAILURE);
148     }
149     /* split string into dirs */
150     while ((p = strrchr(path, ':')) != NULL) {
151     scanelf(p + 1);
152     *p = 0;
153     }
154     if (path != NULL)
155     free(path);
156     break;
157     case '?':
158     default:
159     break;
160     }
161     }
162     while (optind < argc)
163     scanelf(argv[optind++]);
164     }
165    
166     int main(int argc, char **argv)
167     {
168     if (argc < 2)
169     usage(argv);
170     parseargs(argc, argv);
171     return 0;
172     }

  ViewVC Help
Powered by ViewVC 1.1.20