Please double-check the use of strdup(), to avoid memory leakage.

2016-10-12  Daniel Richard G.  <sk...@iskunk.org>

        getprogname: port to IBM z/OS
        * lib/getprogname.c (getprogname) [__MVS__]: Use getpid,
        w_getpsent and strdup to obtain the program name string.


--Daniel


-- 
Daniel Richard G. || sk...@iskunk.org
My ASCII-art .sig got a bad case of Times New Roman.
diff --git a/lib/getprogname.c b/lib/getprogname.c
index 97a6aef..b12efae 100644
--- a/lib/getprogname.c
+++ b/lib/getprogname.c
@@ -28,6 +28,14 @@
 # include <string.h>
 #endif
 
+#ifdef __MVS__
+# ifndef _OPEN_SYS
+#  define _OPEN_SYS
+# endif
+# include <string.h>
+# include <sys/ps.h>
+#endif
+
 #include "dirname.h"
 
 #ifndef HAVE_GETPROGNAME
@@ -75,6 +83,32 @@ getprogname (void)
         p = "?";
     }
   return p;
+#elif __MVS__
+  /* https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/rtwgetp.htm */
+  char *p = "?";
+  pid_t pid = getpid ();
+  int token;
+  W_PSPROC buf;
+  memset (&buf, 0, sizeof(buf));
+  buf.ps_cmdptr    = (char *) malloc (buf.ps_cmdlen    = PS_CMDBLEN_LONG);
+  buf.ps_conttyptr = (char *) malloc (buf.ps_conttylen = PS_CONTTYBLEN);
+  buf.ps_pathptr   = (char *) malloc (buf.ps_pathlen   = PS_PATHBLEN);
+  if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr)
+    {
+      for (token = 0; token >= 0;
+           token = w_getpsent (token, &buf, sizeof(buf)))
+        {
+          if (token > 0 && buf.ps_pid == pid)
+            {
+              p = strdup (last_component (buf.ps_pathptr));
+              break;
+            }
+        }
+    }
+  if (buf.ps_cmdptr)    free (buf.ps_cmdptr);
+  if (buf.ps_conttyptr) free (buf.ps_conttyptr);
+  if (buf.ps_pathptr)   free (buf.ps_pathptr);
+  return p;
 # else
 #  error "getprogname module not ported to this OS"
 # endif

Reply via email to