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

Contents of /pax-utils/scanmacho.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations) (download) (as text)
Tue Dec 30 12:34:28 2008 UTC (6 years, 1 month ago) by vapier
Branch: MAIN
Changes since 1.7: +2 -4 lines
File MIME type: text/x-csrc
touchup whitespace style

1 grobian 1.1 /*
2     * Copyright 2008 Gentoo Foundation
3     * Distributed under the terms of the GNU General Public License v2
4 vapier 1.8 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanmacho.c,v 1.7 2008/12/30 12:02:29 vapier Exp $
5 grobian 1.1 *
6     * based on scanelf by:
7     * Copyright 2003-2007 Ned Ludd - <solar@gentoo.org>
8     * Copyright 2004-2007 Mike Frysinger - <vapier@gentoo.org>
9     * for Darwin specific fun:
10     * 2008 Fabian Groffen - <grobian@gentoo.org>
11     */
12    
13 vapier 1.8 static const char *rcsid = "$Id: scanmacho.c,v 1.7 2008/12/30 12:02:29 vapier Exp $";
14 grobian 1.1 const char * const argv0 = "scanmacho";
15    
16     #include "paxinc.h"
17    
18     #define IS_MODIFIER(c) (c == '%' || c == '#' || c == '+')
19    
20     /* prototypes */
21     static int scanmacho_fatobj(fatobj *fobj);
22     static int scanmacho_file(const char *filename, const struct stat *st_cache);
23     static int scanmacho_from_file(const char *filename);
24     static int scanmacho_dir(const char *path);
25     static void scanelf_envpath(void);
26     static void usage(int status);
27     static int parseargs(int argc, char *argv[]);
28    
29     /* variables to control behavior */
30     static char match_etypes[126] = "";
31     static char scan_envpath = 0;
32     static char scan_symlink = 1;
33     static char scan_archives = 0;
34     static char dir_recurse = 0;
35     static char dir_crossmount = 1;
36     static char show_perms = 0;
37     static char show_size = 0;
38     static char show_needed = 0;
39     static char show_interp = 0;
40     static char show_bind = 0;
41     static char show_soname = 0;
42     static char show_banner = 1;
43     static char show_endian = 0;
44     static char be_quiet = 0;
45     static char be_verbose = 0;
46     static char be_wewy_wewy_quiet = 0;
47     static char be_semi_verbose = 0;
48     static char *find_lib = NULL;
49     static char *out_format = NULL;
50     static char *search_path = NULL;
51     static char g_match = 0;
52    
53     int match_bits = 0;
54     unsigned int match_perms = 0;
55     caddr_t ldcache = 0;
56     size_t ldcache_size = 0;
57     unsigned long setpax = 0UL;
58    
59     int has_objdump = 0;
60    
61     static char *getstr_perms(const char *fname);
62     static char *getstr_perms(const char *fname)
63     {
64     struct stat st;
65     static char buf[8];
66    
67     if ((stat(fname, &st)) == (-1))
68     return (char *) "";
69    
70     snprintf(buf, sizeof(buf), "%o", st.st_mode);
71    
72     return (char *) buf + 2;
73     }
74    
75     static const char *macho_file_needed_lib(
76     fatobj *fobj,
77     char *found_needed,
78     char *found_lib,
79     int op,
80     char **ret,
81     size_t *ret_len
82     ) {
83     char *needed;
84     loadcmd *lcmd;
85     struct dylib_command *dlcmd;
86     uint32_t lc_load_dylib = LC_LOAD_DYLIB;
87    
88     if ((op == 0 && !show_needed) || (op == 1 && !find_lib))
89     return NULL;
90    
91     lcmd = firstloadcmd(fobj);
92    
93     if (fobj->swapped)
94     lc_load_dylib = bswap_32(lc_load_dylib);
95    
96     do {
97     if (lcmd->lcmd->cmd == lc_load_dylib) {
98     dlcmd = (struct dylib_command*)lcmd->data;
99     if (fobj->swapped) {
100     needed = (char *)(lcmd->data +
101     bswap_32(dlcmd->dylib.name.offset));
102     } else {
103     needed = (char *)(lcmd->data + dlcmd->dylib.name.offset);
104     }
105     if (op == 0) {
106     if (!be_wewy_wewy_quiet) {
107     if (*found_needed)
108     xchrcat(ret, ',', ret_len);
109     xstrcat(ret, needed, ret_len);
110     }
111     *found_needed = 1;
112     } else {
113     if (!strncmp(find_lib, needed,
114     strlen(!g_match ? needed : find_lib)))
115     {
116     *found_lib = 1;
117     free(lcmd);
118     return (be_wewy_wewy_quiet ? NULL : needed);
119     }
120     }
121     }
122     } while (nextloadcmd(lcmd));
123    
124     if (op == 0 && !*found_needed && be_verbose)
125     warn("Mach-O lacks LC_LOAD_DYLIB commands: %s", fobj->filename);
126    
127     return NULL;
128     }
129    
130     static char *macho_file_interp(fatobj *fobj, char *found_interp)
131     {
132     loadcmd *lcmd;
133     uint32_t lc_load_dylinker = LC_LOAD_DYLINKER;
134    
135     if (!show_interp)
136     return NULL;
137    
138     lcmd = firstloadcmd(fobj);
139    
140     if (fobj->swapped)
141     lc_load_dylinker = bswap_32(lc_load_dylinker);
142    
143     do {
144     if (lcmd->lcmd->cmd == lc_load_dylinker) {
145     struct dylinker_command *dlcmd =
146     (struct dylinker_command*)lcmd->data;
147     char *dylinker;
148     if (fobj->swapped) {
149     dylinker = (char *)(lcmd->data +
150     bswap_32(dlcmd->name.offset));
151     } else {
152     dylinker = (char *)(lcmd->data + dlcmd->name.offset);
153     }
154     *found_interp = 1;
155     free(lcmd);
156     return (be_wewy_wewy_quiet ? NULL : dylinker);
157     }
158     } while (nextloadcmd(lcmd));
159 vapier 1.7
160 grobian 1.1 return NULL;
161     }
162    
163     static char *macho_file_soname(fatobj *fobj, char *found_soname)
164     {
165     loadcmd *lcmd;
166     char *soname;
167     uint32_t lc_id_dylib = LC_ID_DYLIB;
168    
169     if (!show_soname)
170     return NULL;
171    
172     lcmd = firstloadcmd(fobj);
173    
174     if (fobj->swapped)
175     lc_id_dylib = bswap_32(lc_id_dylib);
176    
177     do {
178     if (lcmd->lcmd->cmd == lc_id_dylib) {
179     struct dylib_command *dlcmd = (struct dylib_command*)lcmd->data;
180     if (fobj->swapped) {
181     soname = (char *)(lcmd->data +
182     bswap_32(dlcmd->dylib.name.offset));
183     } else {
184     soname = (char *)(lcmd->data + dlcmd->dylib.name.offset);
185     }
186     *found_soname = 1;
187     free(lcmd);
188     return (be_wewy_wewy_quiet ? NULL : soname);
189     }
190     } while (nextloadcmd(lcmd));
191 vapier 1.7
192 grobian 1.1 return NULL;
193     }
194    
195     /* scan a macho file and show all the fun stuff */
196     #define prints(str) write(fileno(stdout), str, strlen(str))
197     static int scanmacho_fatobj(fatobj *fobj)
198     {
199     unsigned long i;
200     char found_needed, found_interp, found_soname, found_lib, found_file;
201     static char *out_buffer = NULL;
202     static size_t out_len;
203    
204     found_needed = found_interp = found_soname = \
205     found_lib = found_file = 0;
206    
207     if (be_verbose > 2)
208     printf("%s: scanning file {%s,%s}\n", fobj->filename,
209     get_machocputype(fobj),
210     get_machosubcputype(fobj));
211     else if (be_verbose > 1)
212     printf("%s: scanning file\n", fobj->filename);
213    
214     /* init output buffer */
215     if (!out_buffer) {
216     out_len = sizeof(char) * 80;
217     out_buffer = xmalloc(out_len);
218     }
219     *out_buffer = '\0';
220    
221     /* show the header */
222     if (!be_quiet && show_banner) {
223     for (i = 0; out_format[i]; ++i) {
224     if (!IS_MODIFIER(out_format[i])) continue;
225    
226     switch (out_format[++i]) {
227     case '+': break;
228     case '%': break;
229     case '#': break;
230     case 'F':
231     case 'p':
232     case 'f': prints("FILE "); found_file = 1; break;
233     case 'o': prints(" TYPE "); break;
234     case 'M': prints("CPU "); break;
235     case 'n': prints("NEEDED "); break;
236     case 'i': prints("DYLINKER "); break;
237     case 'b': prints("FLAGS "); break;
238     case 'Z': prints("SIZE "); break;
239     case 'S': prints("INSTALLNAME "); break;
240     case 'N': prints("LIB "); break;
241     case 'a': prints("ARCH "); break;
242     case 'O': prints("PERM "); break;
243     case 'D': prints("ENDIAN "); break;
244     default: warnf("'%c' has no title ?", out_format[i]);
245     }
246     }
247     if (!found_file) prints("FILE ");
248     prints("\n");
249     found_file = 0;
250     show_banner = 0;
251     }
252    
253     /* dump all the good stuff */
254     for (i = 0; out_format[i]; ++i) {
255     const char *out;
256     const char *tmp;
257     static char ubuf[sizeof(unsigned long)*2];
258     if (!IS_MODIFIER(out_format[i])) {
259     xchrcat(&out_buffer, out_format[i], &out_len);
260     continue;
261     }
262    
263     out = NULL;
264     be_wewy_wewy_quiet = (out_format[i] == '#');
265     be_semi_verbose = (out_format[i] == '+');
266     switch (out_format[++i]) {
267     case '+':
268     case '%':
269     case '#':
270     xchrcat(&out_buffer, out_format[i], &out_len); break;
271     case 'F':
272     found_file = 1;
273     if (be_wewy_wewy_quiet) break;
274     xstrcat(&out_buffer, fobj->filename, &out_len);
275     break;
276     case 'p':
277     found_file = 1;
278     if (be_wewy_wewy_quiet) break;
279     tmp = fobj->filename;
280     if (search_path) {
281     ssize_t len_search = strlen(search_path);
282     ssize_t len_file = strlen(fobj->filename);
283     if (!strncmp(fobj->filename, search_path, len_search) && \
284     len_file > len_search)
285     tmp += len_search;
286     if (*tmp == '/' && search_path[len_search-1] == '/') tmp++;
287     }
288     xstrcat(&out_buffer, tmp, &out_len);
289     break;
290     case 'f':
291     found_file = 1;
292     if (be_wewy_wewy_quiet) break;
293     xstrcat(&out_buffer, fobj->base_filename, &out_len);
294     break;
295     case 'o': out = get_machomhtype(fobj); break;
296     case 'M': out = get_machocputype(fobj); break;
297     case 'D': out = get_machoendian(fobj); break;
298     case 'O': out = getstr_perms(fobj->filename); break;
299     case 'n':
300     case 'N': out = macho_file_needed_lib(fobj, &found_needed, &found_lib, (out_format[i]=='N'), &out_buffer, &out_len); break;
301     case 'i': out = macho_file_interp(fobj, &found_interp); break;
302     case 'b': get_machomhflags(fobj, &out_buffer, &out_len); break;
303     case 'S': out = macho_file_soname(fobj, &found_soname); break;
304     case 'a': out = get_machomtype(fobj); break;
305     case 'Z': snprintf(ubuf, sizeof(ubuf), "%llu", (unsigned long long int)fobj->len); out = ubuf; break;;
306     default: warnf("'%c' has no scan code?", out_format[i]);
307     }
308     if (out) {
309     /* hack for comma delimited output like `scanelf -s sym1,sym2,sym3` */
310     if (out_format[i] == 's' && (tmp=strchr(out,',')) != NULL)
311     xstrncat(&out_buffer, out, &out_len, (tmp-out));
312     else
313     xstrcat(&out_buffer, out, &out_len);
314     }
315     }
316    
317     #define FOUND_SOMETHING() \
318     ( found_needed || found_interp || found_soname || found_lib )
319    
320     if (!found_file && (!be_quiet || (be_quiet && FOUND_SOMETHING()))) {
321     xchrcat(&out_buffer, ' ', &out_len);
322     xstrcat(&out_buffer, fobj->filename, &out_len);
323     }
324     if (!be_quiet || (be_quiet && FOUND_SOMETHING())) {
325     puts(out_buffer);
326     fflush(stdout);
327     }
328    
329     return 0;
330     }
331    
332 grobian 1.2 /* scan a single Mach-O */
333 grobian 1.1 static int scanmacho_fat(const char *filename, int fd, size_t len)
334     {
335     int ret = 1;
336     fatobj *fobj;
337     fatobj *walk;
338    
339     /* verify this is real Mach-O */
340     if ((fobj = readmacho_fd(filename, fd, len)) == NULL) {
341     if (be_verbose > 2) printf("%s: not a Mach-O object\n", filename);
342     return ret;
343     }
344     switch (match_bits) {
345     case 32:
346     case 64:
347     walk = fobj;
348     do {
349     if ((walk->ismach64 && match_bits == 64) ||
350     (!walk->ismach64 && match_bits == 32))
351     {
352     ret = scanmacho_fatobj(walk);
353     }
354     } while (walk->next != NULL && (walk = walk->next));
355     goto label_done;
356     default:
357     break;
358     }
359     if (strlen(match_etypes)) {
360 grobian 1.2 char sbuf[128];
361     char ftype[32];
362    
363     snprintf(sbuf, 128, ",%s,", match_etypes);
364    
365     walk = fobj;
366     do {
367     snprintf(ftype, 32, ",%s,", get_machomhtype(walk));
368     if (strstr(sbuf, ftype) != NULL) {
369     ret = scanmacho_fatobj(walk);
370 grobian 1.1 }
371 grobian 1.2 } while (walk->next != NULL && (walk = walk->next));
372     goto label_done;
373 grobian 1.1 }
374 grobian 1.2
375 grobian 1.1 walk = fobj;
376     do {
377     ret = scanmacho_fatobj(walk);
378     } while (walk->next != NULL && (walk = walk->next));
379    
380     label_done:
381     unreadmacho(fobj);
382     return ret;
383     }
384    
385     /* scan an archive of Mach-Os */
386     static int scanmacho_archive(const char *filename, int fd, size_t len)
387     {
388     archive_handle *ar;
389     archive_member *m;
390     char *ar_buffer;
391     fatobj *fobj;
392     fatobj *walk;
393    
394     ar = ar_open_fd(filename, fd);
395     if (ar == NULL)
396     return 1;
397    
398 vapier 1.6 ar_buffer = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
399 grobian 1.1 while ((m = ar_next(ar)) != NULL) {
400     fobj = readmacho_buffer(m->name, ar_buffer + lseek(fd, 0, SEEK_CUR), m->size);
401     if (fobj) {
402     walk = fobj;
403     do {
404     scanmacho_fatobj(walk);
405     } while (walk->next != NULL && (walk = walk->next));
406 grobian 1.5 fobj->data = NULL;
407 grobian 1.1 unreadmacho(fobj);
408     }
409     }
410     munmap(ar_buffer, len);
411    
412     return 0;
413     }
414     /* scan a file which may be an Mach-O or an archive or some other
415     * magical beast */
416     static int scanmacho_file(const char *filename, const struct stat *st_cache)
417     {
418     const struct stat *st = st_cache;
419     struct stat symlink_st;
420     int fd;
421    
422     /* always handle regular files and handle symlinked files if no -y */
423     if (S_ISLNK(st->st_mode)) {
424     if (!scan_symlink)
425     return 1;
426     stat(filename, &symlink_st);
427     st = &symlink_st;
428     }
429    
430     if (!S_ISREG(st->st_mode)) {
431     if (be_verbose > 2) printf("%s: skipping non-file\n", filename);
432     return 1;
433     }
434    
435     if (match_perms) {
436     if ((st->st_mode | match_perms) != st->st_mode)
437     return 1;
438     }
439     if ((fd = open(filename, O_RDONLY)) == -1)
440     return 1;
441    
442     if (scanmacho_fat(filename, fd, st->st_size) == 1 && scan_archives)
443     /* if it isn't an Mach-O, maybe it's an .a archive */
444     scanmacho_archive(filename, fd, st->st_size);
445    
446     close(fd);
447     return 0;
448     }
449    
450 grobian 1.4 /* scan a directory for Mach-O files and print when we find one */
451 grobian 1.1 static int scanmacho_dir(const char *path)
452     {
453     register DIR *dir;
454     register struct dirent *dentry;
455     struct stat st_top, st;
456     char buf[__PAX_UTILS_PATH_MAX];
457     size_t pathlen = 0, len = 0;
458     int ret = 0;
459    
460     /* make sure path exists */
461     if (lstat(path, &st_top) == -1) {
462     if (be_verbose > 2) printf("%s: does not exist\n", path);
463     return 1;
464     }
465    
466     /* ok, if it isn't a directory, assume we can open it */
467     if (!S_ISDIR(st_top.st_mode)) {
468     return scanmacho_file(path, &st_top);
469     }
470    
471     /* now scan the dir looking for fun stuff */
472     if ((dir = opendir(path)) == NULL) {
473     warnf("could not opendir %s: %s", path, strerror(errno));
474     return 1;
475     }
476     if (be_verbose > 1) printf("%s: scanning dir\n", path);
477    
478     pathlen = strlen(path);
479     while ((dentry = readdir(dir))) {
480     if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
481     continue;
482     len = (pathlen + 1 + strlen(dentry->d_name) + 1);
483     if (len >= sizeof(buf)) {
484     warnf("Skipping '%s': len > sizeof(buf); %lu > %lu\n", path,
485     (unsigned long)len, (unsigned long)sizeof(buf));
486     continue;
487     }
488     snprintf(buf, sizeof(buf), "%s%s%s", path, (path[pathlen-1] == '/') ? "" : "/", dentry->d_name);
489     if (lstat(buf, &st) != -1) {
490     if (S_ISREG(st.st_mode))
491     ret = scanmacho_file(buf, &st);
492     else if (dir_recurse && S_ISDIR(st.st_mode)) {
493     if (dir_crossmount || (st_top.st_dev == st.st_dev))
494     ret = scanmacho_dir(buf);
495     }
496     }
497     }
498     closedir(dir);
499     return ret;
500     }
501    
502     static int scanmacho_from_file(const char *filename)
503     {
504     FILE *fp = NULL;
505     char *p;
506     char path[__PAX_UTILS_PATH_MAX];
507     int ret = 0;
508    
509     if (strcmp(filename, "-") == 0)
510     fp = stdin;
511     else if ((fp = fopen(filename, "r")) == NULL)
512     return 1;
513    
514     while ((fgets(path, __PAX_UTILS_PATH_MAX, fp)) != NULL) {
515     if ((p = strchr(path, '\n')) != NULL)
516     *p = 0;
517     search_path = path;
518     ret = scanmacho_dir(path);
519     }
520     if (fp != stdin)
521     fclose(fp);
522     return ret;
523     }
524    
525     /* scan env PATH for paths */
526     static void scanelf_envpath(void)
527     {
528     char *path, *p;
529    
530     path = getenv("PATH");
531     if (!path)
532     err("PATH is not set in your env !");
533     path = xstrdup(path);
534    
535     while ((p = strrchr(path, ':')) != NULL) {
536     scanmacho_dir(p + 1);
537     *p = 0;
538     }
539    
540     free(path);
541     }
542    
543     /* usage / invocation handling functions */ /* Free Flags: c d e j k l r s t u w x z C G H I J K L P Q T U W X Y */
544     #define PARSE_FLAGS "pRmyAnibSN:gE:M:DO:ZaqvF:f:o:BhV"
545     #define a_argument required_argument
546     static struct option const long_opts[] = {
547     {"path", no_argument, NULL, 'p'},
548     {"recursive", no_argument, NULL, 'R'},
549     {"mount", no_argument, NULL, 'm'},
550     {"symlink", no_argument, NULL, 'y'},
551     {"archives", no_argument, NULL, 'A'},
552     {"needed", no_argument, NULL, 'n'},
553     {"interp", no_argument, NULL, 'i'},
554     {"bind", no_argument, NULL, 'b'},
555     {"soname", no_argument, NULL, 'S'},
556     {"lib", a_argument, NULL, 'N'},
557     {"gmatch", no_argument, NULL, 'g'},
558     {"etype", a_argument, NULL, 'E'},
559     {"bits", a_argument, NULL, 'M'},
560     {"endian", no_argument, NULL, 'D'},
561     {"perms", a_argument, NULL, 'O'},
562     {"size", no_argument, NULL, 'Z'},
563     {"all", no_argument, NULL, 'a'},
564     {"quiet", no_argument, NULL, 'q'},
565     {"verbose", no_argument, NULL, 'v'},
566     {"format", a_argument, NULL, 'F'},
567     {"from", a_argument, NULL, 'f'},
568     {"file", a_argument, NULL, 'o'},
569     {"nobanner", no_argument, NULL, 'B'},
570     {"help", no_argument, NULL, 'h'},
571     {"version", no_argument, NULL, 'V'},
572     {NULL, no_argument, NULL, 0x0}
573     };
574    
575     static const char *opts_help[] = {
576     "Scan all directories in PATH environment",
577     "Scan directories recursively",
578     "Don't recursively cross mount points",
579     "Don't scan symlinks",
580     "Scan archives (.a files)",
581     "Print LC_LOAD_DYLIB information (ELF: NEEDED)",
582     "Print LC_LOAD_DYLINKER information (ELF: INTERP)",
583     "Print flags from mach_header (ELF: BIND)",
584     "Print LC_ID_DYLIB information (ELF: SONAME)",
585     "Find a specified library",
586     "Use strncmp to match libraries. (use with -N)",
587     "Print only Mach-O files matching mach_header\n"
588     " MH_OBJECT,MH_EXECUTE ... (ELF: etype)",
589     "Print only Mach-O files matching numeric bits",
590     "Print Endianness",
591     "Print only Mach-O files matching octal permissions",
592     "Print Mach-O file size",
593     "Print all scanned info (-F\"%o %O %D %b %F\")\n",
594     "Only output 'bad' things",
595     "Be verbose (can be specified more than once)",
596     "Use specified format for output",
597     "Read input stream from a filename",
598     "Write output stream to a filename",
599     "Don't display the header",
600     "Print this help and exit",
601     "Print version and exit",
602     NULL
603     };
604    
605     /* display usage and exit */
606     static void usage(int status)
607     {
608     unsigned long i;
609     printf("* Scan Mach-O binaries for stuff\n\n"
610     "Usage: %s [options] <dir1/file1> [dir2 dirN file2 fileN ...]\n\n", argv0);
611     printf("Options: -[%s]\n", PARSE_FLAGS);
612     for (i = 0; long_opts[i].name; ++i)
613     if (long_opts[i].has_arg == no_argument)
614     printf(" -%c, --%-14s* %s\n", long_opts[i].val,
615     long_opts[i].name, opts_help[i]);
616     else
617     printf(" -%c, --%-7s <arg> * %s\n", long_opts[i].val,
618     long_opts[i].name, opts_help[i]);
619    
620     puts("\nFor more information, see the scanmacho(1) manpage");
621     exit(status);
622     }
623    
624     /* parse command line arguments and preform needed actions */
625     static int parseargs(int argc, char *argv[])
626     {
627     int i;
628     const char *from_file = NULL;
629     int ret = 0;
630    
631     opterr = 0;
632     while ((i = getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
633     switch (i) {
634    
635     case 'V':
636     printf("pax-utils-%s: %s compiled %s\n%s\n"
637     "%s written for Gentoo by <solar, vapier and grobian @ gentoo.org>\n",
638     VERSION, __FILE__, __DATE__, rcsid, argv0);
639     exit(EXIT_SUCCESS);
640     break;
641     case 'h': usage(EXIT_SUCCESS); break;
642     case 'f':
643     if (from_file) warn("You prob don't want to specify -f twice");
644     from_file = optarg;
645     break;
646     case 'E':
647     strncpy(match_etypes, optarg, sizeof(match_etypes));
648     break;
649     case 'M':
650     match_bits = atoi(optarg);
651     break;
652     case 'O':
653     if (sscanf(optarg, "%o", &match_perms) == (-1))
654     match_bits = 0;
655     break;
656     case 'o': {
657     if (freopen(optarg, "w", stdout) == NULL)
658     err("Could not open output stream '%s': %s", optarg, strerror(errno));
659     break;
660     }
661     case 'N': {
662     if (find_lib) warn("You prob don't want to specify -N twice");
663     find_lib = optarg;
664     break;
665     }
666     case 'F': {
667     if (out_format) warn("You prob don't want to specify -F twice");
668     out_format = optarg;
669     break;
670     }
671     case 'Z': show_size = 1; break;
672     case 'g': g_match = 1; break;
673     case 'y': scan_symlink = 0; break;
674     case 'A': scan_archives = 1; break;
675     case 'B': show_banner = 0; break;
676     case 'p': scan_envpath = 1; break;
677     case 'R': dir_recurse = 1; break;
678     case 'm': dir_crossmount = 0; break;
679     case 'n': show_needed = 1; break;
680     case 'i': show_interp = 1; break;
681     case 'b': show_bind = 1; break;
682     case 'S': show_soname = 1; break;
683     case 'q': be_quiet = 1; break;
684     case 'v': be_verbose = (be_verbose % 20) + 1; break;
685     case 'a': show_perms = show_endian = show_bind = 1; break;
686     case 'D': show_endian = 1; break;
687     case ':':
688     err("Option '%c' is missing parameter", optopt);
689     case '?':
690     err("Unknown option '%c' or argument missing", optopt);
691     default:
692     err("Unhandled option '%c'; please report this", i);
693     }
694     }
695     /* let the format option override all other options */
696     if (out_format) {
697     show_needed = show_interp = show_bind = show_soname = \
698     show_perms = show_endian = show_size = 0;
699     for (i = 0; out_format[i]; ++i) {
700     if (!IS_MODIFIER(out_format[i])) continue;
701    
702     switch (out_format[++i]) {
703     case '+': break;
704     case '%': break;
705     case '#': break;
706     case 'F': break;
707     case 'p': break;
708     case 'f': break;
709     case 'k': break;
710     case 'N': break;
711     case 'o': break;
712     case 'a': break;
713     case 'M': break;
714     case 'Z': show_size = 1; break;
715     case 'D': show_endian = 1; break;
716     case 'O': show_perms = 1; break;
717     case 'n': show_needed = 1; break;
718     case 'i': show_interp = 1; break;
719     case 'b': show_bind = 1; break;
720     case 'S': show_soname = 1; break;
721     default:
722     err("Invalid format specifier '%c' (byte %i)",
723     out_format[i], i+1);
724     }
725     }
726    
727     /* construct our default format */
728     } else {
729     size_t fmt_len = 30;
730     out_format = xmalloc(sizeof(char) * fmt_len);
731     *out_format = '\0';
732 grobian 1.3 if (!be_quiet) xstrcat(&out_format, "%a ", &fmt_len);
733 grobian 1.1 if (!be_quiet) xstrcat(&out_format, "%o ", &fmt_len);
734     if (show_perms) xstrcat(&out_format, "%O ", &fmt_len);
735     if (show_size) xstrcat(&out_format, "%Z ", &fmt_len);
736     if (show_endian) xstrcat(&out_format, "%D ", &fmt_len);
737     if (show_needed) xstrcat(&out_format, "%n ", &fmt_len);
738     if (show_interp) xstrcat(&out_format, "%i ", &fmt_len);
739     if (show_bind) xstrcat(&out_format, "%b ", &fmt_len);
740     if (show_soname) xstrcat(&out_format, "%S ", &fmt_len);
741     if (find_lib) xstrcat(&out_format, "%N ", &fmt_len);
742     if (!be_quiet) xstrcat(&out_format, "%F ", &fmt_len);
743     }
744     if (be_verbose > 2) printf("Format: %s\n", out_format);
745    
746     /* now lets actually do the scanning */
747     if (scan_envpath)
748     scanelf_envpath();
749     if (!from_file && optind == argc && ttyname(0) == NULL && !scan_envpath)
750     from_file = "-";
751     if (from_file) {
752     scanmacho_from_file(from_file);
753     from_file = *argv;
754     }
755     if (optind == argc && !scan_envpath && !from_file)
756     err("Nothing to scan !?");
757     while (optind < argc) {
758     search_path = argv[optind++];
759     ret = scanmacho_dir(search_path);
760     }
761    
762     return ret;
763     }
764    
765     int main(int argc, char *argv[])
766     {
767     int ret;
768     if (argc < 2)
769     usage(EXIT_FAILURE);
770     ret = parseargs(argc, argv);
771     fclose(stdout);
772     return ret;
773     }

  ViewVC Help
Powered by ViewVC 1.1.20