getprogname.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Program name management.
  2. Copyright (C) 2016 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include "getprogname.h"
  16. #include <errno.h> /* get program_invocation_name declaration */
  17. #include <stdlib.h> /* get __argv declaration */
  18. #ifdef _AIX
  19. # include <unistd.h>
  20. # include <procinfo.h>
  21. # include <string.h>
  22. #endif
  23. #ifdef __MVS__
  24. # ifndef _OPEN_SYS
  25. # define _OPEN_SYS
  26. # endif
  27. # include <string.h>
  28. # error #include <sys/ps.h>
  29. #endif
  30. #ifdef __hpux
  31. # include <unistd.h>
  32. # include <sys/param.h>
  33. # include <sys/pstat.h>
  34. # include <string.h>
  35. #endif
  36. #include "dirname.h"
  37. #ifndef HAVE_GETPROGNAME /* not Mac OS X, FreeBSD, NetBSD, OpenBSD >= 5.4, Cygwin */
  38. char const *
  39. getprogname (void)
  40. {
  41. # if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME /* glibc, BeOS */
  42. /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
  43. return program_invocation_short_name;
  44. # elif HAVE_DECL_PROGRAM_INVOCATION_NAME /* glibc, BeOS */
  45. /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
  46. return last_component (program_invocation_name);
  47. # elif HAVE_GETEXECNAME /* Solaris */
  48. /* http://docs.oracle.com/cd/E19253-01/816-5168/6mbb3hrb1/index.html */
  49. const char *p = getexecname ();
  50. if (!p)
  51. p = "?";
  52. return last_component (p);
  53. # elif HAVE_DECL___ARGV /* mingw, MSVC */
  54. /* https://msdn.microsoft.com/en-us/library/dn727674.aspx */
  55. const char *p = __argv && __argv[0] ? __argv[0] : "?";
  56. return last_component (p);
  57. # elif HAVE_VAR___PROGNAME /* OpenBSD, QNX */
  58. /* http://man.openbsd.org/style.9 */
  59. /* http://www.qnx.de/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.neutrino_lib_ref%2Fp%2F__progname.html */
  60. /* Be careful to declare this only when we absolutely need it
  61. (OpenBSD 5.1), rather than when it's available. Otherwise,
  62. its mere declaration makes program_invocation_short_name
  63. malfunction (have zero length) with Fedora 25's glibc. */
  64. extern char *__progname;
  65. const char *p = __progname;
  66. return p && p[0] ? p : "?";
  67. # elif _AIX /* AIX */
  68. /* Idea by Bastien ROUCARIÈS,
  69. http://lists.gnu.org/archive/html/bug-gnulib/2010-12/msg00095.html
  70. Reference: http://
  71. ibm.biz/knowctr#ssw_aix_53/com.ibm.aix.basetechref/doc/basetrf1/getprocs.htm
  72. */
  73. static char *p;
  74. static int first = 1;
  75. if (first)
  76. {
  77. first = 0;
  78. pid_t pid = getpid ();
  79. struct procentry64 procs;
  80. p = (0 < getprocs64 (&procs, sizeof procs, NULL, 0, &pid, 1)
  81. ? strdup (procs.pi_comm)
  82. : NULL);
  83. if (!p)
  84. p = "?";
  85. }
  86. return p;
  87. # elif defined __hpux
  88. static char *p;
  89. static int first = 1;
  90. if (first)
  91. {
  92. first = 0;
  93. pid_t pid = getpid ();
  94. struct pst_status status;
  95. p = (0 < pstat_getproc (&status, sizeof status, 0, pid)
  96. ? strdup (status.pst_ucomm)
  97. : NULL);
  98. if (!p)
  99. p = "?";
  100. }
  101. return p;
  102. # elif __MVS__ /* z/OS */
  103. /* https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/rtwgetp.htm */
  104. static char *p = "?";
  105. static int first = 1;
  106. if (first)
  107. {
  108. pid_t pid = getpid ();
  109. int token;
  110. W_PSPROC buf;
  111. first = 0;
  112. memset (&buf, 0, sizeof(buf));
  113. buf.ps_cmdptr = (char *) malloc (buf.ps_cmdlen = PS_CMDBLEN_LONG);
  114. buf.ps_conttyptr = (char *) malloc (buf.ps_conttylen = PS_CONTTYBLEN);
  115. buf.ps_pathptr = (char *) malloc (buf.ps_pathlen = PS_PATHBLEN);
  116. if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr)
  117. {
  118. for (token = 0; token >= 0;
  119. token = w_getpsent (token, &buf, sizeof(buf)))
  120. {
  121. if (token > 0 && buf.ps_pid == pid)
  122. {
  123. char *s = strdup (last_component (buf.ps_pathptr));
  124. if (s)
  125. p = s;
  126. break;
  127. }
  128. }
  129. }
  130. free (buf.ps_cmdptr);
  131. free (buf.ps_conttyptr);
  132. free (buf.ps_pathptr);
  133. }
  134. return p;
  135. # else
  136. # error "getprogname module not ported to this OS"
  137. # endif
  138. }
  139. #endif