[glib/glib-2-30] goption: implement platform_get_argv0() for OpenBSD



commit 0c550d4a61619ab83f571bba2b38e9f55f9ad573
Author: Antoine Jacoutot <ajacoutot gnome org>
Date:   Mon Jan 30 16:17:06 2012 +0100

    goption: implement platform_get_argv0() for OpenBSD
    
    https://bugzilla.gnome.org/show_bug.cgi?id=669024

 glib/goption.c            |   31 ++++++++++++++++++++++++++++++-
 glib/tests/option-argv0.c |    4 ++--
 2 files changed, 32 insertions(+), 3 deletions(-)
---
diff --git a/glib/goption.c b/glib/goption.c
index 799ea09..07a06b5 100644
--- a/glib/goption.c
+++ b/glib/goption.c
@@ -139,6 +139,13 @@
 #include <stdio.h>
 #include <errno.h>
 
+#if defined __OpenBSD__
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#endif
+
 #include "goption.h"
 
 #include "gprintf.h"
@@ -1665,7 +1672,7 @@ free_pending_nulls (GOptionContext *context,
 static char *
 platform_get_argv0 (void)
 {
-#ifdef __linux
+#if defined __linux
   char *cmdline;
   char *base_arg0;
   gsize len;
@@ -1685,6 +1692,28 @@ platform_get_argv0 (void)
   base_arg0 = g_path_get_basename (cmdline);
   g_free (cmdline);
   return base_arg0;
+#elif defined __OpenBSD__
+  char **cmdline = NULL;
+  char *base_arg0;
+  gsize len = PATH_MAX;
+
+  int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
+
+  cmdline = (char **) realloc (cmdline, len);
+
+  if (sysctl (mib, nitems (mib), cmdline, &len, NULL, 0) == -1)
+    {
+      g_free (cmdline);
+      return NULL;
+    }
+
+  /* We could just return cmdline, but I think it's better
+   * to hold on to a smaller malloc block; the arguments
+   * could be large.
+   */
+  base_arg0 = g_path_get_basename (*cmdline);
+  g_free (cmdline);
+  return base_arg0;
 #endif
 
   return NULL;
diff --git a/glib/tests/option-argv0.c b/glib/tests/option-argv0.c
index 31fd83b..525f12f 100644
--- a/glib/tests/option-argv0.c
+++ b/glib/tests/option-argv0.c
@@ -27,7 +27,7 @@
 #include <stdio.h>
 #include <string.h>
 
-#ifdef __linux
+#if defined __linux || defined __OpenBSD__
 static void
 test_platform_argv0 (void)
 {
@@ -55,7 +55,7 @@ main (int   argc,
   /* Note - we can't actually use g_test_* because g_test_init mutates
    * g_get_prgname() which is exactly what we wanted to test =/
    */
-#ifdef __linux
+#if defined __linux || defined __OpenBSD__
   g_print ("/option/argv0: ");
   test_platform_argv0 ();
   g_print ("OK\n");



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]