2N/A/* Program name management.
2N/A Copyright (C) 2001-2003, 2005-2010 Free Software Foundation, Inc.
2N/A Written by Bruno Haible <bruno@clisp.org>, 2001.
2N/A
2N/A This program is free software: you can redistribute it and/or modify
2N/A it under the terms of the GNU General Public License as published by
2N/A the Free Software Foundation; either version 3 of the License, or
2N/A (at your option) any later version.
2N/A
2N/A This program is distributed in the hope that it will be useful,
2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A GNU General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU General Public License
2N/A along with this program. If not, see <http://www.gnu.org/licenses/>. */
2N/A
2N/A
2N/A#include <config.h>
2N/A
2N/A/* Specification. */
2N/A#undef ENABLE_RELOCATABLE /* avoid defining set_program_name as a macro */
2N/A#include "progname.h"
2N/A
2N/A#include <errno.h> /* get program_invocation_name declaration */
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A
2N/A
2N/A/* String containing name the program is called with.
2N/A To be initialized by main(). */
2N/Aconst char *program_name = NULL;
2N/A
2N/A/* Set program_name, based on argv[0].
2N/A argv0 must be a string allocated with indefinite extent, and must not be
2N/A modified after this call. */
2N/Avoid
2N/Aset_program_name (const char *argv0)
2N/A{
2N/A /* libtool creates a temporary executable whose name is sometimes prefixed
2N/A with "lt-" (depends on the platform). It also makes argv[0] absolute.
2N/A But the name of the temporary executable is a detail that should not be
2N/A visible to the end user and to the test suite.
2N/A Remove this "<dirname>/.libs/" or "<dirname>/.libs/lt-" prefix here. */
2N/A const char *slash;
2N/A const char *base;
2N/A
2N/A /* Sanity check. POSIX requires the invoking process to pass a non-NULL
2N/A argv[0]. */
2N/A if (argv0 == NULL)
2N/A {
2N/A /* It's a bug in the invoking program. Help diagnosing it. */
2N/A fputs ("A NULL argv[0] was passed through an exec system call.\n",
2N/A stderr);
2N/A abort ();
2N/A }
2N/A
2N/A slash = strrchr (argv0, '/');
2N/A base = (slash != NULL ? slash + 1 : argv0);
2N/A if (base - argv0 >= 7 && strncmp (base - 7, "/.libs/", 7) == 0)
2N/A {
2N/A argv0 = base;
2N/A if (strncmp (base, "lt-", 3) == 0)
2N/A {
2N/A argv0 = base + 3;
2N/A /* On glibc systems, remove the "lt-" prefix from the variable
2N/A program_invocation_short_name. */
2N/A#if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
2N/A program_invocation_short_name = (char *) argv0;
2N/A#endif
2N/A }
2N/A }
2N/A
2N/A /* But don't strip off a leading <dirname>/ in general, because when the user
2N/A runs
2N/A /some/hidden/place/bin/cp foo foo
2N/A he should get the error message
2N/A /some/hidden/place/bin/cp: `foo' and `foo' are the same file
2N/A not
2N/A cp: `foo' and `foo' are the same file
2N/A */
2N/A
2N/A program_name = argv0;
2N/A
2N/A /* On glibc systems, the error() function comes from libc and uses the
2N/A variable program_invocation_name, not program_name. So set this variable
2N/A as well. */
2N/A#if HAVE_DECL_PROGRAM_INVOCATION_NAME
2N/A program_invocation_name = (char *) argv0;
2N/A#endif
2N/A}