orc: Implement a windows helper for getenv()
authorNirbheek Chauhan <nirbheek@centricular.com>
Tue, 30 Jun 2020 10:00:35 +0000 (15:30 +0530)
committerNirbheek Chauhan <nirbheek@centricular.com>
Tue, 30 Jun 2020 11:50:24 +0000 (17:20 +0530)
On Windows, getenv() is deprecated and does not work in all cases. On
the Universal Windows Platform (UWP) it always returns NULL. Add
a wrapper orc_getenv() that calls GetEnvironmentVariable on Windows.

Also change semantics to always make a copy before returning.

Part-of: <https://gitlab.freedesktop.org/gstreamer/orc/-/merge_requests/42>

orc/orc.c
orc/orccompiler.c
orc/orcdebug.c
orc/orcinternal.h

index 2acf522..7b4808c 100644 (file)
--- a/orc/orc.c
+++ b/orc/orc.c
@@ -5,6 +5,11 @@
 #include <string.h>
 #include <stdlib.h>
 
+#ifdef _WIN32
+#include <windows.h>
+#include <processenv.h>
+#endif
+
 #include <orc/orcprogram.h>
 #include <orc/orcdebug.h>
 #include <orc/orconce.h>
@@ -83,3 +88,40 @@ orc_version_string (void)
 {
   return (const char *) VERSION;
 }
+
+/* getenv() is deprecated on Windows and always returns NULL on UWP */
+#ifdef _WIN32
+char*
+_orc_getenv (const char *key)
+{
+  int len;
+  char check[1], *value;
+
+  /* Get the len */
+  len = GetEnvironmentVariableA (key, check, 1);
+  if (len == 0)
+    /* env var is not set or is "" (empty string) */
+    return NULL;
+
+  /* max size of len is 32767, cannot overflow */
+  value = malloc (sizeof (value) * len);
+
+  if (GetEnvironmentVariableA (key, value, len) != (len - 1)) {
+    free (value);
+    return NULL;
+  }
+
+  return value;
+}
+#else
+char*
+_orc_getenv (const char *key)
+{
+  char *value = getenv (key);
+
+  if (value)
+    value = strdup (value);
+
+  return value;
+}
+#endif
index 8d92cbe..de2555d 100644 (file)
@@ -51,11 +51,12 @@ int _orc_compiler_flag_randomize;
 void
 _orc_compiler_init (void)
 {
-  const char *envvar;
+  char *envvar;
 
-  envvar = getenv ("ORC_CODE");
+  envvar = _orc_getenv ("ORC_CODE");
   if (envvar != NULL) {
     _orc_compiler_flag_list = strsplit (envvar, ',');
+    free (envvar);
   }
 
   _orc_compiler_flag_backup = orc_compiler_flag_check ("backup");
index 2e69464..1cefd07 100644 (file)
@@ -29,6 +29,7 @@
 #include "config.h"
 #endif
 #include <orc/orcdebug.h>
+#include <orc/orcinternal.h>
 
 #include <stdio.h>
 #include <string.h>
@@ -55,9 +56,9 @@ static OrcDebugPrintFunc _orc_debug_print_func = orc_debug_print_valist;
 void
 _orc_debug_init(void)
 {
-  const char *envvar;
+  char *envvar;
 
-  envvar = getenv ("ORC_DEBUG");
+  envvar = _orc_getenv ("ORC_DEBUG");
   if (envvar != NULL) {
     char *end = NULL;
     int level;
@@ -65,6 +66,7 @@ _orc_debug_init(void)
     if (end > envvar) {
       _orc_debug_level = level;
     }
+    free (envvar);
   }
 
   ORC_INFO ("orc-" VERSION " debug init");
index cd778c3..b86f698 100644 (file)
@@ -36,6 +36,8 @@ extern const char *_orc_cpu_name;
 
 void orc_compiler_emit_invariants (OrcCompiler *compiler);
 int orc_program_has_float (OrcCompiler *compiler);
+
+char* _orc_getenv (const char *var);
 #endif
 
 ORC_END_DECLS