-
+ C93FDF3DDE65200053B084BF2FE5963801DDA892F384A6737E9D898465DE9F7C964470D068088CE67C3FEAB269A6C4A5D346F05031464FF16E04C0BB163D3C7C
vtools/lib/progname.c
(0 . 0)(1 . 79)
2218
2219 /* Specification. */
2220 #include "progname.h"
2221
2222 #include <stdio.h>
2223 #include <stdlib.h>
2224 #include <string.h>
2225
2226
2227 /* String containing name the program is called with. To be
2228 initialized by |main()|. */
2229 const char *program_name = NULL;
2230
2231 /* Set |program_name|, based on |argv[0]|. |argv0| must be a string
2232 allocated with indefinite extent, and must not be modified after
2233 this call. */
2234 void
2235 set_program_name(const char *argv0) {
2236 /* libtool creates a temporary executable whose name is sometimes
2237 prefixed with "lt-" (depends on the platform). It also makes
2238 |argv[0]| absolute. But the name of the temporary executable
2239 is a detail that should not be visible to the end user and to
2240 the test suite. Remove this |"<dirname>/.libs/"| or
2241 |"<dirname>/.libs/lt-"| prefix here. */
2242 const char *slash;
2243 const char *base;
2244
2245 /* Sanity check. POSIX requires the invoking process to pass a
2246 non-|NULL| |argv[0]|. */
2247 if (argv0 == NULL) {
2248 /* It's a bug in the invoking program. Help diagnosing
2249 it. */
2250 fputs("A NULL argv[0] was passed through an exec system call.\n",
2251 stderr);
2252 abort();
2253 }
2254
2255 slash = strrchr(argv0, '/');
2256 base = (slash != NULL ? slash + 1 : argv0);
2257 if (base - argv0 >= 7 && strncmp(base - 7, "/.libs/", 7) == 0) {
2258 argv0 = base;
2259 if (strncmp(base, "lt-", 3) == 0) {
2260 argv0 = base + 3;
2261 /* On glibc systems, remove the "lt-" prefix from the
2262 variable |program_invocation_short_name|. */
2263 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
2264 program_invocation_short_name = (char *) argv0;
2265 #endif
2266 }
2267 }
2268
2269 /* But don't strip off a leading |<dirname>/| in general, because
2270 when the user runs
2271
2272 |/some/hidden/place/bin/cp foo foo|
2273
2274 he should get the error message
2275
2276 |/some/hidden/place/bin/cp: `foo' and `foo' are the same file|
2277
2278 not
2279
2280 | cp: `foo' and `foo' are the same file|
2281 */
2282
2283 program_name = argv0;
2284
2285 /* On glibc systems, the |error()| function comes from libc and
2286 uses the variable |program_invocation_name|, not
2287 |program_name|. So set this variable as well. */
2288 #if HAVE_DECL_PROGRAM_INVOCATION_NAME
2289 program_invocation_name = (char *) argv0;
2290 #endif
2291 }
2292
2293 const char *
2294 get_program_name(void) {
2295 return program_name;
2296 }