tweaks: rename two variables, and always pass a valid result back

What is the point of parsing a number when you're not interested in
the result?  All callers of parse_num() pass a container for it.
master
Benno Schulenberg 2017-01-11 19:15:45 +01:00
parent 16a7fd4bfc
commit ea40765904
1 changed files with 7 additions and 10 deletions

View File

@ -85,25 +85,22 @@ int digits(ssize_t n)
} }
#endif #endif
/* Read a ssize_t from str, and store it in *val (if val is not NULL). /* Read an integer from str. If it parses okay, store it in *result
* On error, we return FALSE and don't change *val. Otherwise, we * and return TRUE; otherwise, return FALSE. */
* return TRUE. */ bool parse_num(const char *str, ssize_t *result)
bool parse_num(const char *str, ssize_t *val)
{ {
char *first_error; char *first_error;
ssize_t j; ssize_t value;
/* The manual page for strtol() says this is required, and /* The manual page for strtol() says this is required. */
* it looks like it is! */
errno = 0; errno = 0;
j = (ssize_t)strtol(str, &first_error, 10); value = (ssize_t)strtol(str, &first_error, 10);
if (errno == ERANGE || *str == '\0' || *first_error != '\0') if (errno == ERANGE || *str == '\0' || *first_error != '\0')
return FALSE; return FALSE;
if (val != NULL) *result = value;
*val = j;
return TRUE; return TRUE;
} }