summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGisle Vanem <gisle.vanem@gmail.com>2017-12-17 17:26:10 -0500
committerJay Satiro <raysatiro@yahoo.com>2017-12-26 02:01:48 -0500
commit859ac3602159ef3df77ac4cb1ae191b069ff1b5b (patch)
tree80eae8e7fa9d6505c16190422be22c5b8ce324df /src
parentb399b04902c43f479b51b142505700edcfc34271 (diff)
downloadcurl-859ac3602159ef3df77ac4cb1ae191b069ff1b5b.tar.gz
tool_getparam: Support size modifiers for --max-filesize
- Move the size modifier detection code from limit-rate to its own function so that it can also be used with max-filesize. Size modifiers are the suffixes such as G (gigabyte), M (megabyte) etc. For example --max-filesize 1G Ref: https://curl.haxx.se/mail/archive-2017-12/0000.html Closes https://github.com/curl/curl/pull/2179
Diffstat (limited to 'src')
-rw-r--r--src/tool_getparam.c99
1 files changed, 64 insertions, 35 deletions
diff --git a/src/tool_getparam.c b/src/tool_getparam.c
index 12e3abd55..3f4046417 100644
--- a/src/tool_getparam.c
+++ b/src/tool_getparam.c
@@ -424,6 +424,58 @@ GetFileAndPassword(char *nextarg, char **file, char **password)
cleanarg(nextarg);
}
+/* Get a size parameter for '--limit-rate' or '--max-filesize'.
+ * We support a 'G', 'M' or 'K' suffix too.
+ */
+static ParameterError GetSizeParameter(struct GlobalConfig *global,
+ const char *arg,
+ const char *which,
+ curl_off_t *value_out)
+{
+ char *unit;
+ curl_off_t value;
+
+ if(curlx_strtoofft(arg, &unit, 0, &value)) {
+ warnf(global, "invalid number specified for %s\n", which);
+ return PARAM_BAD_USE;
+ }
+
+ if(!*unit)
+ unit = (char *)"b";
+ else if(strlen(unit) > 1)
+ unit = (char *)"w"; /* unsupported */
+
+ switch(*unit) {
+ case 'G':
+ case 'g':
+ if(value > (CURL_OFF_T_MAX / (1024*1024*1024)))
+ return PARAM_NUMBER_TOO_LARGE;
+ value *= 1024*1024*1024;
+ break;
+ case 'M':
+ case 'm':
+ if(value > (CURL_OFF_T_MAX / (1024*1024)))
+ return PARAM_NUMBER_TOO_LARGE;
+ value *= 1024*1024;
+ break;
+ case 'K':
+ case 'k':
+ if(value > (CURL_OFF_T_MAX / 1024))
+ return PARAM_NUMBER_TOO_LARGE;
+ value *= 1024;
+ break;
+ case 'b':
+ case 'B':
+ /* for plain bytes, leave as-is */
+ break;
+ default:
+ warnf(global, "unsupported %s unit. Use G, M, K or B!\n", which);
+ return PARAM_BAD_USE;
+ }
+ *value_out = value;
+ return PARAM_OK;
+}
+
ParameterError getparameter(const char *flag, /* f or -long-flag */
char *nextarg, /* NULL if unset */
bool *usedarg, /* set to TRUE if the arg
@@ -589,40 +641,11 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
break;
case 'i': /* --limit-rate */
{
- /* We support G, M, K too */
- char *unit;
curl_off_t value;
- if(curlx_strtoofft(nextarg, &unit, 0, &value)) {
- warnf(global, "unsupported rate\n");
- return PARAM_BAD_USE;
- }
+ ParameterError pe = GetSizeParameter(global, nextarg, "rate", &value);
- if(!*unit)
- unit = (char *)"b";
- else if(strlen(unit) > 1)
- unit = (char *)"w"; /* unsupported */
-
- switch(*unit) {
- case 'G':
- case 'g':
- value *= 1024*1024*1024;
- break;
- case 'M':
- case 'm':
- value *= 1024*1024;
- break;
- case 'K':
- case 'k':
- value *= 1024;
- break;
- case 'b':
- case 'B':
- /* for plain bytes, leave as-is */
- break;
- default:
- warnf(global, "unsupported rate unit. Use G, M, K or B!\n");
- return PARAM_BAD_USE;
- }
+ if(pe != PARAM_OK)
+ return pe;
config->recvpersecond = value;
config->sendpersecond = value;
}
@@ -753,9 +776,15 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
return PARAM_LIBCURL_DOESNT_SUPPORT;
break;
case 'y': /* --max-filesize */
- err = str2offset(&config->max_filesize, nextarg);
- if(err)
- return err;
+ {
+ curl_off_t value;
+ ParameterError pe =
+ GetSizeParameter(global, nextarg, "max-filesize", &value);
+
+ if(pe != PARAM_OK)
+ return pe;
+ config->max_filesize = value;
+ }
break;
case 'z': /* --disable-eprt */
config->disable_eprt = toggle;