getprogname: port to AIX
authorJim Meyering <meyering@fb.com>
Thu, 22 Sep 2016 04:15:59 +0000 (21:15 -0700)
committerJim Meyering <meyering@fb.com>
Thu, 22 Sep 2016 15:12:14 +0000 (08:12 -0700)
* lib/getprogname.c (getprogname) [_AIX]: Use getpid, getprocs64
and strdup to obtain a short program name string.  Using code from
Bruno Haible and an idea from Bastien ROUCARIÈS, in
https://lists.gnu.org/archive/html/bug-gnulib/2010-12/msg00249.html
Assaf Gordon reported that this new file would fail to compile on
AIX-7.1 32bit.

ChangeLog
lib/getprogname.c

index 82f4264d34b4988d7bd3748d3b6e5f76ea363280..417de6ab77d226d522400816bbc306a61d3f012c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2016-09-21  Jim Meyering  <meyering@fb.com>
+
+       getprogname: port to AIX
+       * lib/getprogname.c (getprogname) [_AIX]: Use getpid, getprocs64
+       and strdup to obtain a short program name string.  Using code from
+       Bruno Haible and an idea from Bastien ROUCARIÈS, in
+       https://lists.gnu.org/archive/html/bug-gnulib/2010-12/msg00249.html
+       Assaf Gordon reported that this new file would fail to compile on
+       AIX-7.1 32bit.
+
 2016-09-16  Paul Eggert  <eggert@cs.ucla.edu>
 
        extensions: fix typo in comment
index d70c2aa20d4d758d34fda1102926aed38dbdb750..17e6059e1c1360d5cf131bcb04ac20d6f79c0960 100644 (file)
 #include <errno.h> /* get program_invocation_name declaration */
 #include <stdlib.h> /* get __argv declaration */
 
+#ifdef _AIX
+# include <unistd.h>
+# include <procinfo.h>
+# include <string.h>
+#endif
+
 #include "dirname.h"
 
 #ifndef HAVE_GETPROGNAME
@@ -41,6 +47,26 @@ getprogname (void)
 # elif HAVE_DECL___ARGV
   const char *p = __argv && __argv[0] ? __argv[0] : "?";
   return last_component (p);
+# elif _AIX
+  /* Idea by Bastien ROUCARIÈS <address@hidden>,
+     http://lists.gnu.org/archive/html/bug-gnulib/2010-12/msg00095.html
+     Reference: http://
+   ibm.biz/knowctr#ssw_aix_53/com.ibm.aix.basetechref/doc/basetrf1/getprocs.htm
+  */
+  static char *p;
+  static int first = 1;
+  if (first)
+    {
+      first = 0;
+      pid_t pid = getpid ();
+      struct procentry64 procs;
+      p = (0 < getprocs64 (&procs, sizeof procs, NULL, 0, &pid, 1)
+           ? strdup (procs.pi_comm)
+           : NULL);
+      if (!p)
+        p = "?";
+    }
+  return p;
 # else
 #  error "getprogname module not ported to this OS"
 # endif