changeset 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 74aa8d21daa5
children c9d7c2bfa3f1
files ChangeLog lib/getprogname.c
diffstat 2 files changed, 33 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Oct 08 16:53:59 2018 -0700
+++ b/ChangeLog	Thu Oct 11 18:24:51 2018 +0200
@@ -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
--- 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 = "?";
     }