getprogname.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Program name management.
  2. Copyright (C) 2016-2017 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. # 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. #ifdef __sgi
  37. # include <string.h>
  38. # include <unistd.h>
  39. # include <stdio.h>
  40. # include <fcntl.h>
  41. # include <sys/procfs.h>
  42. #endif
  43. #include "dirname.h"
  44. #ifndef HAVE_GETPROGNAME /* not Mac OS X, FreeBSD, NetBSD, OpenBSD >= 5.4, Cygwin */
  45. char const *
  46. getprogname (void)
  47. {
  48. # if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME /* glibc, BeOS */
  49. /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
  50. return program_invocation_short_name;
  51. # elif HAVE_DECL_PROGRAM_INVOCATION_NAME /* glibc, BeOS */
  52. /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
  53. return last_component (program_invocation_name);
  54. # elif HAVE_GETEXECNAME /* Solaris */
  55. /* http://docs.oracle.com/cd/E19253-01/816-5168/6mbb3hrb1/index.html */
  56. const char *p = getexecname ();
  57. if (!p)
  58. p = "?";
  59. return last_component (p);
  60. # elif HAVE_DECL___ARGV /* mingw, MSVC */
  61. /* https://msdn.microsoft.com/en-us/library/dn727674.aspx */
  62. const char *p = __argv && __argv[0] ? __argv[0] : "?";
  63. return last_component (p);
  64. # elif HAVE_VAR___PROGNAME /* OpenBSD, QNX */
  65. /* http://man.openbsd.org/style.9 */
  66. /* http://www.qnx.de/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.neutrino_lib_ref%2Fp%2F__progname.html */
  67. /* Be careful to declare this only when we absolutely need it
  68. (OpenBSD 5.1), rather than when it's available. Otherwise,
  69. its mere declaration makes program_invocation_short_name
  70. malfunction (have zero length) with Fedora 25's glibc. */
  71. extern char *__progname;
  72. const char *p = __progname;
  73. return p && p[0] ? p : "?";
  74. # elif _AIX /* AIX */
  75. /* Idea by Bastien ROUCARIÈS,
  76. http://lists.gnu.org/archive/html/bug-gnulib/2010-12/msg00095.html
  77. Reference: http://
  78. ibm.biz/knowctr#ssw_aix_53/com.ibm.aix.basetechref/doc/basetrf1/getprocs.htm
  79. */
  80. static char *p;
  81. static int first = 1;
  82. if (first)
  83. {
  84. first = 0;
  85. pid_t pid = getpid ();
  86. struct procentry64 procs;
  87. p = (0 < getprocs64 (&procs, sizeof procs, NULL, 0, &pid, 1)
  88. ? strdup (procs.pi_comm)
  89. : NULL);
  90. if (!p)
  91. p = "?";
  92. }
  93. return p;
  94. # elif defined __hpux
  95. static char *p;
  96. static int first = 1;
  97. if (first)
  98. {
  99. first = 0;
  100. pid_t pid = getpid ();
  101. struct pst_status status;
  102. p = (0 < pstat_getproc (&status, sizeof status, 0, pid)
  103. ? strdup (status.pst_ucomm)
  104. : NULL);
  105. if (!p)
  106. p = "?";
  107. }
  108. return p;
  109. # elif __MVS__ /* z/OS */
  110. /* https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/rtwgetp.htm */
  111. static char *p = "?";
  112. static int first = 1;
  113. if (first)
  114. {
  115. pid_t pid = getpid ();
  116. int token;
  117. W_PSPROC buf;
  118. first = 0;
  119. memset (&buf, 0, sizeof(buf));
  120. buf.ps_cmdptr = (char *) malloc (buf.ps_cmdlen = PS_CMDBLEN_LONG);
  121. buf.ps_conttyptr = (char *) malloc (buf.ps_conttylen = PS_CONTTYBLEN);
  122. buf.ps_pathptr = (char *) malloc (buf.ps_pathlen = PS_PATHBLEN);
  123. if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr)
  124. {
  125. for (token = 0; token >= 0;
  126. token = w_getpsent (token, &buf, sizeof(buf)))
  127. {
  128. if (token > 0 && buf.ps_pid == pid)
  129. {
  130. char *s = strdup (last_component (buf.ps_pathptr));
  131. if (s)
  132. p = s;
  133. break;
  134. }
  135. }
  136. }
  137. free (buf.ps_cmdptr);
  138. free (buf.ps_conttyptr);
  139. free (buf.ps_pathptr);
  140. }
  141. return p;
  142. # elif defined __sgi /* IRIX */
  143. char filename[50];
  144. int fd;
  145. sprintf (filename, "/proc/pinfo/%d", (int) getpid ());
  146. fd = open (filename, O_RDONLY);
  147. if (0 <= fd)
  148. {
  149. prpsinfo_t buf;
  150. int ioctl_ok = 0 <= ioctl (fd, PIOCPSINFO, &buf);
  151. close (fd);
  152. if (ioctl_ok)
  153. {
  154. char *name = buf.pr_fname;
  155. char *namesize = sizeof buf.pr_fname;
  156. char *namenul = memchr (name, '\0', namesize);
  157. size_t namelen = namenul ? namenul - name : namesize;
  158. char *namecopy = malloc (namelen + 1);
  159. if (namecopy)
  160. {
  161. namecopy[namelen] = 0;
  162. return memcpy (namecopy, name, namelen);
  163. }
  164. }
  165. }
  166. return NULL;
  167. # else
  168. # error "getprogname module not ported to this OS"
  169. # endif
  170. }
  171. #endif