Mercurial > gnulib
annotate lib/getprogname.c @ 39903:cadbcf6826fd
getprogname: Work around program name truncation when possible.
* lib/getprogname.c (getprogname) [HP-UX]: When pst_ucomm is truncated,
possibly use pst_cmd instead.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 11 Oct 2018 18:24:51 +0200 |
parents | 33ee5e90be64 |
children | 7bdba2aa999b |
rev | line source |
---|---|
18396 | 1 /* Program name management. |
19484
10eb9086bea0
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19439
diff
changeset
|
2 Copyright (C) 2016-2018 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 */ |
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
73 /* https://msdn.microsoft.com/en-us/library/dn727674.aspx */ |
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); |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
76 # elif HAVE_VAR___PROGNAME /* OpenBSD, 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; |
7537a3e5eeb4
getprogname: port to OpenBSD 5.1
Jim Meyering <meyering@fb.com>
parents:
18427
diff
changeset
|
85 return p && p[0] ? p : "?"; |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
86 # elif _AIX /* AIX */ |
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
87 /* 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
|
88 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
|
89 Reference: https://www.ibm.com/support/knowledgecenter/en/ssw_aix_61/com.ibm.aix.basetrf1/getprocs.htm |
18427 | 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; | |
18482
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
105 # elif defined __hpux |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
106 static char *p; |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
107 static int first = 1; |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
108 if (first) |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
109 { |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
110 first = 0; |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
111 pid_t pid = getpid (); |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
112 struct pst_status status; |
39903
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
113 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
|
114 { |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
115 if (strlen (status.pst_ucomm) < PST_UCOMMLEN - 1) |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
116 p = status.pst_ucomm; |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
117 else |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
118 { |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
119 /* status.pst_ucomm is truncated to length PST_UCOMMLEN - 1. |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
120 Look at status.pst_cmd instead. */ |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
121 char *space = strchr (status.pst_cmd, ' '); |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
122 if (space != NULL) |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
123 *space = '\0'; |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
124 p = strrchr (status.pst_cmd, '/'); |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
125 if (p != NULL) |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
126 p++; |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
127 else |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
128 p = status.pst_cmd; |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
129 if (strlen (p) > PST_UCOMMLEN - 1 |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
130 && memcmp (p, status.pst_ucomm, PST_UCOMMLEN - 1) == 0) |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
131 /* p is less truncated than status.pst_ucomm. */ |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
132 ; |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
133 else |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
134 p = status.pst_ucomm; |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
135 } |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
136 p = strdup (p); |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
137 } |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
138 else |
cadbcf6826fd
getprogname: Work around program name truncation when possible.
Bruno Haible <bruno@clisp.org>
parents:
19496
diff
changeset
|
139 p = NULL; |
18482
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
140 if (!p) |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
141 p = "?"; |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
142 } |
7016824b7f88
getprogname: port to HP-UX
John David Anglin <dave.anglin@bell.net>
parents:
18461
diff
changeset
|
143 return p; |
18461
66921c9564c8
getprogname: Fix test failure on Cygwin. Comments.
Bruno Haible <bruno@clisp.org>
parents:
18452
diff
changeset
|
144 # elif __MVS__ /* z/OS */ |
18448
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
145 /* 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
|
146 static char *p = "?"; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
147 static int first = 1; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
148 if (first) |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
149 { |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
150 pid_t pid = getpid (); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
151 int token; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
152 W_PSPROC buf; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
153 first = 0; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
154 memset (&buf, 0, sizeof(buf)); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
155 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
|
156 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
|
157 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
|
158 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
|
159 { |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
160 for (token = 0; token >= 0; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
161 token = w_getpsent (token, &buf, sizeof(buf))) |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
162 { |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
163 if (token > 0 && buf.ps_pid == pid) |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
164 { |
18452
deb7ee8c9016
getprogname: IBM z/OS: avoid NULL-dereference
Jim Meyering <meyering@fb.com>
parents:
18448
diff
changeset
|
165 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
|
166 if (s) |
deb7ee8c9016
getprogname: IBM z/OS: avoid NULL-dereference
Jim Meyering <meyering@fb.com>
parents:
18448
diff
changeset
|
167 p = s; |
18448
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
168 break; |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
169 } |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
170 } |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
171 } |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
172 free (buf.ps_cmdptr); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
173 free (buf.ps_conttyptr); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
174 free (buf.ps_pathptr); |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
175 } |
b8568900ba11
getprogname: port to IBM z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents:
18437
diff
changeset
|
176 return p; |
18654
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
177 # elif defined __sgi /* IRIX */ |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
178 char filename[50]; |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
179 int fd; |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
180 |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
181 sprintf (filename, "/proc/pinfo/%d", (int) getpid ()); |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
182 fd = open (filename, O_RDONLY); |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
183 if (0 <= fd) |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
184 { |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
185 prpsinfo_t buf; |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
186 int ioctl_ok = 0 <= ioctl (fd, PIOCPSINFO, &buf); |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
187 close (fd); |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
188 if (ioctl_ok) |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
189 { |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
190 char *name = buf.pr_fname; |
19436
f8452b92d10e
getprogname: Fix compilation error on IRIX.
Bruno Haible <bruno@clisp.org>
parents:
19192
diff
changeset
|
191 size_t namesize = sizeof buf.pr_fname; |
18654
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
192 char *namenul = memchr (name, '\0', namesize); |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
193 size_t namelen = namenul ? namenul - name : namesize; |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
194 char *namecopy = malloc (namelen + 1); |
18661
4e626befa8df
getprogname: fix port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18654
diff
changeset
|
195 if (namecopy) |
4e626befa8df
getprogname: fix port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18654
diff
changeset
|
196 { |
4e626befa8df
getprogname: fix port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18654
diff
changeset
|
197 namecopy[namelen] = 0; |
4e626befa8df
getprogname: fix port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18654
diff
changeset
|
198 return memcpy (namecopy, name, namelen); |
4e626befa8df
getprogname: fix port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18654
diff
changeset
|
199 } |
18654
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
200 } |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
201 } |
fd3deadd769d
getprogname: port to IRIX
Paul Eggert <eggert@cs.ucla.edu>
parents:
18626
diff
changeset
|
202 return NULL; |
18404
ee1b22cf7503
getprogname: port to systems with __argv (mingw, msvc)
Jim Meyering <meyering@fb.com>
parents:
18402
diff
changeset
|
203 # else |
ee1b22cf7503
getprogname: port to systems with __argv (mingw, msvc)
Jim Meyering <meyering@fb.com>
parents:
18402
diff
changeset
|
204 # 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
|
205 # endif |
18404
ee1b22cf7503
getprogname: port to systems with __argv (mingw, msvc)
Jim Meyering <meyering@fb.com>
parents:
18402
diff
changeset
|
206 } |
18396 | 207 |
208 #endif | |
19496
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
209 |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
210 /* |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
211 * Hey Emacs! |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
212 * Local Variables: |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
213 * coding: utf-8 |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
214 * End: |
33ee5e90be64
maint: Add encoding marker for Emacs to non-ASCII sources.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
215 */ |