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