]> Savannah Git Hosting - gnulib.git/commitdiff
getprogname: Work around program name truncation when possible.
authorBruno Haible <bruno@clisp.org>
Thu, 11 Oct 2018 16:24:51 +0000 (18:24 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 11 Oct 2018 16:24:51 +0000 (18:24 +0200)
* lib/getprogname.c (getprogname) [HP-UX]: When pst_ucomm is truncated,
possibly use pst_cmd instead.

ChangeLog
lib/getprogname.c

index 6161c05b6ccd495957f316a396889a37e4bb78ec..3940fb2ddd3c60064854219c86d322fde596f04e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2018-10-11  Bruno Haible  <bruno@clisp.org>
+
+       getprogname: Work around program name truncation when possible.
+       * lib/getprogname.c (getprogname) [HP-UX]: When pst_ucomm is truncated,
+       possibly use pst_cmd instead.
+
 2018-10-08  Paul Eggert  <eggert@cs.ucla.edu>
 
        fts: cleanup after FTS_NOATIME removal
index 58625de31ba9606c7b303fafb6ffe8dd2cc02129..4f97df491c50f73e70781b2df36a11286bfa6cb4 100644 (file)
@@ -110,9 +110,33 @@ getprogname (void)
       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 = "?";
     }