Blame src/gl/progname.c

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