$OpenBSD: patch-glib_goption_c,v 1.1 2012/02/02 07:10:18 ajacoutot Exp $

From e43a98c00091f5e293d2d9d72df2c04081802abe Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@gnome.org>
Date: Mon, 30 Jan 2012 15:17:06 +0000
Subject: goption: implement platform_get_argv0() for OpenBSD

--- glib/goption.c.orig	Tue Aug 16 02:51:30 2011
+++ glib/goption.c	Mon Jan 30 15:58:16 2012
@@ -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;
@@ -1683,6 +1690,28 @@ platform_get_argv0 (void)
    * could be large.
    */
   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
