Mercurial > gnulib
annotate lib/getprogname.c @ 40235:5a52ef2d4772
all: Update URLs to msdn.microsoft.com.
* lib/stat-w32.c et al.: Update URLs after most of msdn.microsoft.com
was moved to docs.microsoft.com.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 14 Mar 2019 09:49:24 +0100 |
parents | bec39651dc8d |
children |
rev | line source |
---|---|
18396 | 1 /* Program name management. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
39904
diff
changeset
|
2 Copyright (C) 2016-2019 Free Software Foundation, Inc. |
18396 | 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 | |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
18396 | 16 |
17 #include <config.h> | |
18 | |
19 /* Specification. */ | |
20 #include "getprogname.h" | |
21 | |
22 #include <errno.h> /* get program_invocation_name declaration */ | |
18404
ee1b22cf7503
getprogname: port to systems with __argv (mingw, msvc)
Jim Meyering <meyering@fb.com>
parents:
18402
diff
changeset
|
23 #include <stdlib.h> /* get __argv declaration */ |
18396 | 24 |
18427 | 25 #ifdef _AIX |
26 # include <unistd.h> | |
27 # include <procinfo.h> | |
28 # include <string.h> | |
29 #endif | |
30 | |
18448
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
31 #ifdef __MVS__ |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
32 # ifndef _OPEN_SYS |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
33 # define _OPEN_SYS |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
34 # endif |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
35 # include <string.h> |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
36 # include <sys/ps.h> |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
37 #endif |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
38 |
18482
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
39 #ifdef __hpux |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
40 # include <unistd.h> |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
41 # include <sys/param.h> |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
42 # include <sys/pstat.h> |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
43 # include <string.h> |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
44 #endif |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
45 |
18654
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
46 #ifdef __sgi |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
47 # include <string.h> |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
48 # include <unistd.h> |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
49 # include <stdio.h> |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
50 # include <fcntl.h> |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
51 # include <sys/procfs.h> |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
52 #endif |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
53 |
18404
ee1b22cf7503
getprogname: port to systems with __argv (mingw, msvc)
Jim Meyering <meyering@fb.com>
parents:
18402
diff
changeset
|
54 #include "dirname.h" |
18396 | 55 |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
56 #ifndef HAVE_GETPROGNAME /* not Mac OS X, FreeBSD, NetBSD, OpenBSD >= 5.4, Cygwin */ |
18404
ee1b22cf7503
getprogname: port to systems with __argv (mingw, msvc)
Jim Meyering <meyering@fb.com>
parents:
18402
diff
changeset
|
57 char const * |
18396 | 58 getprogname (void) |
59 { | |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
60 # if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME /* glibc, BeOS */ |
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
61 /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */ |
18396 | 62 return program_invocation_short_name; |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
63 # elif HAVE_DECL_PROGRAM_INVOCATION_NAME /* glibc, BeOS */ |
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
64 /* https://www.gnu.org/software/libc/manual/html_node/Error-Messages.html */ |
18404
ee1b22cf7503
getprogname: port to systems with __argv (mingw, msvc)
Jim Meyering <meyering@fb.com>
parents:
18402
diff
changeset
|
65 return last_component (program_invocation_name); |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
66 # elif HAVE_GETEXECNAME /* Solaris */ |
19192
d86e08b1f555
all: Replace many more http URLs by https URLs. Update stale URLs.
Bruno Haible <bruno@clisp.org>
parents:
19190
diff
changeset
|
67 /* https://docs.oracle.com/cd/E19253-01/816-5168/6mbb3hrb1/index.html */ |
18407
201a251ddcf2
getprogname: fix errors in previous change
Gisle Vanem <gvanem@yahoo.no>
parents:
18404
diff
changeset
|
68 const char *p = getexecname (); |
201a251ddcf2
getprogname: fix errors in previous change
Gisle Vanem <gvanem@yahoo.no>
parents:
18404
diff
changeset
|
69 if (!p) |
201a251ddcf2
getprogname: fix errors in previous change
Gisle Vanem <gvanem@yahoo.no>
parents:
18404
diff
changeset
|
70 p = "?"; |
201a251ddcf2
getprogname: fix errors in previous change
Gisle Vanem <gvanem@yahoo.no>
parents:
18404
diff
changeset
|
71 return last_component (p); |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
72 # elif HAVE_DECL___ARGV /* mingw, MSVC */ |
40235
5a52ef2d4772
all: Update URLs to msdn.microsoft.com.
Bruno Haible <bruno@clisp.org>
parents:
40138
diff
changeset
|
73 /* https://docs.microsoft.com/en-us/cpp/c-runtime-library/argc-argv-wargv */ |
18407
201a251ddcf2
getprogname: fix errors in previous change
Gisle Vanem <gvanem@yahoo.no>
parents:
18404
diff
changeset
|
74 const char *p = __argv && __argv[0] ? __argv[0] : "?"; |
201a251ddcf2
getprogname: fix errors in previous change
Gisle Vanem <gvanem@yahoo.no>
parents:
18404
diff
changeset
|
75 return last_component (p); |
40138
bec39651dc8d
getprogname: Port to Android 4.3.
Bruno Haible <bruno@clisp.org>
parents:
40057
diff
changeset
|
76 # elif HAVE_VAR___PROGNAME /* OpenBSD, Android, QNX */ |
19192
d86e08b1f555
all: Replace many more http URLs by https URLs. Update stale URLs.
Bruno Haible <bruno@clisp.org>
parents:
19190
diff
changeset
|
77 /* https://man.openbsd.org/style.9 */ |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
78 /* http://www.qnx.de/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.neutrino_lib_ref%2Fp%2F__progname.html */ |
18434
2596c4b87581
getprogname: avoid __progname vs program_invocation_short_name pitfall
Jim Meyering <meyering@fb.com>
parents:
18433
diff
changeset
|
79 /* Be careful to declare this only when we absolutely need it |
2596c4b87581
getprogname: avoid __progname vs program_invocation_short_name pitfall
Jim Meyering <meyering@fb.com>
parents:
18433
diff
changeset
|
80 (OpenBSD 5.1), rather than when it's available. Otherwise, |
2596c4b87581
getprogname: avoid __progname vs program_invocation_short_name pitfall
Jim Meyering <meyering@fb.com>
parents:
18433
diff
changeset
|
81 its mere declaration makes program_invocation_short_name |
2596c4b87581
getprogname: avoid __progname vs program_invocation_short_name pitfall
Jim Meyering <meyering@fb.com>
parents:
18433
diff
changeset
|
82 malfunction (have zero length) with Fedora 25's glibc. */ |
2596c4b87581
getprogname: avoid __progname vs program_invocation_short_name pitfall
Jim Meyering <meyering@fb.com>
parents:
18433
diff
changeset
|
83 extern char *__progname; |
18433
7537a3e5eeb4
getprogname: port to OpenBSD 5.1
Jim Meyering <meyering@fb.com>
parents:
18427
diff
changeset
|
84 const char *p = __progname; |
40138
bec39651dc8d
getprogname: Port to Android 4.3.
Bruno Haible <bruno@clisp.org>
parents:
40057
diff
changeset
|
85 # if defined __ANDROID__ |
bec39651dc8d
getprogname: Port to Android 4.3.
Bruno Haible <bruno@clisp.org>
parents:
40057
diff
changeset
|
86 return last_component (p); |
bec39651dc8d
getprogname: Port to Android 4.3.
Bruno Haible <bruno@clisp.org>
parents:
40057
diff
changeset
|
87 # else |
18433
7537a3e5eeb4
getprogname: port to OpenBSD 5.1
Jim Meyering <meyering@fb.com>
parents:
18427
diff
changeset
|
88 return p && p[0] ? p : "?"; |
40138
bec39651dc8d
getprogname: Port to Android 4.3.
Bruno Haible <bruno@clisp.org>
parents:
40057
diff
changeset
|
89 # endif |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
90 # elif _AIX /* AIX */ |
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
91 /* Idea by Bastien ROUCARIÈS, |
19439
8bfc20b57265
maint: shorten https://lists.gnu.org/archive/html/... links
Jim Meyering <meyering@fb.com>
parents:
19436
diff
changeset
|
92 https://lists.gnu.org/r/bug-gnulib/2010-12/msg00095.html |
19192
d86e08b1f555
all: Replace many more http URLs by https URLs. Update stale URLs.
Bruno Haible <bruno@clisp.org>
parents:
19190
diff
changeset
|
93 Reference: https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.basetrf1/getprocs.htm |
18427 | 94 */ |
95 static char *p; | |
96 static int first = 1; | |
97 if (first) | |
98 { | |
99 first = 0; | |
100 pid_t pid = getpid (); | |
101 struct procentry64 procs; | |
102 p = (0 < getprocs64 (&procs, sizeof procs, NULL, 0, &pid, 1) | |
103 ? strdup (procs.pi_comm) | |
104 : NULL); | |
105 if (!p) | |
106 p = "?"; | |
107 } | |
108 return p; | |
18482
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
109 # elif defined __hpux |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
110 static char *p; |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
111 static int first = 1; |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
112 if (first) |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
113 { |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
114 first = 0; |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
115 pid_t pid = getpid (); |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
116 struct pst_status status; |
39903
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
117 if (pstat_getproc (&status, sizeof status, 0, pid) > 0) |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
118 { |
39904
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
119 char *ucomm = status.pst_ucomm; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
120 char *cmd = status.pst_cmd; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
121 if (strlen (ucomm) < PST_UCOMMLEN - 1) |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
122 p = ucomm; |
39903
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
123 else |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
124 { |
39904
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
125 /* ucomm is truncated to length PST_UCOMMLEN - 1. |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
126 Look at cmd instead. */ |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
127 char *space = strchr (cmd, ' '); |
39903
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
128 if (space != NULL) |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
129 *space = '\0'; |
39904
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
130 p = strrchr (cmd, '/'); |
39903
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
131 if (p != NULL) |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
132 p++; |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
133 else |
39904
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
134 p = cmd; |
39903
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
135 if (strlen (p) > PST_UCOMMLEN - 1 |
39904
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
136 && memcmp (p, ucomm, PST_UCOMMLEN - 1) == 0) |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
137 /* p is less truncated than ucomm. */ |
39903
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
138 ; |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
139 else |
39904
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
140 p = ucomm; |
39903
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
141 } |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
142 p = strdup (p); |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
143 } |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
144 else |
39904
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
145 { |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
146 # if !defined __LP64__ |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
147 /* Support for 32-bit programs running in 64-bit HP-UX. |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
148 The documented way to do this is to use the same source code |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
149 as above, but in a compilation unit where '#define _PSTAT64 1' |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
150 is in effect. I prefer a single compilation unit; the struct |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
151 size and the offsets are not going to change. */ |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
152 char status64[1216]; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
153 if (__pstat_getproc64 (status64, sizeof status64, 0, pid) > 0) |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
154 { |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
155 char *ucomm = status64 + 288; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
156 char *cmd = status64 + 168; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
157 if (strlen (ucomm) < PST_UCOMMLEN - 1) |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
158 p = ucomm; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
159 else |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
160 { |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
161 /* ucomm is truncated to length PST_UCOMMLEN - 1. |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
162 Look at cmd instead. */ |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
163 char *space = strchr (cmd, ' '); |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
164 if (space != NULL) |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
165 *space = '\0'; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
166 p = strrchr (cmd, '/'); |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
167 if (p != NULL) |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
168 p++; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
169 else |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
170 p = cmd; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
171 if (strlen (p) > PST_UCOMMLEN - 1 |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
172 && memcmp (p, ucomm, PST_UCOMMLEN - 1) == 0) |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
173 /* p is less truncated than ucomm. */ |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
174 ; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
175 else |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
176 p = ucomm; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
177 } |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
178 p = strdup (p); |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
179 } |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
180 else |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
181 # endif |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
182 p = NULL; |
c9d7c2bfa3f1
getprogname: Add support for 32-bit programs on HP-UX.
Bruno Haible <bruno@clisp.org>
parents:
39903
diff
changeset
|
183 } |
18482
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
184 if (!p) |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
185 p = "?"; |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
186 } |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
187 return p; |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
188 # elif __MVS__ /* z/OS */ |
18448
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
189 /* https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/rtwgetp.htm */ |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
190 static char *p = "?"; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
191 static int first = 1; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
192 if (first) |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
193 { |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
194 pid_t pid = getpid (); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
195 int token; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
196 W_PSPROC buf; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
197 first = 0; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
198 memset (&buf, 0, sizeof(buf)); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
199 buf.ps_cmdptr = (char *) malloc (buf.ps_cmdlen = PS_CMDBLEN_LONG); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
200 buf.ps_conttyptr = (char *) malloc (buf.ps_conttylen = PS_CONTTYBLEN); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
201 buf.ps_pathptr = (char *) malloc (buf.ps_pathlen = PS_PATHBLEN); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
202 if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr) |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
203 { |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
204 for (token = 0; token >= 0; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
205 token = w_getpsent (token, &buf, sizeof(buf))) |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
206 { |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
207 if (token > 0 && buf.ps_pid == pid) |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
208 { |
18452
deb7ee8c9016
getprogname: IBM z/OS: avoid NULL-dereference
Jim Meyering <meyering@fb.com>
parents:
18448
diff
changeset
|
209 char *s = strdup (last_component (buf.ps_pathptr)); |
deb7ee8c9016
getprogname: IBM z/OS: avoid NULL-dereference
Jim Meyering <meyering@fb.com>
parents:
18448
diff
changeset
|
210 if (s) |
deb7ee8c9016
getprogname: IBM z/OS: avoid NULL-dereference
Jim Meyering <meyering@fb.com>
parents:
18448
diff
changeset
|
211 p = s; |
18448
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
212 break; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
213 } |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
214 } |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
215 } |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
216 free (buf.ps_cmdptr); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
217 free (buf.ps_conttyptr); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
218 free (buf.ps_pathptr); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
219 } |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
220 return p; |
18654
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
221 # elif defined __sgi /* IRIX */ |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
222 char filename[50]; |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
223 int fd; |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
224 |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
225 sprintf (filename, "/proc/pinfo/%d", (int) getpid ()); |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
226 fd = open (filename, O_RDONLY); |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
227 if (0 <= fd) |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
228 { |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
229 prpsinfo_t buf; |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
230 int ioctl_ok = 0 <= ioctl (fd, PIOCPSINFO, &buf); |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
231 close (fd); |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
232 if (ioctl_ok) |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
233 { |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
234 char *name = buf.pr_fname; |
19436
f8452b92d10e
getprogname: Fix compilation error on IRIX.
Bruno Haible <bruno@clisp.org>
parents:
19192
diff
changeset
|
235 size_t namesize = sizeof buf.pr_fname; |
18654
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
236 char *namenul = memchr (name, '\0', namesize); |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
237 size_t namelen = namenul ? namenul - name : namesize; |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
238 char *namecopy = malloc (namelen + 1); |
18661
4e626befa8df
getprogname: fix port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18654
diff
changeset
|
239 if (namecopy) |
4e626befa8df
getprogname: fix port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18654
diff
changeset
|
240 { |
4e626befa8df
getprogname: fix port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18654
diff
changeset
|
241 namecopy[namelen] = 0; |
4e626befa8df
getprogname: fix port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18654
diff
changeset
|
242 return memcpy (namecopy, name, namelen); |
4e626befa8df
getprogname: fix port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18654
diff
changeset
|
243 } |
18654
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
244 } |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
245 } |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
246 return NULL; |
18404
ee1b22cf7503
getprogname: port to systems with __argv (mingw, msvc)
Jim Meyering <meyering@fb.com>
parents:
18402
diff
changeset
|
247 # else |
ee1b22cf7503
getprogname: port to systems with __argv (mingw, msvc)
Jim Meyering <meyering@fb.com>
parents:
18402
diff
changeset
|
248 # error "getprogname module not ported to this OS" |
18402
55d5db124878
getprogname: port to Solaris 10
Paul Eggert <eggert@cs.ucla.edu>
parents:
18396
diff
changeset
|
249 # endif |
18404
ee1b22cf7503
getprogname: port to systems with __argv (mingw, msvc)
Jim Meyering <meyering@fb.com>
parents:
18402
diff
changeset
|
250 } |
18396 | 251 |
252 #endif | |
19496
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
253 |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
254 /* |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
255 * Hey Emacs! |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
256 * Local Variables: |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
257 * coding: utf-8 |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
258 * End: |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
259 */ |