diff options
Diffstat (limited to 'gl/getprogname.c')
| -rw-r--r-- | gl/getprogname.c | 66 |
1 files changed, 10 insertions, 56 deletions
diff --git a/gl/getprogname.c b/gl/getprogname.c index 4fe7c90d..8eec96c5 100644 --- a/gl/getprogname.c +++ b/gl/getprogname.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* Program name management. | 1 | /* Program name management. |
| 2 | Copyright (C) 2016-2025 Free Software Foundation, Inc. | 2 | Copyright (C) 2016-2026 Free Software Foundation, Inc. |
| 3 | 3 | ||
| 4 | This program is free software: you can redistribute it and/or modify | 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 | 5 | it under the terms of the GNU Lesser General Public License as published by |
| @@ -42,14 +42,6 @@ | |||
| 42 | # include <string.h> | 42 | # include <string.h> |
| 43 | #endif | 43 | #endif |
| 44 | 44 | ||
| 45 | #if defined __sgi || defined __osf__ | ||
| 46 | # include <string.h> | ||
| 47 | # include <unistd.h> | ||
| 48 | # include <stdio.h> | ||
| 49 | # include <fcntl.h> | ||
| 50 | # include <sys/procfs.h> | ||
| 51 | #endif | ||
| 52 | |||
| 53 | #if defined __SCO_VERSION__ || defined __sysv5__ | 45 | #if defined __SCO_VERSION__ || defined __sysv5__ |
| 54 | # include <fcntl.h> | 46 | # include <fcntl.h> |
| 55 | # include <string.h> | 47 | # include <string.h> |
| @@ -137,7 +129,7 @@ getprogname (void) | |||
| 137 | else | 129 | else |
| 138 | p = cmd; | 130 | p = cmd; |
| 139 | if (strlen (p) > PST_UCOMMLEN - 1 | 131 | if (strlen (p) > PST_UCOMMLEN - 1 |
| 140 | && memcmp (p, ucomm, PST_UCOMMLEN - 1) == 0) | 132 | && memeq (p, ucomm, PST_UCOMMLEN - 1)) |
| 141 | /* p is less truncated than ucomm. */ | 133 | /* p is less truncated than ucomm. */ |
| 142 | ; | 134 | ; |
| 143 | else | 135 | else |
| @@ -173,7 +165,7 @@ getprogname (void) | |||
| 173 | else | 165 | else |
| 174 | p = cmd; | 166 | p = cmd; |
| 175 | if (strlen (p) > PST_UCOMMLEN - 1 | 167 | if (strlen (p) > PST_UCOMMLEN - 1 |
| 176 | && memcmp (p, ucomm, PST_UCOMMLEN - 1) == 0) | 168 | && memeq (p, ucomm, PST_UCOMMLEN - 1)) |
| 177 | /* p is less truncated than ucomm. */ | 169 | /* p is less truncated than ucomm. */ |
| 178 | ; | 170 | ; |
| 179 | else | 171 | else |
| @@ -196,7 +188,6 @@ getprogname (void) | |||
| 196 | if (first) | 188 | if (first) |
| 197 | { | 189 | { |
| 198 | pid_t pid = getpid (); | 190 | pid_t pid = getpid (); |
| 199 | int token; | ||
| 200 | W_PSPROC buf; | 191 | W_PSPROC buf; |
| 201 | first = 0; | 192 | first = 0; |
| 202 | memset (&buf, 0, sizeof(buf)); | 193 | memset (&buf, 0, sizeof(buf)); |
| @@ -205,7 +196,8 @@ getprogname (void) | |||
| 205 | buf.ps_pathptr = (char *) malloc (buf.ps_pathlen = PS_PATHBLEN); | 196 | buf.ps_pathptr = (char *) malloc (buf.ps_pathlen = PS_PATHBLEN); |
| 206 | if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr) | 197 | if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr) |
| 207 | { | 198 | { |
| 208 | for (token = 0; token >= 0; | 199 | for (int token = 0; |
| 200 | token >= 0; | ||
| 209 | token = w_getpsent (token, &buf, sizeof(buf))) | 201 | token = w_getpsent (token, &buf, sizeof(buf))) |
| 210 | { | 202 | { |
| 211 | if (token > 0 && buf.ps_pid == pid) | 203 | if (token > 0 && buf.ps_pid == pid) |
| @@ -234,60 +226,22 @@ getprogname (void) | |||
| 234 | free (buf.ps_pathptr); | 226 | free (buf.ps_pathptr); |
| 235 | } | 227 | } |
| 236 | return p; | 228 | return p; |
| 237 | # elif defined __sgi || defined __osf__ /* IRIX or Tru64 */ | ||
| 238 | char filename[50]; | ||
| 239 | int fd; | ||
| 240 | |||
| 241 | # if defined __sgi | ||
| 242 | sprintf (filename, "/proc/pinfo/%d", (int) getpid ()); | ||
| 243 | # else | ||
| 244 | sprintf (filename, "/proc/%d", (int) getpid ()); | ||
| 245 | # endif | ||
| 246 | fd = open (filename, O_RDONLY | O_CLOEXEC); | ||
| 247 | if (0 <= fd) | ||
| 248 | { | ||
| 249 | prpsinfo_t buf; | ||
| 250 | int ioctl_ok = 0 <= ioctl (fd, PIOCPSINFO, &buf); | ||
| 251 | close (fd); | ||
| 252 | if (ioctl_ok) | ||
| 253 | { | ||
| 254 | char *name = buf.pr_fname; | ||
| 255 | size_t namesize = sizeof buf.pr_fname; | ||
| 256 | /* It may not be NUL-terminated. */ | ||
| 257 | char *namenul = memchr (name, '\0', namesize); | ||
| 258 | size_t namelen = namenul ? namenul - name : namesize; | ||
| 259 | char *namecopy = malloc (namelen + 1); | ||
| 260 | if (namecopy) | ||
| 261 | { | ||
| 262 | namecopy[namelen] = '\0'; | ||
| 263 | return memcpy (namecopy, name, namelen); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | } | ||
| 267 | return NULL; | ||
| 268 | # elif defined __SCO_VERSION__ || defined __sysv5__ /* SCO OpenServer6/UnixWare */ | 229 | # elif defined __SCO_VERSION__ || defined __sysv5__ /* SCO OpenServer6/UnixWare */ |
| 269 | char buf[80]; | 230 | char buf[80]; |
| 270 | int fd; | ||
| 271 | sprintf (buf, "/proc/%d/cmdline", getpid()); | 231 | sprintf (buf, "/proc/%d/cmdline", getpid()); |
| 272 | fd = open (buf, O_RDONLY); | 232 | int fd = open (buf, O_RDONLY); |
| 273 | if (0 <= fd) | 233 | if (0 <= fd) |
| 274 | { | 234 | { |
| 275 | size_t n = read (fd, buf, 79); | 235 | size_t n = read (fd, buf, 79); |
| 276 | if (n > 0) | 236 | if (n > 0) |
| 277 | { | 237 | { |
| 278 | buf[n] = '\0'; /* Guarantee null-termination */ | 238 | buf[n] = '\0'; /* Guarantee null-termination */ |
| 279 | char *progname; | 239 | char *progname = strrchr (buf, '/'); |
| 280 | progname = strrchr (buf, '/'); | ||
| 281 | if (progname) | 240 | if (progname) |
| 282 | { | 241 | progname = progname + 1; /* Skip the '/' */ |
| 283 | progname = progname + 1; /* Skip the '/' */ | ||
| 284 | } | ||
| 285 | else | 242 | else |
| 286 | { | 243 | progname = buf; |
| 287 | progname = buf; | 244 | char *ret = malloc (strlen (progname) + 1); |
| 288 | } | ||
| 289 | char *ret; | ||
| 290 | ret = malloc (strlen (progname) + 1); | ||
| 291 | if (ret) | 245 | if (ret) |
| 292 | { | 246 | { |
| 293 | strcpy (ret, progname); | 247 | strcpy (ret, progname); |
