break;
case 810: /* authenv */
+ auth = tr_env_get_string ("TR_AUTH", NULL);
+ if (auth == NULL)
{
- char *authenv = getenv ("TR_AUTH");
- if (!authenv) {
- fprintf (stderr, "The TR_AUTH environment variable is not set\n");
- exit (0);
- }
- auth = tr_strdup (authenv);
+ fprintf (stderr, "The TR_AUTH environment variable is not set\n");
+ exit (0);
}
break;
#include <limits.h> /* USHRT_MAX */
#include <stdio.h> /* fprintf () */
-#include <stdlib.h> /* getenv () */
#include <string.h> /* strchr (), memcmp (), memcpy () */
#include <event2/buffer.h>
tr_variant benc;
const bool variant_loaded = !tr_variantFromBenc (&benc, msg, msglen);
- if (getenv ("TR_CURL_VERBOSE") != NULL)
+ if (tr_env_key_exists ("TR_CURL_VERBOSE"))
{
if (!variant_loaded)
fprintf (stderr, "%s", "Announce response was not in benc format\n");
const char * str;
const bool variant_loaded = !tr_variantFromBenc (&top, msg, msglen);
- if (getenv ("TR_CURL_VERBOSE") != NULL)
+ if (tr_env_key_exists ("TR_CURL_VERBOSE"))
{
if (!variant_loaded)
fprintf (stderr, "%s", "Scrape response was not in benc format\n");
#include <assert.h>
#include <errno.h>
#include <stdio.h>
-#include <stdlib.h> /* getenv() */
#include <event2/buffer.h>
if (!initialized)
{
- int fd = 0;
- const char * str = getenv ("TR_DEBUG_FD");
-
- if (str && *str)
- fd = atoi (str);
+ const int fd = tr_env_get_int ("TR_DEBUG_FD", 0);
switch (fd)
{
if (!home)
{
- home = tr_strdup (getenv ("HOME"));
+ home = tr_env_get_string ("HOME", NULL);
if (!home)
{
if (!s)
{
- if ((s = getenv ("TRANSMISSION_HOME")))
- {
- s = tr_strdup (s);
- }
- else
+ s = tr_env_get_string ("TRANSMISSION_HOME", NULL);
+
+ if (s == NULL)
{
#ifdef __APPLE__
s = tr_buildPath (getHomeDir (), "Library", "Application Support", appname, NULL);
find_directory (B_USER_SETTINGS_DIRECTORY, -1, true, buf, sizeof (buf));
s = tr_buildPath (buf, appname, NULL);
#else
- if ((s = getenv ("XDG_CONFIG_HOME")))
- s = tr_buildPath (s, appname, NULL);
+ if ((s = tr_env_get_string ("XDG_CONFIG_HOME", NULL)))
+ {
+ s = tr_buildPath (s, appname, NULL);
+ tr_free (s);
+ }
else
- s = tr_buildPath (getHomeDir (), ".config", appname, NULL);
+ {
+ s = tr_buildPath (getHomeDir (), ".config", appname, NULL);
+ }
#endif
}
}
if (user_dir == NULL)
{
- const char * config_home;
+ char * config_home;
char * config_file;
char * content;
size_t content_len;
/* figure out where to look for user-dirs.dirs */
- config_home = getenv ("XDG_CONFIG_HOME");
+ config_home = tr_env_get_string ("XDG_CONFIG_HOME", NULL);
if (config_home && *config_home)
config_file = tr_buildPath (config_home, "user-dirs.dirs", NULL);
else
config_file = tr_buildPath (getHomeDir (), ".config", "user-dirs.dirs", NULL);
+ tr_free (config_home);
/* read in user-dirs.dirs and look for the download dir entry */
content = (char *) tr_loadFile (config_file, &content_len);
if (!s)
{
- if ((s = getenv ("CLUTCH_HOME")))
- {
- s = tr_strdup (s);
- }
- else if ((s = getenv ("TRANSMISSION_WEB_HOME")))
- {
- s = tr_strdup (s);
- }
- else
+ s = tr_env_get_string ("CLUTCH_HOME", NULL);
+ if (s == NULL)
+ s = tr_env_get_string ("TRANSMISSION_WEB_HOME", NULL);
+ if (s == NULL)
{
#ifdef BUILD_MAC_CLIENT /* on Mac, look in the Application Support folder first, then in the app bundle. */
#else /* everyone else, follow the XDG spec */
tr_list *candidates = NULL, *l;
- const char * tmp;
+ char * tmp;
/* XDG_DATA_HOME should be the first in the list of candidates */
- tmp = getenv ("XDG_DATA_HOME");
+ tmp = tr_env_get_string ("XDG_DATA_HOME", NULL);
if (tmp && *tmp)
{
- tr_list_append (&candidates, tr_strdup (tmp));
+ tr_list_append (&candidates, tmp);
}
else
{
char * dhome = tr_buildPath (getHomeDir (), ".local", "share", NULL);
tr_list_append (&candidates, dhome);
+ tr_free (tmp);
}
/* XDG_DATA_DIRS are the backup directories */
{
const char * pkg = PACKAGE_DATA_DIR;
- const char * xdg = getenv ("XDG_DATA_DIRS");
+ char * xdg = tr_env_get_string ("XDG_DATA_DIRS", NULL);
const char * fallback = "/usr/local/share:/usr/share";
char * buf = tr_strdup_printf ("%s:%s:%s", (pkg?pkg:""), (xdg?xdg:""), fallback);
+ tr_free (xdg);
tmp = buf;
while (tmp && *tmp)
{
{
if ((end - tmp) > 1)
tr_list_append (&candidates, tr_strndup (tmp, end - tmp));
- tmp = end + 1;
+ tmp = (char *) end + 1;
}
else if (tmp && *tmp)
{
tr_logAddNamedDbg ("DHT", "Initializing DHT");
- if (getenv ("TR_DHT_VERBOSE") != NULL)
+ if (tr_env_key_exists ("TR_DHT_VERBOSE"))
dht_debug = stderr;
dat_file = tr_buildPath (ss->configDir, "dht.dat", NULL);
#include <limits.h> /* INT_MAX */
#include <math.h> /* sqrt () */
#include <string.h> /* strlen () */
+#include <stdlib.h> /* setenv (), unsetenv () */
+
+#ifdef _WIN32
+ #include <windows.h>
+ #define setenv(key, value, unused) SetEnvironmentVariableA (key, value)
+ #define unsetenv(key) SetEnvironmentVariableA (key, NULL)
+#endif
#include "transmission.h"
#include "ConvertUTF.h" /* tr_utf8_validate*/
return 0;
}
+static int
+test_env (void)
+{
+ const char * test_key = "TR_TEST_ENV";
+ int x;
+ char * s;
+
+ unsetenv (test_key);
+
+ check (!tr_env_key_exists (test_key));
+ x = tr_env_get_int (test_key, 123);
+ check_int_eq (123, x);
+ s = tr_env_get_string (test_key, NULL);
+ check (s == NULL);
+ s = tr_env_get_string (test_key, "a");
+ check_streq ("a", s);
+ tr_free (s);
+
+ setenv (test_key, "", 1);
+
+ check (tr_env_key_exists (test_key));
+ x = tr_env_get_int (test_key, 456);
+ check_int_eq (456, x);
+ s = tr_env_get_string (test_key, NULL);
+ check_streq ("", s);
+ tr_free (s);
+ s = tr_env_get_string (test_key, "b");
+ check_streq ("", s);
+ tr_free (s);
+
+ setenv (test_key, "135", 1);
+
+ check (tr_env_key_exists (test_key));
+ x = tr_env_get_int (test_key, 789);
+ check_int_eq (135, x);
+ s = tr_env_get_string (test_key, NULL);
+ check_streq ("135", s);
+ tr_free (s);
+ s = tr_env_get_string (test_key, "c");
+ check_streq ("135", s);
+ tr_free (s);
+
+ return 0;
+}
+
int
main (void)
{
test_strstrip,
test_truncd,
test_url,
- test_utf8 };
+ test_utf8,
+ test_env };
return runTests (tests, NUM_TESTS (tests));
}
#include <locale.h> /* localeconv () */
#include <math.h> /* pow (), fabs (), floor () */
#include <stdio.h>
-#include <stdlib.h>
+#include <stdlib.h> /* getenv () */
#include <string.h> /* strerror (), memset (), memmem () */
#include <time.h> /* nanosleep () */
#ifdef _WIN32
#include <w32api.h>
#define WINVER WindowsXP /* freeaddrinfo (), getaddrinfo (), getnameinfo () */
- #include <windows.h> /* Sleep (), GetSystemTimeAsFileTime () */
+ #include <windows.h> /* Sleep (), GetSystemTimeAsFileTime (), GetEnvironmentVariable () */
#endif
#include "transmission.h"
tr_variantListAddStr (l, speed_units.units[i].name);
}
+/***
+**** ENVIRONMENT
+***/
+
+bool
+tr_env_key_exists (const char * key)
+{
+ assert (key != NULL);
+
+#ifdef _WIN32
+
+ return GetEnvironmentVariableA (key, NULL, 0) != 0;
+
+#else
+
+ return getenv (key) != NULL;
+
+#endif
+}
+
+int
+tr_env_get_int (const char * key,
+ int default_value)
+{
+#ifdef _WIN32
+
+ char value[16];
+
+ assert (key != NULL);
+
+ if (GetEnvironmentVariableA (key, value, ARRAYSIZE (value)) > 1)
+ return atoi (value);
+
+#else
+
+ const char * value;
+
+ assert (key != NULL);
+
+ value = getenv (key);
+
+ if (value != NULL && *value != '\0')
+ return atoi (value);
+
+#endif
+
+ return default_value;
+}
+
+char * tr_env_get_string (const char * key,
+ const char * default_value)
+{
+#ifdef _WIN32
+
+ wchar_t * wide_key;
+ char * value = NULL;
+
+ wide_key = tr_win32_utf8_to_native (key, -1);
+ if (wide_key != NULL)
+ {
+ const DWORD size = GetEnvironmentVariableW (wide_key, NULL, 0);
+ if (size != 0)
+ {
+ wchar_t * const wide_value = tr_new (wchar_t, size);
+ if (GetEnvironmentVariableW (wide_key, wide_value, size) == size - 1)
+ value = tr_win32_native_to_utf8 (wide_value, size);
+
+ tr_free (wide_value);
+ }
+
+ tr_free (wide_key);
+ }
+
+ if (value == NULL && default_value != NULL)
+ value = tr_strdup (default_value);
+
+ return value;
+
+#else
+
+ char * value;
+
+ assert (key != NULL);
+
+ value = getenv (key);
+ if (value == NULL)
+ value = (char *) default_value;
+
+ if (value != NULL)
+ value = tr_strdup (value);
+
+ return value;
+
+#endif
+}
****
***/
+/** @brief Check if environment variable exists. */
+bool tr_env_key_exists (const char * key);
+
+/** @brief Get environment variable value as int. */
+int tr_env_get_int (const char * key,
+ int default_value);
+
+/** @brief Get environment variable value as string (should be freed afterwards). */
+char * tr_env_get_string (const char * key,
+ const char * default_value);
+
+/***
+****
+***/
+
#ifdef __cplusplus
}
#endif
#include <assert.h>
#include <string.h> /* strlen (), strstr () */
-#include <stdlib.h> /* getenv () */
#ifdef _WIN32
#include <ws2tcpip.h>
{
bool curl_verbose;
bool curl_ssl_verify;
- const char * curl_ca_bundle;
+ char * curl_ca_bundle;
int close_mode;
struct tr_web_task * tasks;
tr_lock * taskLock;
web->close_mode = ~0;
web->taskLock = tr_lockNew ();
web->tasks = NULL;
- web->curl_verbose = getenv ("TR_CURL_VERBOSE") != NULL;
- web->curl_ssl_verify = getenv ("TR_CURL_SSL_VERIFY") != NULL;
- web->curl_ca_bundle = getenv ("CURL_CA_BUNDLE");
+ web->curl_verbose = tr_env_key_exists ("TR_CURL_VERBOSE");
+ web->curl_ssl_verify = tr_env_key_exists ("TR_CURL_SSL_VERIFY");
+ web->curl_ca_bundle = tr_env_get_string ("CURL_CA_BUNDLE", NULL);
if (web->curl_ssl_verify)
{
tr_logAddNamedInfo ("web", "will verify tracker certs using envvar CURL_CA_BUNDLE: %s",
tr_list_free (&paused_easy_handles, NULL);
curl_multi_cleanup (multi);
tr_lockFree (web->taskLock);
+ tr_free (web->curl_ca_bundle);
tr_free (web->cookie_filename);
tr_free (web);
session->web = NULL;
#include <stdio.h> /* fprintf () */
#include <string.h> /* strcmp (), strchr (), memcmp () */
-#include <stdlib.h> /* getenv (), qsort () */
+#include <stdlib.h> /* qsort () */
#include <time.h>
#define CURL_DISABLE_TYPECHECK /* otherwise -Wunreachable-code goes insane */
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writeFunc);
curl_easy_setopt (curl, CURLOPT_WRITEDATA, writebuf);
curl_easy_setopt (curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- curl_easy_setopt (curl, CURLOPT_VERBOSE, getenv ("TR_CURL_VERBOSE") != NULL);
+ curl_easy_setopt (curl, CURLOPT_VERBOSE, tr_env_key_exists ("TR_CURL_VERBOSE"));
curl_easy_setopt (curl, CURLOPT_ENCODING, "");
return curl;
}