Mercurial > hg > chrpath
annotate main.c @ 2:7bf4a164d5bb default tip
fix bug: long_options have to be zero terminated
author | Peter Meerwald <p.meerwald@bct-electronic.com> |
---|---|
date | Fri, 20 Jul 2012 11:28:30 +0200 |
parents | b8f7423e385c |
children |
rev | line source |
---|---|
0 | 1 /* |
2 * Author: Petter Reinholdtsen <pere@hungry.com> | |
3 * date: 2001-01-20 | |
4 * | |
5 * Alter ELF rpath information (insert, modify, remove). | |
6 * | |
7 * Based on source from Peeter Joot <peeterj@ca.ibm.com> and Geoffrey | |
8 * Keating <geoffk@ozemail.com.au>. | |
9 */ | |
10 | |
11 #ifdef HAVE_CONFIG_H | |
12 # include "config.h" | |
13 #endif | |
14 | |
15 #include <stdio.h> | |
16 #include <stdlib.h> | |
17 #include <unistd.h> | |
18 #ifdef HAVE_GETOPT_H | |
19 #include <getopt.h> | |
20 #endif | |
21 #include "protos.h" | |
22 | |
23 #ifdef HAVE_GETOPT_LONG | |
24 # define GETOPT_LONG getopt_long | |
25 | |
26 static struct option long_options[] = | |
27 { | |
28 {"convert", 0, 0, 'c'}, | |
29 {"delete", 0, 0, 'd'}, | |
30 {"help", 0, 0, 'h'}, | |
31 {"keepgoing", 0, 0, 'k'}, | |
32 {"list", 0, 0, 'l'}, | |
33 {"replace", 1, 0, 'r'}, | |
2
7bf4a164d5bb
fix bug: long_options have to be zero terminated
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
0
diff
changeset
|
34 {"version", 0, 0, 'v'}, |
7bf4a164d5bb
fix bug: long_options have to be zero terminated
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
0
diff
changeset
|
35 {0, 0, 0, 0} |
0 | 36 }; |
37 | |
38 #else /* not HAVE_GETOPT_LONG */ | |
39 # define GETOPT_LONG(argc,argv,optstr,lopts,lidx) getopt(argc,argv,optstr) | |
40 #endif /* not HAVE_GETOPT_LONG */ | |
41 | |
42 static void | |
43 usage(char *progname) | |
44 { | |
45 printf("Usage: %s [-v|-d|-c|-r <path>] <program> [<program> ...]\n\n", | |
46 progname); | |
47 printf(" -v|--version Display program version number\n"); | |
48 printf(" -d|--delete Delete current rpath/runpath setting\n"); | |
49 #if defined(DT_RUNPATH) | |
50 printf(" -c|--convert Convert rpath to runpath\n"); | |
51 #endif /* DT_RUNPATH */ | |
52 printf(" -r <path>|--replace <path> Replace current rpath/runpath setting\n"); | |
53 printf(" with the path given\n"); | |
54 printf(" -l|--list List the current rpath/runpath (default)\n"); | |
55 printf(" -h|--help Show this usage information.\n"); | |
56 #ifndef HAVE_GETOPT_LONG | |
57 printf("\n *** The long options are not available on this platform"); | |
58 #endif /* not HAVE_GETOPT_LONG */ | |
59 #if !defined(DT_RUNPATH) | |
60 printf("\n *** There is no support for runpath on this platform"); | |
61 #endif /* DT_RUNPATH */ | |
62 printf("\n"); | |
63 } | |
64 | |
65 int | |
66 main(int argc, char * const argv[]) | |
67 { | |
68 int retval = 0; | |
69 int convert = 0; /* convert to given type */ | |
70 int remove = 0; /* remove or not */ | |
71 int keep_going = 0; /* Break on first error, or keep going? */ | |
72 char *newpath = NULL; /* insert this path */ | |
73 int opt; | |
74 #ifdef HAVE_GETOPT_LONG | |
75 int option_index = 0; | |
76 #endif /* HAVE_GETOPT_LONG */ | |
77 | |
78 if (argc < 2) | |
79 { | |
80 usage(argv[0]); | |
81 return 1; | |
82 } | |
83 | |
84 do { | |
85 opt = GETOPT_LONG(argc, argv, "cdhklr:v", long_options, &option_index); | |
86 switch (opt) | |
87 { | |
88 #if defined(DT_RUNPATH) | |
89 case 'c': | |
90 convert = 1; | |
91 break; | |
92 #endif /* DT_RUNPATH */ | |
93 case 'd': | |
94 remove = 1; | |
95 break; | |
96 case 'k': | |
97 keep_going = 1; | |
98 break; | |
99 case 'r': | |
100 newpath = optarg; | |
101 break; | |
102 case 'v': | |
103 printf("%s version %s\n", PACKAGE, VERSION); | |
104 exit(0); | |
105 break; | |
106 case 'l': /* This is the default action */ | |
107 newpath = NULL; | |
108 break; | |
109 case -1: | |
110 break; | |
111 default: | |
112 printf("Invalid argument '%c'\n", opt); | |
113 case 'h': | |
114 usage(argv[0]); | |
115 exit(0); | |
116 break; | |
117 } | |
118 } while (-1 != opt); | |
119 | |
120 while (optind < argc && (!retval || keep_going)) | |
121 { | |
122 if (remove) | |
123 retval |= killrpath(argv[optind++]); | |
124 else | |
125 /* list by default, replace if path is set */ | |
126 retval |= chrpath(argv[optind++], newpath, convert); | |
127 } | |
128 | |
129 return retval; | |
130 } |