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

Contents of /pax-utils/scanmacho.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download) (as text)
Mon Sep 8 10:16:31 2008 UTC (5 years, 10 months ago) by grobian
Branch: MAIN
Changes since 1.2: +3 -2 lines
File MIME type: text/x-csrc
Include arch by default, since it is important when displaying fat files

1 /*
2 * Copyright 2008 Gentoo Foundation
3 * Distributed under the terms of the GNU General Public License v2
4 * $Header: /var/cvsroot/gentoo-projects/pax-utils/scanmacho.c,v 1.2 2008/09/08 08:01:35 grobian Exp $
5 *
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 static const char *rcsid = "$Id: scanmacho.c,v 1.2 2008/09/08 08:01:35 grobian Exp $";
14 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
160 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
192 return NULL;
193 }
194
195
196 /* scan a macho file and show all the fun stuff */
197 #define prints(str) write(fileno(stdout), str, strlen(str))
198 static int scanmacho_fatobj(fatobj *fobj)
199 {
200 unsigned long i;
201 char found_needed, found_interp, found_soname, found_lib, found_file;
202 static char *out_buffer = NULL;
203 static size_t out_len;
204
205 found_needed = found_interp = found_soname = \
206 found_lib = found_file = 0;
207
208 if (be_verbose > 2)
209 printf("%s: scanning file {%s,%s}\n", fobj->filename,
210 get_machocputype(fobj),
211 get_machosubcputype(fobj));
212 else if (be_verbose > 1)
213 printf("%s: scanning file\n", fobj->filename);
214
215 /* init output buffer */
216 if (!out_buffer) {
217 out_len = sizeof(char) * 80;
218 out_buffer = xmalloc(out_len);
219 }
220 *out_buffer = '\0';
221
222 /* show the header */
223 if (!be_quiet && show_banner) {
224 for (i = 0; out_format[i]; ++i) {
225 if (!IS_MODIFIER(out_format[i])) continue;
226
227 switch (out_format[++i]) {
228 case '+': break;
229 case '%': break;
230 case '#': break;
231 case 'F':
232 case 'p':
233 case 'f': prints("FILE "); found_file = 1; break;
234 case 'o': prints(" TYPE "); break;
235 case 'M': prints("CPU "); break;
236 case 'n': prints("NEEDED "); break;
237 case 'i': prints("DYLINKER "); break;
238 case 'b': prints("FLAGS "); break;
239 case 'Z': prints("SIZE "); break;
240 case 'S': prints("INSTALLNAME "); break;
241 case 'N': prints("LIB "); break;
242 case 'a': prints("ARCH "); break;
243 case 'O': prints("PERM "); break;
244 case 'D': prints("ENDIAN "); break;
245 default: warnf("'%c' has no title ?", out_format[i]);
246 }
247 }
248 if (!found_file) prints("FILE ");
249 prints("\n");
250 found_file = 0;
251 show_banner = 0;
252 }
253
254 /* dump all the good stuff */
255 for (i = 0; out_format[i]; ++i) {
256 const char *out;
257 const char *tmp;
258 static char ubuf[sizeof(unsigned long)*2];
259 if (!IS_MODIFIER(out_format[i])) {
260 xchrcat(&out_buffer, out_format[i], &out_len);
261 continue;
262 }
263
264 out = NULL;
265 be_wewy_wewy_quiet = (out_format[i] == '#');
266 be_semi_verbose = (out_format[i] == '+');
267 switch (out_format[++i]) {
268 case '+':
269 case '%':
270 case '#':
271 xchrcat(&out_buffer, out_format[i], &out_len); break;
272 case 'F':
273 found_file = 1;
274 if (be_wewy_wewy_quiet) break;
275 xstrcat(&out_buffer, fobj->filename, &out_len);
276 break;
277 case 'p':
278 found_file = 1;
279 if (be_wewy_wewy_quiet) break;
280 tmp = fobj->filename;
281 if (search_path) {
282 ssize_t len_search = strlen(search_path);
283 ssize_t len_file = strlen(fobj->filename);
284 if (!strncmp(fobj->filename, search_path, len_search) && \
285 len_file > len_search)
286 tmp += len_search;
287 if (*tmp == '/' && search_path[len_search-1] == '/') tmp++;
288 }
289 xstrcat(&out_buffer, tmp, &out_len);
290 break;
291 case 'f':
292 found_file = 1;
293 if (be_wewy_wewy_quiet) break;
294 xstrcat(&out_buffer, fobj->base_filename, &out_len);
295 break;
296 case 'o': out = get_machomhtype(fobj); break;
297 case 'M': out = get_machocputype(fobj); break;
298 case 'D': out = get_machoendian(fobj); break;
299 case 'O': out = getstr_perms(fobj->filename); break;
300 case 'n':
301 case 'N': out = macho_file_needed_lib(fobj, &found_needed, &found_lib, (out_format[i]=='N'), &out_buffer, &out_len); break;
302 case 'i': out = macho_file_interp(fobj, &found_interp); break;
303 case 'b': get_machomhflags(fobj, &out_buffer, &out_len); break;
304 case 'S': out = macho_file_soname(fobj, &found_soname); break;
305 case 'a': out = get_machomtype(fobj); break;
306 case 'Z': snprintf(ubuf, sizeof(ubuf), "%llu", (unsigned long long int)fobj->len); out = ubuf; break;;
307 default: warnf("'%c' has no scan code?", out_format[i]);
308 }
309 if (out) {
310 /* hack for comma delimited output like `scanelf -s sym1,sym2,sym3` */
311 if (out_format[i] == 's' && (tmp=strchr(out,',')) != NULL)
312 xstrncat(&out_buffer, out, &out_len, (tmp-out));
313 else
314 xstrcat(&out_buffer, out, &out_len);
315 }
316 }
317
318 #define FOUND_SOMETHING() \
319 ( found_needed || found_interp || found_soname || found_lib )
320
321 if (!found_file && (!be_quiet || (be_quiet && FOUND_SOMETHING()))) {
322 xchrcat(&out_buffer, ' ', &out_len);
323 xstrcat(&out_buffer, fobj->filename, &out_len);
324 }
325 if (!be_quiet || (be_quiet && FOUND_SOMETHING())) {
326 puts(out_buffer);
327 fflush(stdout);
328 }
329
330 return 0;
331 }
332
333 /* scan a single Mach-O */
334 static int scanmacho_fat(const char *filename, int fd, size_t len)
335 {
336 int ret = 1;
337 fatobj *fobj;
338 fatobj *walk;
339
340 /* verify this is real Mach-O */
341 if ((fobj = readmacho_fd(filename, fd, len)) == NULL) {
342 if (be_verbose > 2) printf("%s: not a Mach-O object\n", filename);
343 return ret;
344 }
345 switch (match_bits) {
346 case 32:
347 case 64:
348 walk = fobj;
349 do {
350 if ((walk->ismach64 && match_bits == 64) ||
351 (!walk->ismach64 && match_bits == 32))
352 {
353 ret = scanmacho_fatobj(walk);
354 }
355 } while (walk->next != NULL && (walk = walk->next));
356 goto label_done;
357 default:
358 break;
359 }
360 if (strlen(match_etypes)) {
361 char sbuf[128];
362 char ftype[32];
363
364 snprintf(sbuf, 128, ",%s,", match_etypes);
365
366 walk = fobj;
367 do {
368 snprintf(ftype, 32, ",%s,", get_machomhtype(walk));
369 if (strstr(sbuf, ftype) != NULL) {
370 ret = scanmacho_fatobj(walk);
371 }
372 } while (walk->next != NULL && (walk = walk->next));
373 goto label_done;
374 }
375
376 walk = fobj;
377 do {
378 ret = scanmacho_fatobj(walk);
379 } while (walk->next != NULL && (walk = walk->next));
380
381 label_done:
382 unreadmacho(fobj);
383 return ret;
384 }
385
386 /* scan an archive of Mach-Os */
387 static int scanmacho_archive(const char *filename, int fd, size_t len)
388 {
389 archive_handle *ar;
390 archive_member *m;
391 char *ar_buffer;
392 fatobj *fobj;
393 fatobj *walk;
394
395 ar = ar_open_fd(filename, fd);
396 if (ar == NULL)
397 return 1;
398
399 ar_buffer = (char*)mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
400 while ((m = ar_next(ar)) != NULL) {
401 fobj = readmacho_buffer(m->name, ar_buffer + lseek(fd, 0, SEEK_CUR), m->size);
402 if (fobj) {
403 walk = fobj;
404 do {
405 scanmacho_fatobj(walk);
406 } while (walk->next != NULL && (walk = walk->next));
407 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 /* scan a directory for ET_EXEC files and print when we find one */
451 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
526 /* scan env PATH for paths */
527 static void scanelf_envpath(void)
528 {
529 char *path, *p;
530
531 path = getenv("PATH");
532 if (!path)
533 err("PATH is not set in your env !");
534 path = xstrdup(path);
535
536 while ((p = strrchr(path, ':')) != NULL) {
537 scanmacho_dir(p + 1);
538 *p = 0;
539 }
540
541 free(path);
542 }
543
544 /* 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 */
545 #define PARSE_FLAGS "pRmyAnibSN:gE:M:DO:ZaqvF:f:o:BhV"
546 #define a_argument required_argument
547 static struct option const long_opts[] = {
548 {"path", no_argument, NULL, 'p'},
549 {"recursive", no_argument, NULL, 'R'},
550 {"mount", no_argument, NULL, 'm'},
551 {"symlink", no_argument, NULL, 'y'},
552 {"archives", no_argument, NULL, 'A'},
553 {"needed", no_argument, NULL, 'n'},
554 {"interp", no_argument, NULL, 'i'},
555 {"bind", no_argument, NULL, 'b'},
556 {"soname", no_argument, NULL, 'S'},
557 {"lib", a_argument, NULL, 'N'},
558 {"gmatch", no_argument, NULL, 'g'},
559 {"etype", a_argument, NULL, 'E'},
560 {"bits", a_argument, NULL, 'M'},
561 {"endian", no_argument, NULL, 'D'},
562 {"perms", a_argument, NULL, 'O'},
563 {"size", no_argument, NULL, 'Z'},
564 {"all", no_argument, NULL, 'a'},
565 {"quiet", no_argument, NULL, 'q'},
566 {"verbose", no_argument, NULL, 'v'},
567 {"format", a_argument, NULL, 'F'},
568 {"from", a_argument, NULL, 'f'},
569 {"file", a_argument, NULL, 'o'},
570 {"nobanner", no_argument, NULL, 'B'},
571 {"help", no_argument, NULL, 'h'},
572 {"version", no_argument, NULL, 'V'},
573 {NULL, no_argument, NULL, 0x0}
574 };
575
576 static const char *opts_help[] = {
577 "Scan all directories in PATH environment",
578 "Scan directories recursively",
579 "Don't recursively cross mount points",
580 "Don't scan symlinks",
581 "Scan archives (.a files)",
582 "Print LC_LOAD_DYLIB information (ELF: NEEDED)",
583 "Print LC_LOAD_DYLINKER information (ELF: INTERP)",
584 "Print flags from mach_header (ELF: BIND)",
585 "Print LC_ID_DYLIB information (ELF: SONAME)",
586 "Find a specified library",
587 "Use strncmp to match libraries. (use with -N)",
588 "Print only Mach-O files matching mach_header\n"
589 " MH_OBJECT,MH_EXECUTE ... (ELF: etype)",
590 "Print only Mach-O files matching numeric bits",
591 "Print Endianness",
592 "Print only Mach-O files matching octal permissions",
593 "Print Mach-O file size",
594 "Print all scanned info (-F\"%o %O %D %b %F\")\n",
595 "Only output 'bad' things",
596 "Be verbose (can be specified more than once)",
597 "Use specified format for output",
598 "Read input stream from a filename",
599 "Write output stream to a filename",
600 "Don't display the header",
601 "Print this help and exit",
602 "Print version and exit",
603 NULL
604 };
605
606 /* display usage and exit */
607 static void usage(int status)
608 {
609 unsigned long i;
610 printf("* Scan Mach-O binaries for stuff\n\n"
611 "Usage: %s [options] <dir1/file1> [dir2 dirN file2 fileN ...]\n\n", argv0);
612 printf("Options: -[%s]\n", PARSE_FLAGS);
613 for (i = 0; long_opts[i].name; ++i)
614 if (long_opts[i].has_arg == no_argument)
615 printf(" -%c, --%-14s* %s\n", long_opts[i].val,
616 long_opts[i].name, opts_help[i]);
617 else
618 printf(" -%c, --%-7s <arg> * %s\n", long_opts[i].val,
619 long_opts[i].name, opts_help[i]);
620
621 puts("\nFor more information, see the scanmacho(1) manpage");
622 exit(status);
623 }
624
625 /* parse command line arguments and preform needed actions */
626 static int parseargs(int argc, char *argv[])
627 {
628 int i;
629 const char *from_file = NULL;
630 int ret = 0;
631
632 opterr = 0;
633 while ((i = getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
634 switch (i) {
635
636 case 'V':
637 printf("pax-utils-%s: %s compiled %s\n%s\n"
638 "%s written for Gentoo by <solar, vapier and grobian @ gentoo.org>\n",
639 VERSION, __FILE__, __DATE__, rcsid, argv0);
640 exit(EXIT_SUCCESS);
641 break;
642 case 'h': usage(EXIT_SUCCESS); break;
643 case 'f':
644 if (from_file) warn("You prob don't want to specify -f twice");
645 from_file = optarg;
646 break;
647 case 'E':
648 strncpy(match_etypes, optarg, sizeof(match_etypes));
649 break;
650 case 'M':
651 match_bits = atoi(optarg);
652 break;
653 case 'O':
654 if (sscanf(optarg, "%o", &match_perms) == (-1))
655 match_bits = 0;
656 break;
657 case 'o': {
658 if (freopen(optarg, "w", stdout) == NULL)
659 err("Could not open output stream '%s': %s", optarg, strerror(errno));
660 break;
661 }
662 case 'N': {
663 if (find_lib) warn("You prob don't want to specify -N twice");
664 find_lib = optarg;
665 break;
666 }
667 case 'F': {
668 if (out_format) warn("You prob don't want to specify -F twice");
669 out_format = optarg;
670 break;
671 }
672 case 'Z': show_size = 1; break;
673 case 'g': g_match = 1; break;
674 case 'y': scan_symlink = 0; break;
675 case 'A': scan_archives = 1; break;
676 case 'B': show_banner = 0; break;
677 case 'p': scan_envpath = 1; break;
678 case 'R': dir_recurse = 1; break;
679 case 'm': dir_crossmount = 0; break;
680 case 'n': show_needed = 1; break;
681 case 'i': show_interp = 1; break;
682 case 'b': show_bind = 1; break;
683 case 'S': show_soname = 1; break;
684 case 'q': be_quiet = 1; break;
685 case 'v': be_verbose = (be_verbose % 20) + 1; break;
686 case 'a': show_perms = show_endian = show_bind = 1; break;
687 case 'D': show_endian = 1; break;
688 case ':':
689 err("Option '%c' is missing parameter", optopt);
690 case '?':
691 err("Unknown option '%c' or argument missing", optopt);
692 default:
693 err("Unhandled option '%c'; please report this", i);
694 }
695 }
696 /* let the format option override all other options */
697 if (out_format) {
698 show_needed = show_interp = show_bind = show_soname = \
699 show_perms = show_endian = show_size = 0;
700 for (i = 0; out_format[i]; ++i) {
701 if (!IS_MODIFIER(out_format[i])) continue;
702
703 switch (out_format[++i]) {
704 case '+': break;
705 case '%': break;
706 case '#': break;
707 case 'F': break;
708 case 'p': break;
709 case 'f': break;
710 case 'k': break;
711 case 'N': break;
712 case 'o': break;
713 case 'a': break;
714 case 'M': break;
715 case 'Z': show_size = 1; break;
716 case 'D': show_endian = 1; break;
717 case 'O': show_perms = 1; break;
718 case 'n': show_needed = 1; break;
719 case 'i': show_interp = 1; break;
720 case 'b': show_bind = 1; break;
721 case 'S': show_soname = 1; break;
722 default:
723 err("Invalid format specifier '%c' (byte %i)",
724 out_format[i], i+1);
725 }
726 }
727
728 /* construct our default format */
729 } else {
730 size_t fmt_len = 30;
731 out_format = xmalloc(sizeof(char) * fmt_len);
732 *out_format = '\0';
733 if (!be_quiet) xstrcat(&out_format, "%a ", &fmt_len);
734 if (!be_quiet) xstrcat(&out_format, "%o ", &fmt_len);
735 if (show_perms) xstrcat(&out_format, "%O ", &fmt_len);
736 if (show_size) xstrcat(&out_format, "%Z ", &fmt_len);
737 if (show_endian) xstrcat(&out_format, "%D ", &fmt_len);
738 if (show_needed) xstrcat(&out_format, "%n ", &fmt_len);
739 if (show_interp) xstrcat(&out_format, "%i ", &fmt_len);
740 if (show_bind) xstrcat(&out_format, "%b ", &fmt_len);
741 if (show_soname) xstrcat(&out_format, "%S ", &fmt_len);
742 if (find_lib) xstrcat(&out_format, "%N ", &fmt_len);
743 if (!be_quiet) xstrcat(&out_format, "%F ", &fmt_len);
744 }
745 if (be_verbose > 2) printf("Format: %s\n", out_format);
746
747 /* now lets actually do the scanning */
748 if (scan_envpath)
749 scanelf_envpath();
750 if (!from_file && optind == argc && ttyname(0) == NULL && !scan_envpath)
751 from_file = "-";
752 if (from_file) {
753 scanmacho_from_file(from_file);
754 from_file = *argv;
755 }
756 if (optind == argc && !scan_envpath && !from_file)
757 err("Nothing to scan !?");
758 while (optind < argc) {
759 search_path = argv[optind++];
760 ret = scanmacho_dir(search_path);
761 }
762
763 return ret;
764 }
765
766 int main(int argc, char *argv[])
767 {
768 int ret;
769 if (argc < 2)
770 usage(EXIT_FAILURE);
771 ret = parseargs(argc, argv);
772 fclose(stdout);
773 return ret;
774 }

  ViewVC Help
Powered by ViewVC 1.1.20