config.c: add git_env_ulong() to parse environment variable

The new function parses an integeral value that fits in unsigned long in human readable form, i.e. possibly with unit suffix, e.g. 10k = 10240, etc., from an environment variable. Parsing of GIT_MMAP_LIMIT and GIT_ALLOC_LIMIT will use it in later patches. Signed-off-by: Steffen Prohaska <prohaska@zib.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Steffen Prohaska committed Aug 26, 2014 at 17:23 UTC 23b0c4782e5f9357e6189253021053a4404ddf4e
2 files changed +17
cache.h
+1
@@ -1318,6 +1318,7 @@ extern int git_config_rename_section_in_file(const char *, const char *, const c
1318 extern const char *git_etc_gitconfig(void);
1319 extern int check_repository_format_version(const char *var, const char *value, void *cb);
1320 extern int git_env_bool(const char *, int);
1321 +extern unsigned long git_env_ulong(const char *, unsigned long);
1322 extern int git_config_system(void);
1323 extern int config_error_nonbool(const char *);
1324 #if defined(__GNUC__)
config.c
+16
@@ -1116,12 +1116,28 @@ const char *git_etc_gitconfig(void)
1116 return system_wide;
1117 }
1118
1119 +/*
1120 + * Parse environment variable 'k' as a boolean (in various
1121 + * possible spellings); if missing, use the default value 'def'.
1122 + */
1123 int git_env_bool(const char *k, int def)
1124 {
1125 const char *v = getenv(k);
1126 return v ? git_config_bool(k, v) : def;
1127 }
1128
1129 +/*
1130 + * Parse environment variable 'k' as ulong with possibly a unit
1131 + * suffix; if missing, use the default value 'val'.
1132 + */
1133 +unsigned long git_env_ulong(const char *k, unsigned long val)
1134 +{
1135 + const char *v = getenv(k);
1136 + if (v && !git_parse_ulong(v, &val))
1137 + die("failed to parse %s", k);
1138 + return val;
1139 +}
1140 +
1141 int git_config_system(void)
1142 {
1143 return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);