diff options
author | pgen <p.gen.progs@gmail.com> | 2021-03-08 20:34:40 +0100 |
---|---|---|
committer | pgen <p.gen.progs@gmail.com> | 2021-03-08 20:34:40 +0100 |
commit | f28f9866bad8629fc66ed56ce524464ffb840158 (patch) | |
tree | 930e20c5c2994f9551864669ac69ff46acd948e6 | |
parent | f4f331ef4498d1b944eb75dcb6c4c555973b8d05 (diff) |
Improve the integer verification function.
-rw-r--r-- | smenu.c | 2 | ||||
-rw-r--r-- | utils.c | 22 |
2 files changed, 14 insertions, 10 deletions
@@ -886,7 +886,7 @@ check_integer_constraint(int nb_args, char ** args, char * value, char * par) { if (!is_integer(value)) { - fprintf(stderr, "This argument of %s is not an integer: %s", par, value); + fprintf(stderr, "The argument of %s is not an integer: %s", par, value); return 0; } return 1; @@ -312,17 +312,21 @@ xwcscasecmp(const wchar_t * s1, const wchar_t * s2) return (-*s2); } -/* ====================================== */ -/* Returns 1 if s represents an integer */ -/* else 0. */ -/* s != NULL is assumed. */ -/* 0 and 0x prefixes are not understood. */ -/* ====================================== */ +/* ==================================================================== */ +/* Returns 1 if s can be converted into an integer otherwise returns 0. */ +/* ==================================================================== */ int is_integer(const char * const s) { - char * end; + long int n; - strtol(s, &end, 10); - return (*end == '\0'); + if (!s || (strspn(s, "0123456789 ") != strlen(s))) + return 0; + + n = strtol(s, NULL, 10); + + if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) + return 1; + else + return 0; } |