progname.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Program name management.
  2. Copyright (C) 2001-2003, 2005-2009 Free Software Foundation, Inc.
  3. Written by Bruno Haible <haible@clisp.cons.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 <string.h>
  19. /* String containing name the program is called with.
  20. To be initialized by main(). */
  21. const char *program_name = NULL;
  22. /* Set program_name, based on argv[0]. */
  23. void
  24. set_program_name (const char *argv0)
  25. {
  26. /* libtool creates a temporary executable whose name is sometimes prefixed
  27. with "lt-" (depends on the platform). It also makes argv[0] absolute.
  28. But the name of the temporary executable is a detail that should not be
  29. visible to the end user and to the test suite.
  30. Remove this "<dirname>/.libs/" or "<dirname>/.libs/lt-" prefix here. */
  31. const char *slash;
  32. const char *base;
  33. slash = strrchr (argv0, '/');
  34. base = (slash != NULL ? slash + 1 : argv0);
  35. if (base - argv0 >= 7 && strncmp (base - 7, "/.libs/", 7) == 0)
  36. {
  37. argv0 = base;
  38. if (strncmp (base, "lt-", 3) == 0)
  39. argv0 = base + 3;
  40. }
  41. /* But don't strip off a leading <dirname>/ in general, because when the user
  42. runs
  43. /some/hidden/place/bin/cp foo foo
  44. he should get the error message
  45. /some/hidden/place/bin/cp: `foo' and `foo' are the same file
  46. not
  47. cp: `foo' and `foo' are the same file
  48. */
  49. program_name = argv0;
  50. }