Blame lib/progname.c

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