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