diff 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
line wrap: on
line diff
--- a/lib/getprogname.c	Mon Oct 08 16:53:59 2018 -0700
+++ b/lib/getprogname.c	Thu Oct 11 18:24:51 2018 +0200
@@ -110,9 +110,33 @@
       first = 0;
       pid_t pid = getpid ();
       struct pst_status status;
-      p = (0 < pstat_getproc (&status, sizeof status, 0, pid)
-           ? strdup (status.pst_ucomm)
-           : NULL);
+      if (pstat_getproc (&status, sizeof status, 0, pid) > 0)
+        {
+          if (strlen (status.pst_ucomm) < PST_UCOMMLEN - 1)
+            p = status.pst_ucomm;
+          else
+            {
+              /* status.pst_ucomm is truncated to length PST_UCOMMLEN - 1.
+                 Look at status.pst_cmd instead.  */
+              char *space = strchr (status.pst_cmd, ' ');
+              if (space != NULL)
+                *space = '\0';
+              p = strrchr (status.pst_cmd, '/');
+              if (p != NULL)
+                p++;
+              else
+                p = status.pst_cmd;
+              if (strlen (p) > PST_UCOMMLEN - 1
+                  && memcmp (p, status.pst_ucomm, PST_UCOMMLEN - 1) == 0)
+                /* p is less truncated than status.pst_ucomm.  */
+                ;
+              else
+                p = status.pst_ucomm;
+            }
+          p = strdup (p);
+        }
+      else
+        p = NULL;
       if (!p)
         p = "?";
     }