tuklib_progname.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file tuklib_progname.c
  4. /// \brief Program name to be displayed in messages
  5. //
  6. // Author: Lasse Collin
  7. //
  8. // This file has been put into the public domain.
  9. // You can do whatever you want with this file.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "tuklib_progname.h"
  13. #include <string.h>
  14. #if !HAVE_DECL_PROGRAM_INVOCATION_NAME
  15. char *progname = NULL;
  16. #endif
  17. extern void
  18. tuklib_progname_init(char **argv)
  19. {
  20. #ifdef TUKLIB_DOSLIKE
  21. // On these systems, argv[0] always has the full path and .exe
  22. // suffix even if the user just types the plain program name.
  23. // We modify argv[0] to make it nicer to read.
  24. // Strip the leading path.
  25. char *p = argv[0] + strlen(argv[0]);
  26. while (argv[0] < p && p[-1] != '/' && p[-1] != '\\')
  27. --p;
  28. argv[0] = p;
  29. // Strip the .exe suffix.
  30. p = strrchr(p, '.');
  31. if (p != NULL)
  32. *p = '\0';
  33. // Make it lowercase.
  34. for (p = argv[0]; *p != '\0'; ++p)
  35. if (*p >= 'A' && *p <= 'Z')
  36. *p = *p - 'A' + 'a';
  37. #endif
  38. progname = argv[0];
  39. return;
  40. }