Skip to content

Display and control your Android device

Expose sc_get_env() to read environment variable

Romain Vimont131372d2c4b4
diff --git a/app/meson.build b/app/meson.build
index f089ffb1..be02ebc1 100644
--- a/app/meson.build
+++ b/app/meson.build
@@ -46,6 +46,7 @@ src = [
'src/util/acksync.c',
'src/util/audiobuf.c',
'src/util/average.c',
+ 'src/util/env.c',
'src/util/file.c',
'src/util/intmap.c',
'src/util/intr.c',
diff --git a/app/src/icon.c b/app/src/icon.c
index a76a85c9..4f3a9a39 100644
--- a/app/src/icon.c
+++ b/app/src/icon.c
@@ -9,6 +9,7 @@
#include "config.h"
#include "compat.h"
+#include "util/env.h"
#include "util/file.h"
#include "util/log.h"
#include "util/str.h"
@@ -19,35 +20,22 @@
static char *
get_icon_path(void) {
-#ifdef __WINDOWS__
- const wchar_t *icon_path_env = _wgetenv(L"SCRCPY_ICON_PATH");
-#else
- const char *icon_path_env = getenv("SCRCPY_ICON_PATH");
-#endif
- if (icon_path_env) {
+ char *icon_path = sc_get_env("SCRCPY_ICON_PATH");
+ if (icon_path) {
// if the envvar is set, use it
-#ifdef __WINDOWS__
- char *icon_path = sc_str_from_wchars(icon_path_env);
-#else
- char *icon_path = strdup(icon_path_env);
-#endif
- if (!icon_path) {
- LOG_OOM();
- return NULL;
- }
LOGD("Using SCRCPY_ICON_PATH: %s", icon_path);
return icon_path;
}
#ifndef PORTABLE
LOGD("Using icon: " SCRCPY_DEFAULT_ICON_PATH);
- char *icon_path = strdup(SCRCPY_DEFAULT_ICON_PATH);
+ icon_path = strdup(SCRCPY_DEFAULT_ICON_PATH);
if (!icon_path) {
LOG_OOM();
return NULL;
}
#else
- char *icon_path = sc_file_get_local_path(SCRCPY_PORTABLE_ICON_FILENAME);
+ icon_path = sc_file_get_local_path(SCRCPY_PORTABLE_ICON_FILENAME);
if (!icon_path) {
LOGE("Could not get icon path");
return NULL;
diff --git a/app/src/server.c b/app/src/server.c
index 584a3c34..fe55baa2 100644
--- a/app/src/server.c
+++ b/app/src/server.c
@@ -9,6 +9,7 @@
#include "adb/adb.h"
#include "util/binary.h"
+#include "util/env.h"
#include "util/file.h"
#include "util/log.h"
#include "util/net_intr.h"
@@ -25,35 +26,22 @@
static char *
get_server_path(void) {
-#ifdef __WINDOWS__
- const wchar_t *server_path_env = _wgetenv(L"SCRCPY_SERVER_PATH");
-#else
- const char *server_path_env = getenv("SCRCPY_SERVER_PATH");
-#endif
- if (server_path_env) {
+ char *server_path = sc_get_env("SCRCPY_SERVER_PATH");
+ if (server_path) {
// if the envvar is set, use it
-#ifdef __WINDOWS__
- char *server_path = sc_str_from_wchars(server_path_env);
-#else
- char *server_path = strdup(server_path_env);
-#endif
- if (!server_path) {
- LOG_OOM();
- return NULL;
- }
LOGD("Using SCRCPY_SERVER_PATH: %s", server_path);
return server_path;
}
#ifndef PORTABLE
LOGD("Using server: " SC_SERVER_PATH_DEFAULT);
- char *server_path = strdup(SC_SERVER_PATH_DEFAULT);
+ server_path = strdup(SC_SERVER_PATH_DEFAULT);
if (!server_path) {
LOG_OOM();
return NULL;
}
#else
- char *server_path = sc_file_get_local_path(SC_SERVER_FILENAME);
+ server_path = sc_file_get_local_path(SC_SERVER_FILENAME);
if (!server_path) {
LOGE("Could not get local file path, "
"using " SC_SERVER_FILENAME " from current directory");
diff --git a/app/src/util/env.c b/app/src/util/env.c
new file mode 100644
index 00000000..1128e5ea
--- /dev/null
+++ b/app/src/util/env.c
@@ -0,0 +1,29 @@
+#include "env.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include "util/str.h"
+
+char *
+sc_get_env(const char *varname) {
+#ifdef _WIN32
+ wchar_t *w_varname = sc_str_to_wchars(varname);
+ if (!w_varname) {
+ return NULL;
+ }
+ const wchar_t *value = _wgetenv(w_varname);
+ free(w_varname);
+ if (!value) {
+ return NULL;
+ }
+
+ return sc_str_from_wchars(value);
+#else
+ const char *value = getenv(varname);
+ if (!value) {
+ return NULL;
+ }
+
+ return strdup(value);
+#endif
+}
diff --git a/app/src/util/env.h b/app/src/util/env.h
new file mode 100644
index 00000000..50a31165
--- /dev/null
+++ b/app/src/util/env.h
@@ -0,0 +1,12 @@
+#ifndef SC_ENV_H
+#define SC_ENV_H
+
+#include "common.h"
+
+// Return the value of the environment variable (may be NULL).
+//
+// The returned value must be freed by the caller.
+char *
+sc_get_env(const char *varname);
+
+#endif