progname.c 3.1 KB

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