Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | #include <config.h> | - |
18 | | - |
19 | | - |
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 | #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 | - |
53 | | - |
54 | #include "dirname.h" | - |
55 | | - |
56 | #ifndef HAVE_GETPROGNAME /* not Mac OS X, FreeBSD, NetBSD, OpenBSD >= 5.4, Cygwin */ | - |
57 | char const * | - |
58 | getprogname (void) | - |
59 | { | - |
60 | # if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME /* glibc, BeOS */ | - |
61 | | - |
62 | return program_invocation_short_name; never executed: return program_invocation_short_name; | 0 |
63 | # elif HAVE_DECL_PROGRAM_INVOCATION_NAME /* glibc, BeOS */ | - |
64 | | - |
65 | return last_component (program_invocation_name); | - |
66 | # elif HAVE_GETEXECNAME /* Solaris */ | - |
67 | | - |
68 | const char *p = getexecname (); | - |
69 | if (!p) | - |
70 | p = "?"; | - |
71 | return last_component (p); | - |
72 | # elif HAVE_DECL___ARGV /* mingw, MSVC */ | - |
73 | | - |
74 | const char *p = __argv && __argv[0] ? __argv[0] : "?"; | - |
75 | return last_component (p); | - |
76 | # elif HAVE_VAR___PROGNAME /* OpenBSD, QNX */ | - |
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | extern char *__progname; | - |
84 | const char *p = __progname; | - |
85 | return p && p[0] ? p : "?"; | - |
86 | # elif _AIX /* AIX */ | - |
87 | | - |
88 | | - |
89 | | - |
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 | p = (0 < pstat_getproc (&status, sizeof status, 0, pid) | - |
114 | ? strdup (status.pst_ucomm) | - |
115 | : NULL); | - |
116 | if (!p) | - |
117 | p = "?"; | - |
118 | } | - |
119 | return p; | - |
120 | # elif __MVS__ /* z/OS */ | - |
121 | | - |
122 | static char *p = "?"; | - |
123 | static int first = 1; | - |
124 | if (first) | - |
125 | { | - |
126 | pid_t pid = getpid (); | - |
127 | int token; | - |
128 | W_PSPROC buf; | - |
129 | first = 0; | - |
130 | memset (&buf, 0, sizeof(buf)); | - |
131 | buf.ps_cmdptr = (char *) malloc (buf.ps_cmdlen = PS_CMDBLEN_LONG); | - |
132 | buf.ps_conttyptr = (char *) malloc (buf.ps_conttylen = PS_CONTTYBLEN); | - |
133 | buf.ps_pathptr = (char *) malloc (buf.ps_pathlen = PS_PATHBLEN); | - |
134 | if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr) | - |
135 | { | - |
136 | for (token = 0; token >= 0; | - |
137 | token = w_getpsent (token, &buf, sizeof(buf))) | - |
138 | { | - |
139 | if (token > 0 && buf.ps_pid == pid) | - |
140 | { | - |
141 | char *s = strdup (last_component (buf.ps_pathptr)); | - |
142 | if (s) | - |
143 | p = s; | - |
144 | break; | - |
145 | } | - |
146 | } | - |
147 | } | - |
148 | free (buf.ps_cmdptr); | - |
149 | free (buf.ps_conttyptr); | - |
150 | free (buf.ps_pathptr); | - |
151 | } | - |
152 | return p; | - |
153 | # elif defined __sgi /* IRIX */ | - |
154 | char filename[50]; | - |
155 | int fd; | - |
156 | | - |
157 | sprintf (filename, "/proc/pinfo/%d", (int) getpid ()); | - |
158 | fd = open (filename, O_RDONLY); | - |
159 | if (0 <= fd) | - |
160 | { | - |
161 | prpsinfo_t buf; | - |
162 | int ioctl_ok = 0 <= ioctl (fd, PIOCPSINFO, &buf); | - |
163 | close (fd); | - |
164 | if (ioctl_ok) | - |
165 | { | - |
166 | char *name = buf.pr_fname; | - |
167 | size_t namesize = sizeof buf.pr_fname; | - |
168 | char *namenul = memchr (name, '\0', namesize); | - |
169 | size_t namelen = namenul ? namenul - name : namesize; | - |
170 | char *namecopy = malloc (namelen + 1); | - |
171 | if (namecopy) | - |
172 | { | - |
173 | namecopy[namelen] = 0; | - |
174 | return memcpy (namecopy, name, namelen); | - |
175 | } | - |
176 | } | - |
177 | } | - |
178 | return NULL; | - |
179 | # else | - |
180 | # error "getprogname module not ported to this OS" | - |
181 | # endif | - |
182 | } | - |
183 | | - |
184 | #endif | - |
185 | | - |
186 | | - |
187 | | - |
188 | | - |
189 | | - |
190 | | - |
191 | | - |
| | |