summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpgen <p.gen.progs@gmail.com>2021-03-08 20:34:40 +0100
committerpgen <p.gen.progs@gmail.com>2021-03-08 20:34:40 +0100
commitf28f9866bad8629fc66ed56ce524464ffb840158 (patch)
tree930e20c5c2994f9551864669ac69ff46acd948e6
parentf4f331ef4498d1b944eb75dcb6c4c555973b8d05 (diff)
Improve the integer verification function.
-rw-r--r--smenu.c2
-rw-r--r--utils.c22
2 files changed, 14 insertions, 10 deletions
diff --git a/smenu.c b/smenu.c
index 872221d..69847cd 100644
--- a/smenu.c
+++ b/smenu.c
@@ -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;
diff --git a/utils.c b/utils.c
index 6e8f050..80bba23 100644
--- a/utils.c
+++ b/utils.c
@@ -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;
}