Skip to content

Commit 572795c

Browse files
ttaylorrgitster
authored andcommitted
http.c: introduce set_long_from_env() for convenience
In 7059cd9 (http_init(): Fix config file parsing, 2009-03-09), http.c gained a new "set_from_env()" function as a convenience function around conditionally assigning an environment variable to some variable if and only if the environment variable was set to begin with. But prior to 7059cd9, there were two spots which need to first strtol() whatever is set in the environment before assigning it to a long pointer. Both instances stored the result of getenv() in a temporary variable, and conditionally strtol() it depending on whether or not getenv() returned NULL. Replace those two instances with a new cousin of 'set_from_env()' called 'set_long_from_env()', which does what its name suggests. This allows us to remove the temporary variables and clean up some minor code duplication while also adding more robust error handling. More importantly, however, it prepares us for a future commit which will introduce more instances of assigning an environment variable to a long. Signed-off-by: Taylor Blau <[email protected]> Acked-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 894221d commit 572795c

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

http.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,10 +1256,30 @@ static void set_from_env(char **var, const char *envname)
12561256
}
12571257
}
12581258

1259+
static void set_long_from_env(long *var, const char *envname)
1260+
{
1261+
const char *val = getenv(envname);
1262+
if (val) {
1263+
long tmp;
1264+
char *endp;
1265+
int saved_errno = errno;
1266+
1267+
errno = 0;
1268+
tmp = strtol(val, &endp, 10);
1269+
1270+
if (errno)
1271+
warning_errno(_("failed to parse %s"), envname);
1272+
else if (*endp || endp == val)
1273+
warning(_("failed to parse %s"), envname);
1274+
else
1275+
*var = tmp;
1276+
1277+
errno = saved_errno;
1278+
}
1279+
}
1280+
12591281
void http_init(struct remote *remote, const char *url, int proactive_auth)
12601282
{
1261-
char *low_speed_limit;
1262-
char *low_speed_time;
12631283
char *normalized_url;
12641284
struct urlmatch_config config = URLMATCH_CONFIG_INIT;
12651285

@@ -1338,12 +1358,8 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
13381358

13391359
set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
13401360

1341-
low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1342-
if (low_speed_limit)
1343-
curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1344-
low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1345-
if (low_speed_time)
1346-
curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1361+
set_long_from_env(&curl_low_speed_limit, "GIT_HTTP_LOW_SPEED_LIMIT");
1362+
set_long_from_env(&curl_low_speed_time, "GIT_HTTP_LOW_SPEED_TIME");
13471363

13481364
if (curl_ssl_verify == -1)
13491365
curl_ssl_verify = 1;

0 commit comments

Comments
 (0)