From 369d98cc3640a5db78305c8fb0aefdd7cd6cc9f0 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 18 Oct 2023 20:51:23 +0200 Subject: plugins/utils: Use C99 booleans diff --git a/plugins/utils.c b/plugins/utils.c index 71c0bdd..7e14b6e 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -147,98 +147,80 @@ print_revision (const char *command_name, const char *revision) command_name, revision, PACKAGE, VERSION); } -int -is_numeric (char *number) -{ +bool is_numeric (char *number) { char tmp[1]; float x; if (!number) - return FALSE; + return false; else if (sscanf (number, "%f%c", &x, tmp) == 1) - return TRUE; + return true; else - return FALSE; + return false; } -int -is_positive (char *number) -{ +bool is_positive (char *number) { if (is_numeric (number) && atof (number) > 0.0) - return TRUE; + return true; else - return FALSE; + return false; } -int -is_negative (char *number) -{ +bool is_negative (char *number) { if (is_numeric (number) && atof (number) < 0.0) - return TRUE; + return true; else - return FALSE; + return false; } -int -is_nonnegative (char *number) -{ +bool is_nonnegative (char *number) { if (is_numeric (number) && atof (number) >= 0.0) - return TRUE; + return true; else - return FALSE; + return false; } -int -is_percentage (char *number) -{ +bool is_percentage (char *number) { int x; if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100) - return TRUE; + return true; else - return FALSE; + return false; } -int -is_integer (char *number) -{ +bool is_integer (char *number) { long int n; if (!number || (strspn (number, "-0123456789 ") != strlen (number))) - return FALSE; + return false; n = strtol (number, NULL, 10); if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) - return TRUE; + return true; else - return FALSE; + return false; } -int -is_intpos (char *number) -{ +bool is_intpos (char *number) { if (is_integer (number) && atoi (number) > 0) - return TRUE; + return true; else - return FALSE; + return false; } -int -is_intneg (char *number) -{ +bool is_intneg (char *number) { if (is_integer (number) && atoi (number) < 0) - return TRUE; + return true; else - return FALSE; + return false; } -int -is_intnonneg (char *number) -{ +bool is_intnonneg (char *number) { if (is_integer (number) && atoi (number) >= 0) - return TRUE; + return true; else - return FALSE; + return false; } /* @@ -246,19 +228,19 @@ is_intnonneg (char *number) * On success the number will be written to the _target_ address, if _target_ is not set * to NULL. */ -int is_int64(char *number, int64_t *target) { +bool is_int64(char *number, int64_t *target) { errno = 0; uint64_t tmp = strtoll(number, NULL, 10); if (errno != 0) { - return 0; + return false; } if (tmp < INT64_MIN || tmp > INT64_MAX) { - return 0; + return false; } if (target != NULL) { *target = tmp; } - return 1; + return true; } /* @@ -266,40 +248,36 @@ int is_int64(char *number, int64_t *target) { * On success the number will be written to the _target_ address, if _target_ is not set * to NULL. */ -int is_uint64(char *number, uint64_t *target) { +bool is_uint64(char *number, uint64_t *target) { errno = 0; uint64_t tmp = strtoll(number, NULL, 10); if (errno != 0) { - return 0; + return false; } if (tmp < 0 || tmp > UINT64_MAX) { - return 0; + return false; } if (target != NULL) { *target = tmp; } - return 1; + return true; } -int -is_intpercent (char *number) -{ +bool is_intpercent (char *number) { int i; if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100) - return TRUE; + return true; else - return FALSE; + return false; } -int -is_option (char *str) -{ +bool is_option (char *str) { if (!str) - return FALSE; + return false; else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) - return TRUE; + return true; else - return FALSE; + return false; } #ifdef NEED_GETTIMEOFDAY diff --git a/plugins/utils.h b/plugins/utils.h index cb979ce..62e489b 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -16,6 +16,8 @@ suite of plugins. */ /* now some functions etc are being defined in ../lib/utils_base.c */ #include "utils_base.h" +#include + #ifdef NP_EXTRA_OPTS /* Include extra-opts functions if compiled in */ @@ -34,21 +36,21 @@ extern time_t start_time, end_time; /* Test input types */ -int is_integer (char *); -int is_intpos (char *); -int is_intneg (char *); -int is_intnonneg (char *); -int is_intpercent (char *); -int is_uint64(char *number, uint64_t *target); -int is_int64(char *number, int64_t *target); - -int is_numeric (char *); -int is_positive (char *); -int is_negative (char *); -int is_nonnegative (char *); -int is_percentage (char *); - -int is_option (char *); +bool is_integer (char *); +bool is_intpos (char *); +bool is_intneg (char *); +bool is_intnonneg (char *); +bool is_intpercent (char *); +bool is_uint64(char *number, uint64_t *target); +bool is_int64(char *number, int64_t *target); + +bool is_numeric (char *); +bool is_positive (char *); +bool is_negative (char *); +bool is_nonnegative (char *); +bool is_percentage (char *); + +bool is_option (char *); /* Generalized timer that will do milliseconds if available */ #ifndef HAVE_STRUCT_TIMEVAL -- cgit v0.10-9-g596f