]> git.cworth.org Git - tar/blob - gnu/progname.c
upstream: Fix extraction of device nodes.
[tar] / gnu / progname.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Program name management.
4    Copyright (C) 2001-2003, 2005-2010 Free Software Foundation, Inc.
5    Written by Bruno Haible <bruno@clisp.org>, 2001.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20
21 #include <config.h>
22
23 /* Specification.  */
24 #undef ENABLE_RELOCATABLE /* avoid defining set_program_name as a macro */
25 #include "progname.h"
26
27 #include <errno.h> /* get program_invocation_name declaration */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32
33 /* String containing name the program is called with.
34    To be initialized by main().  */
35 const char *program_name = NULL;
36
37 /* Set program_name, based on argv[0].
38    argv0 must be a string allocated with indefinite extent, and must not be
39    modified after this call.  */
40 void
41 set_program_name (const char *argv0)
42 {
43   /* libtool creates a temporary executable whose name is sometimes prefixed
44      with "lt-" (depends on the platform).  It also makes argv[0] absolute.
45      But the name of the temporary executable is a detail that should not be
46      visible to the end user and to the test suite.
47      Remove this "<dirname>/.libs/" or "<dirname>/.libs/lt-" prefix here.  */
48   const char *slash;
49   const char *base;
50
51   /* Sanity check.  POSIX requires the invoking process to pass a non-NULL
52      argv[0].  */
53   if (argv0 == NULL)
54     {
55       /* It's a bug in the invoking program.  Help diagnosing it.  */
56       fputs ("A NULL argv[0] was passed through an exec system call.\n",
57              stderr);
58       abort ();
59     }
60
61   slash = strrchr (argv0, '/');
62   base = (slash != NULL ? slash + 1 : argv0);
63   if (base - argv0 >= 7 && strncmp (base - 7, "/.libs/", 7) == 0)
64     {
65       argv0 = base;
66       if (strncmp (base, "lt-", 3) == 0)
67         {
68           argv0 = base + 3;
69           /* On glibc systems, remove the "lt-" prefix from the variable
70              program_invocation_short_name.  */
71 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
72           program_invocation_short_name = (char *) argv0;
73 #endif
74         }
75     }
76
77   /* But don't strip off a leading <dirname>/ in general, because when the user
78      runs
79          /some/hidden/place/bin/cp foo foo
80      he should get the error message
81          /some/hidden/place/bin/cp: `foo' and `foo' are the same file
82      not
83          cp: `foo' and `foo' are the same file
84    */
85
86   program_name = argv0;
87
88   /* On glibc systems, the error() function comes from libc and uses the
89      variable program_invocation_name, not program_name.  So set this variable
90      as well.  */
91 #if HAVE_DECL_PROGRAM_INVOCATION_NAME
92   program_invocation_name = (char *) argv0;
93 #endif
94 }