Blame gnulib/progname.c

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