diff --git a/HISTORY b/HISTORY index 155c536..ae2af2d 100644 --- a/HISTORY +++ b/HISTORY @@ -1,7 +1,8 @@ -Version 1.65 2022-12-30 +Version 1.65 2023-01-04 * locked_list.h: add functions locked_list_move and locked_list_move_tail * add function tcp_socket_connected + * parse_bytes support space charactors (before and after the unit) Version 1.64 2022-11-19 * shared_func.[hc]: normalize_path use type string_t for general purpose diff --git a/src/shared_func.c b/src/shared_func.c index 2650b50..8b186ad 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -2393,7 +2393,7 @@ int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes) int result; pReservedEnd = NULL; - *bytes = strtol(pStr, &pReservedEnd, 10); + *bytes = strtoll(pStr, &pReservedEnd, 10); if (*bytes < 0) { logError("file: "__FILE__", line: %d, " \ @@ -2408,6 +2408,19 @@ int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes) return 0; } + if (*pReservedEnd == ' ' || *pReservedEnd == '\t') + { + do { + ++pReservedEnd; + } while (*pReservedEnd == ' ' || *pReservedEnd == '\t'); + + if (*pReservedEnd == '\0') + { + *bytes *= default_unit_bytes; + return 0; + } + } + if (*pReservedEnd == 'T' || *pReservedEnd == 't') { *bytes *= 1024 * 1024 * 1024 * 1024LL; @@ -2439,16 +2452,34 @@ int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes) { return 0; } - if ((*(pReservedEnd + 1) == 'B' || *(pReservedEnd + 1) == 'b') && - (*(pReservedEnd + 2) == '\0')) + + ++pReservedEnd; + if (*pReservedEnd == 'B' || *pReservedEnd == 'b') { - return 0; + if (*(pReservedEnd + 1) == '\0') + { + return 0; + } + ++pReservedEnd; } + + if (*pReservedEnd == ' ' || *pReservedEnd == '\t') + { + do { + ++pReservedEnd; + } while (*pReservedEnd == ' ' || *pReservedEnd == '\t'); + + if (*pReservedEnd == '\0') + { + return 0; + } + } + result = EINVAL; } logError("file: "__FILE__", line: %d, " - "unkown byte unit: %s, input string: %s", + "unkown byte unit: \"%s\", input string: \"%s\"", __LINE__, pReservedEnd, pStr); return result; }