Debian Codegraph @

lib/getprogname.c

raw
1 /* Program name management.
2 Copyright (C) 2016-2026 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 Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 /* Specification. Also get __argv declaration. */
20 #include <stdlib.h>
21
22 #include <errno.h> /* get program_invocation_name declaration */
23
24 #ifdef _AIX
25 # include <unistd.h>
26 # include <procinfo.h>
27 # include <string.h>
28 #endif
29
30 #ifdef __MVS__
31 # ifndef _OPEN_SYS
32 # define _OPEN_SYS
33 # endif
34 # include <string.h>
35 # include <sys/ps.h>
36 #endif
37
38 #ifdef __hpux
39 # include <unistd.h>
40 # include <sys/param.h>
41 # include <sys/pstat.h>
42 # include <string.h>
43 #endif
44
45 #if defined __SCO_VERSION__ || defined __sysv5__
46 # include <fcntl.h>
47 # include <string.h>
48 #endif
49
50 #include "basename-lgpl.h"
51
52 #ifndef HAVE_GETPROGNAME /* not Mac OS X, FreeBSD, NetBSD, OpenBSD >= 5.4, Solaris >= 11, Cygwin, Android API level >= 21 */
53 char const *
54 getprogname (void)
55 {
56 # if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME /* glibc, BeOS */
57 /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
58 return program_invocation_short_name;
59 # elif HAVE_DECL_PROGRAM_INVOCATION_NAME /* glibc, BeOS */
60 /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */
61 return last_component (program_invocation_name);
62 # elif HAVE_GETEXECNAME /* Solaris */
63 /* https://docs.oracle.com/cd/E19253-01/816-5168/6mbb3hrb1/index.html */
64 const char *p = getexecname ();
65 if (!p)
66 p = "?";
67 return last_component (p);
68 # elif HAVE_DECL___ARGV /* mingw, MSVC */
69 /* https://docs.microsoft.com/en-us/cpp/c-runtime-library/argc-argv-wargv */
70 const char *p = __argv && __argv[0] ? __argv[0] : "?";
71 return last_component (p);
72 # elif HAVE_VAR___PROGNAME /* OpenBSD, Android, QNX */
73 /* https://man.openbsd.org/style.9 */
74 /* http://www.qnx.de/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.neutrino_lib_ref%2Fp%2F__progname.html */
75 /* Be careful to declare this only when we absolutely need it
76 (OpenBSD 5.1), rather than when it's available. Otherwise,
77 its mere declaration makes program_invocation_short_name
78 malfunction (have zero length) with Fedora 25's glibc. */
79 extern char *__progname;
80 const char *p = __progname;
81 # if defined __ANDROID__
82 return last_component (p);
83 # else
84 return p && p[0] ? p : "?";
85 # endif
86 # elif _AIX /* AIX */
87 /* Idea by Bastien ROUCARIÈS,
88 https://lists.gnu.org/r/bug-gnulib/2010-12/msg00095.html
89 Reference: https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.basetrf1/getprocs.htm
90 */
91 static char *p;
92 static int first = 1;
93 if (first)
94 {
95 first = 0;
96 pid_t pid = getpid ();
97 struct procentry64 procs;
98 p = (0 < getprocs64 (&procs, sizeof procs, NULL, 0, &pid, 1)
99 ? strdup (procs.pi_comm)
100 : NULL);
101 if (!p)
102 p = "?";
103 }
104 return p;
105 # elif defined __hpux
106 static char *p;
107 static int first = 1;
108 if (first)
109 {
110 first = 0;
111 pid_t pid = getpid ();
112 struct pst_status status;
113 if (pstat_getproc (&status, sizeof status, 0, pid) > 0)
114 {
115 char *ucomm = status.pst_ucomm;
116 char *cmd = status.pst_cmd;
117 if (strlen (ucomm) < PST_UCOMMLEN - 1)
118 p = ucomm;
119 else
120 {
121 /* ucomm is truncated to length PST_UCOMMLEN - 1.
122 Look at cmd instead. */
123 char *space = strchr (cmd, ' ');
124 if (space != NULL)
125 *space = '\0';
126 p = strrchr (cmd, '/');
127 if (p != NULL)
128 p++;
129 else
130 p = cmd;
131 if (strlen (p) > PST_UCOMMLEN - 1
132 && memeq (p, ucomm, PST_UCOMMLEN - 1))
133 /* p is less truncated than ucomm. */
134 ;
135 else
136 p = ucomm;
137 }
138 p = strdup (p);
139 }
140 else
141 {
142 # if !defined __LP64__
143 /* Support for 32-bit programs running in 64-bit HP-UX.
144 The documented way to do this is to use the same source code
145 as above, but in a compilation unit where '#define _PSTAT64 1'
146 is in effect. I prefer a single compilation unit; the struct
147 size and the offsets are not going to change. */
148 char status64[1216];
149 if (__pstat_getproc64 (status64, sizeof status64, 0, pid) > 0)
150 {
151 char *ucomm = status64 + 288;
152 char *cmd = status64 + 168;
153 if (strlen (ucomm) < PST_UCOMMLEN - 1)
154 p = ucomm;
155 else
156 {
157 /* ucomm is truncated to length PST_UCOMMLEN - 1.
158 Look at cmd instead. */
159 char *space = strchr (cmd, ' ');
160 if (space != NULL)
161 *space = '\0';
162 p = strrchr (cmd, '/');
163 if (p != NULL)
164 p++;
165 else
166 p = cmd;
167 if (strlen (p) > PST_UCOMMLEN - 1
168 && memeq (p, ucomm, PST_UCOMMLEN - 1))
169 /* p is less truncated than ucomm. */
170 ;
171 else
172 p = ucomm;
173 }
174 p = strdup (p);
175 }
176 else
177 # endif
178 p = NULL;
179 }
180 if (!p)
181 p = "?";
182 }
183 return p;
184 # elif __MVS__ /* z/OS */
185 /* https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/rtwgetp.htm */
186 static char *p = "?";
187 static int first = 1;
188 if (first)
189 {
190 pid_t pid = getpid ();
191 W_PSPROC buf;
192 first = 0;
193 memset (&buf, 0, sizeof(buf));
194 buf.ps_cmdptr = (char *) malloc (buf.ps_cmdlen = PS_CMDBLEN_LONG);
195 buf.ps_conttyptr = (char *) malloc (buf.ps_conttylen = PS_CONTTYBLEN);
196 buf.ps_pathptr = (char *) malloc (buf.ps_pathlen = PS_PATHBLEN);
197 if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr)
198 {
199 for (int token = 0;
200 token >= 0;
201 token = w_getpsent (token, &buf, sizeof(buf)))
202 {
203 if (token > 0 && buf.ps_pid == pid)
204 {
205 char *s = strdup (last_component (buf.ps_pathptr));
206 if (s)
207 {
208 # if defined __XPLINK__ && __CHARSET_LIB == 1
209 /* The compiler option -qascii is in use.
210 https://makingdeveloperslivesbetter.wordpress.com/2022/01/07/is-z-os-ascii-or-ebcdic-yes/
211 https://www.ibm.com/docs/en/zos/2.5.0?topic=features-macros-related-compiler-option-settings
212 So, convert the result from EBCDIC to ASCII.
213 https://www.ibm.com/docs/en/zos/2.5.0?topic=functions-e2a-s-convert-string-from-ebcdic-ascii */
214 if (__e2a_s (s) == (size_t)-1)
215 free (s);
216 else
217 # endif
218 p = s;
219 }
220 break;
221 }
222 }
223 }
224 free (buf.ps_cmdptr);
225 free (buf.ps_conttyptr);
226 free (buf.ps_pathptr);
227 }
228 return p;
229 # elif defined __SCO_VERSION__ || defined __sysv5__ /* SCO OpenServer6/UnixWare */
230 char buf[80];
231 sprintf (buf, "/proc/%d/cmdline", getpid());
232 int fd = open (buf, O_RDONLY);
233 if (0 <= fd)
234 {
235 size_t n = read (fd, buf, 79);
236 if (n > 0)
237 {
238 buf[n] = '\0'; /* Guarantee null-termination */
239 char *progname = strrchr (buf, '/');
240 if (progname)
241 progname = progname + 1; /* Skip the '/' */
242 else
243 progname = buf;
244 char *ret = malloc (strlen (progname) + 1);
245 if (ret)
246 {
247 strcpy (ret, progname);
248 return ret;
249 }
250 }
251 close (fd);
252 }
253 return "?";
254 # else
255 # error "getprogname module not ported to this OS"
256 # endif
257 }
258
259 #endif
260
261 /*
262 * Hey Emacs!
263 * Local Variables:
264 * coding: utf-8
265 * End:
266 */