From: Jordan Lee Date: Sun, 21 Sep 2014 18:05:14 +0000 (+0000) Subject: (trunk) #4160: mike.dld patch: 4160-07-env.patch X-Git-Tag: 2.90~356 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=be290162c2a3ca3e6d09402f2f6e3457ff8841fd;p=transmission (trunk) #4160: mike.dld patch: 4160-07-env.patch --- diff --git a/daemon/remote.c b/daemon/remote.c index c2ccdefb0..36a2e9274 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -1908,13 +1908,11 @@ processArgs (const char * rpcurl, int argc, const char ** argv) 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; diff --git a/libtransmission/announcer-http.c b/libtransmission/announcer-http.c index cc754a35b..330ad5c69 100644 --- a/libtransmission/announcer-http.c +++ b/libtransmission/announcer-http.c @@ -9,7 +9,6 @@ #include /* USHRT_MAX */ #include /* fprintf () */ -#include /* getenv () */ #include /* strchr (), memcmp (), memcpy () */ #include @@ -211,7 +210,7 @@ on_announce_done (tr_session * session, 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"); @@ -366,7 +365,7 @@ on_scrape_done (tr_session * session, 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"); diff --git a/libtransmission/log.c b/libtransmission/log.c index 98c451793..aeccd304a 100644 --- a/libtransmission/log.c +++ b/libtransmission/log.c @@ -10,7 +10,6 @@ #include #include #include -#include /* getenv() */ #include @@ -66,11 +65,7 @@ tr_logGetFile (void) 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) { diff --git a/libtransmission/platform.c b/libtransmission/platform.c index 46a613d9c..efdb397c6 100644 --- a/libtransmission/platform.c +++ b/libtransmission/platform.c @@ -247,7 +247,7 @@ getHomeDir (void) if (!home) { - home = tr_strdup (getenv ("HOME")); + home = tr_env_get_string ("HOME", NULL); if (!home) { @@ -320,11 +320,9 @@ tr_getDefaultConfigDir (const char * appname) 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); @@ -337,10 +335,15 @@ tr_getDefaultConfigDir (const char * appname) 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 } } @@ -355,17 +358,18 @@ tr_getDefaultDownloadDir (void) 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); @@ -428,15 +432,10 @@ tr_getWebClientDir (const tr_session * session UNUSED) 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. */ @@ -522,26 +521,28 @@ tr_getWebClientDir (const tr_session * session UNUSED) #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) { @@ -550,7 +551,7 @@ tr_getWebClientDir (const tr_session * session UNUSED) { if ((end - tmp) > 1) tr_list_append (&candidates, tr_strndup (tmp, end - tmp)); - tmp = end + 1; + tmp = (char *) end + 1; } else if (tmp && *tmp) { diff --git a/libtransmission/tr-dht.c b/libtransmission/tr-dht.c index 4a44b1253..49c95908a 100644 --- a/libtransmission/tr-dht.c +++ b/libtransmission/tr-dht.c @@ -275,7 +275,7 @@ tr_dhtInit (tr_session *ss) 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); diff --git a/libtransmission/utils-test.c b/libtransmission/utils-test.c index 9f33e8b15..281adfdee 100644 --- a/libtransmission/utils-test.c +++ b/libtransmission/utils-test.c @@ -10,6 +10,13 @@ #include /* INT_MAX */ #include /* sqrt () */ #include /* strlen () */ +#include /* setenv (), unsetenv () */ + +#ifdef _WIN32 + #include + #define setenv(key, value, unused) SetEnvironmentVariableA (key, value) + #define unsetenv(key) SetEnvironmentVariableA (key, NULL) +#endif #include "transmission.h" #include "ConvertUTF.h" /* tr_utf8_validate*/ @@ -478,6 +485,51 @@ test_strdup_printf (void) 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) { @@ -495,7 +547,8 @@ main (void) test_strstrip, test_truncd, test_url, - test_utf8 }; + test_utf8, + test_env }; return runTests (tests, NUM_TESTS (tests)); } diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 733dbeff0..75d9316f0 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -24,7 +24,7 @@ #include /* localeconv () */ #include /* pow (), fabs (), floor () */ #include -#include +#include /* getenv () */ #include /* strerror (), memset (), memmem () */ #include /* nanosleep () */ @@ -40,7 +40,7 @@ #ifdef _WIN32 #include #define WINVER WindowsXP /* freeaddrinfo (), getaddrinfo (), getnameinfo () */ - #include /* Sleep (), GetSystemTimeAsFileTime () */ + #include /* Sleep (), GetSystemTimeAsFileTime (), GetEnvironmentVariable () */ #endif #include "transmission.h" @@ -1749,3 +1749,98 @@ tr_formatter_get_units (void * vdict) 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 +} diff --git a/libtransmission/utils.h b/libtransmission/utils.h index d88d663dc..8c2c23f6b 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -486,6 +486,21 @@ void tr_formatter_get_units (void * dict); **** ***/ +/** @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 diff --git a/libtransmission/web.c b/libtransmission/web.c index 2e9575a8f..1dfacf01b 100644 --- a/libtransmission/web.c +++ b/libtransmission/web.c @@ -9,7 +9,6 @@ #include #include /* strlen (), strstr () */ -#include /* getenv () */ #ifdef _WIN32 #include @@ -101,7 +100,7 @@ struct tr_web { 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; @@ -387,9 +386,9 @@ tr_webThreadFunc (void * vsession) 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", @@ -522,6 +521,7 @@ tr_webThreadFunc (void * vsession) 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; diff --git a/utils/show.c b/utils/show.c index 4b6b6ea3c..dabfe524f 100644 --- a/utils/show.c +++ b/utils/show.c @@ -9,7 +9,7 @@ #include /* fprintf () */ #include /* strcmp (), strchr (), memcmp () */ -#include /* getenv (), qsort () */ +#include /* qsort () */ #include #define CURL_DISABLE_TYPECHECK /* otherwise -Wunreachable-code goes insane */ @@ -188,7 +188,7 @@ tr_curl_easy_init (struct evbuffer * writebuf) 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; }