From 93b9d361ce8b6325d489e243b2a3f22bc0fd53e6 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@fb.com>
Date: Wed, 21 Sep 2016 21:15:59 -0700
Subject: [PATCH] getprogname: port to AIX
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* 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         | 10 ++++++++++
 lib/getprogname.c | 27 +++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 82f4264..417de6a 100644
--- 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
diff --git a/lib/getprogname.c b/lib/getprogname.c
index d70c2aa..69c49ba 100644
--- a/lib/getprogname.c
+++ b/lib/getprogname.c
@@ -22,6 +22,12 @@
 #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,27 @@ 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
-- 
2.7.4

