From ddbabaa3b659bed9dcf5c5a2bfc430fb816277c7 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 15 Oct 2023 18:21:31 +0200 Subject: Replace all old school booleans in lib witch C99 ones diff --git a/lib/extra_opts.c b/lib/extra_opts.c index 89b1056..b9843eb 100644 --- a/lib/extra_opts.c +++ b/lib/extra_opts.c @@ -26,15 +26,14 @@ #include "extra_opts.h" /* FIXME: copied from utils.h; we should move a bunch of libs! */ -int -is_option2 (char *str) +bool is_option2 (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; } /* this is the externally visible function used by plugins */ diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 57e6094..0cc864a 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -131,7 +131,7 @@ np_get_defaults(const char *locator, const char *default_section) if (inifile == NULL) die(STATE_UNKNOWN, _("Can't read config file: %s\n"), strerror(errno)); - if (read_defaults(inifile, i.stanza, &defaults) == FALSE) + if (!read_defaults(inifile, i.stanza, &defaults)) die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file); @@ -157,7 +157,8 @@ np_get_defaults(const char *locator, const char *default_section) static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts) { - int c, status = FALSE; + int c = 0; + bool status = false; size_t i, stanza_len; enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA; @@ -219,7 +220,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) die(STATE_UNKNOWN, "%s\n", _("Config file error")); } - status = TRUE; + status = true; break; } break; diff --git a/lib/tests/test_disk.c b/lib/tests/test_disk.c index 269d20b..e283fe2 100644 --- a/lib/tests/test_disk.c +++ b/lib/tests/test_disk.c @@ -44,19 +44,19 @@ main (int argc, char **argv) plan_tests(33); - ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list"); + ok( np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); np_add_name(&exclude_filesystem, "/var/log"); - ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now"); - ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list"); + ok( np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); + ok( np_find_name(exclude_filesystem, "/home") == false, "/home not in list"); np_add_name(&exclude_filesystem, "/home"); - ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now"); - ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list"); + ok( np_find_name(exclude_filesystem, "/home") == true, "is in list now"); + ok( np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list"); - ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list"); + ok( np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); np_add_name(&exclude_fstype, "iso9660"); - ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now"); + ok( np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); - ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables"); + ok( np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); /* for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { @@ -120,7 +120,7 @@ main (int argc, char **argv) np_add_parameter(&paths, "/home/tonvoon"); np_add_parameter(&paths, "/dev/c2t0d0s0"); - np_set_best_match(paths, dummy_mount_list, FALSE); + np_set_best_match(paths, dummy_mount_list, false); for (p = paths; p; p = p->name_next) { struct mount_entry *temp_me; temp_me = p->best_match; @@ -144,7 +144,7 @@ main (int argc, char **argv) np_add_parameter(&paths, "/home/tonvoon"); np_add_parameter(&paths, "/home"); - np_set_best_match(paths, dummy_mount_list, TRUE); + np_set_best_match(paths, dummy_mount_list, true); for (p = paths; p; p = p->name_next) { if (! strcmp(p->name, "/home/groups")) { ok( ! p->best_match , "/home/groups correctly not found"); diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index a109e3f..01afacd 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c @@ -62,99 +62,99 @@ main (int argc, char **argv) range = parse_range_string("6"); ok( range != NULL, "'6' is valid range"); ok( range->start == 0, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 6, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); free(range); range = parse_range_string("1:12%%"); ok( range != NULL, "'1:12%%' is valid - percentages are ignored"); ok( range->start == 1, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 12, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); free(range); range = parse_range_string("-7:23"); ok( range != NULL, "'-7:23' is valid range"); ok( range->start == -7, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 23, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); free(range); range = parse_range_string(":5.75"); ok( range != NULL, "':5.75' is valid range"); ok( range->start == 0, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 5.75, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); free(range); range = parse_range_string("~:-95.99"); ok( range != NULL, "~:-95.99' is valid range"); - ok( range->start_infinity == TRUE, "Using negative infinity"); + ok( range->start_infinity == true, "Using negative infinity"); ok( range->end == -95.99, "End correct (with rounding errors)"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); free(range); range = parse_range_string("12345678901234567890:"); temp = atof("12345678901234567890"); /* Can't just use this because number too large */ ok( range != NULL, "'12345678901234567890:' is valid range"); ok( range->start == temp, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); - ok( range->end_infinity == TRUE, "Using infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); + ok( range->end_infinity == true, "Using infinity"); /* Cannot do a "-1" on temp, as it appears to be same value */ - ok( check_range(temp/1.1, range) == TRUE, "12345678901234567890/1.1 - alert"); - ok( check_range(temp, range) == FALSE, "12345678901234567890 - no alert"); - ok( check_range(temp*2, range) == FALSE, "12345678901234567890*2 - no alert"); + ok( check_range(temp/1.1, range) == true, "12345678901234567890/1.1 - alert"); + ok( check_range(temp, range) == false, "12345678901234567890 - no alert"); + ok( check_range(temp*2, range) == false, "12345678901234567890*2 - no alert"); free(range); range = parse_range_string("~:0"); ok( range != NULL, "'~:0' is valid range"); - ok( range->start_infinity == TRUE, "Using negative infinity"); + ok( range->start_infinity == true, "Using negative infinity"); ok( range->end == 0, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); ok( range->alert_on == OUTSIDE, "Will alert on outside of this range"); - ok( check_range(0.5, range) == TRUE, "0.5 - alert"); - ok( check_range(-10, range) == FALSE, "-10 - no alert"); - ok( check_range(0, range) == FALSE, "0 - no alert"); + ok( check_range(0.5, range) == true, "0.5 - alert"); + ok( check_range(-10, range) == false, "-10 - no alert"); + ok( check_range(0, range) == false, "0 - no alert"); free(range); - + range = parse_range_string("@0:657.8210567"); ok( range != 0, "@0:657.8210567' is a valid range"); ok( range->start == 0, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 657.8210567, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); ok( range->alert_on == INSIDE, "Will alert on inside of this range" ); - ok( check_range(32.88, range) == TRUE, "32.88 - alert"); - ok( check_range(-2, range) == FALSE, "-2 - no alert"); - ok( check_range(657.8210567, range) == TRUE, "657.8210567 - alert"); - ok( check_range(0, range) == TRUE, "0 - alert"); + ok( check_range(32.88, range) == true, "32.88 - alert"); + ok( check_range(-2, range) == false, "-2 - no alert"); + ok( check_range(657.8210567, range) == true, "657.8210567 - alert"); + ok( check_range(0, range) == true, "0 - alert"); free(range); range = parse_range_string("@1:1"); ok( range != NULL, "'@1:1' is a valid range"); ok( range->start == 1, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 1, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); ok( range->alert_on == INSIDE, "Will alert on inside of this range" ); - ok( check_range(0.5, range) == FALSE, "0.5 - no alert"); - ok( check_range(1, range) == TRUE, "1 - alert"); - ok( check_range(5.2, range) == FALSE, "5.2 - no alert"); + ok( check_range(0.5, range) == false, "0.5 - no alert"); + ok( check_range(1, range) == true, "1 - alert"); + ok( check_range(5.2, range) == false, "5.2 - no alert"); free(range); range = parse_range_string("1:1"); ok( range != NULL, "'1:1' is a valid range"); ok( range->start == 1, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 1, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); - ok( check_range(0.5, range) == TRUE, "0.5 - alert"); - ok( check_range(1, range) == FALSE, "1 - no alert"); - ok( check_range(5.2, range) == TRUE, "5.2 - alert"); + ok( range->end_infinity == false, "Not using infinity"); + ok( check_range(0.5, range) == true, "0.5 - alert"); + ok( check_range(1, range) == false, "1 - no alert"); + ok( check_range(5.2, range) == true, "5.2 - alert"); free(range); range = parse_range_string("2:1"); @@ -459,7 +459,7 @@ main (int argc, char **argv) ok(this_monitoring_plugin==NULL, "Free'd this_monitoring_plugin"); - ok(mp_suid() == FALSE, "Test aren't suid"); + ok(mp_suid() == false, "Test aren't suid"); /* base states with random case */ char *states[] = { diff --git a/lib/utils_base.c b/lib/utils_base.c index eabcd7e..3c7221c 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -40,7 +40,7 @@ monitoring_plugin *this_monitoring_plugin=NULL; unsigned int timeout_state = STATE_CRITICAL; unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; -int _np_state_read_file(FILE *); +bool _np_state_read_file(FILE *); void np_init( char *plugin_name, int argc, char **argv ) { if (this_monitoring_plugin==NULL) { @@ -105,12 +105,12 @@ die (int result, const char *fmt, ...) void set_range_start (range *this, double value) { this->start = value; - this->start_infinity = FALSE; + this->start_infinity = false; } void set_range_end (range *this, double value) { this->end = value; - this->end_infinity = FALSE; + this->end_infinity = false; } range @@ -124,9 +124,9 @@ range /* Set defaults */ temp_range->start = 0; - temp_range->start_infinity = FALSE; + temp_range->start_infinity = false; temp_range->end = 0; - temp_range->end_infinity = TRUE; + temp_range->end_infinity = true; temp_range->alert_on = OUTSIDE; temp_range->text = strdup(str); @@ -138,7 +138,7 @@ range end_str = index(str, ':'); if (end_str != NULL) { if (str[0] == '~') { - temp_range->start_infinity = TRUE; + temp_range->start_infinity = true; } else { start = strtod(str, NULL); /* Will stop at the ':' */ set_range_start(temp_range, start); @@ -152,8 +152,8 @@ range set_range_end(temp_range, end); } - if (temp_range->start_infinity == TRUE || - temp_range->end_infinity == TRUE || + if (temp_range->start_infinity == true || + temp_range->end_infinity == true || temp_range->start <= temp_range->end) { return temp_range; } @@ -223,31 +223,30 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) { printf("\n"); } -/* Returns TRUE if alert should be raised based on the range */ -int -check_range(double value, range *my_range) +/* Returns true if alert should be raised based on the range */ +bool check_range(double value, range *my_range) { - int no = FALSE; - int yes = TRUE; + bool no = false; + bool yes = true; if (my_range->alert_on == INSIDE) { - no = TRUE; - yes = FALSE; + no = true; + yes = false; } - if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) { + if (my_range->end_infinity == false && my_range->start_infinity == false) { if ((my_range->start <= value) && (value <= my_range->end)) { return no; } else { return yes; } - } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) { + } else if (my_range->start_infinity == false && my_range->end_infinity == true) { if (my_range->start <= value) { return no; } else { return yes; } - } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) { + } else if (my_range->start_infinity == true && my_range->end_infinity == false) { if (value <= my_range->end) { return no; } else { @@ -263,12 +262,12 @@ int get_status(double value, thresholds *my_thresholds) { if (my_thresholds->critical != NULL) { - if (check_range(value, my_thresholds->critical) == TRUE) { + if (check_range(value, my_thresholds->critical) == true) { return STATE_CRITICAL; } } if (my_thresholds->warning != NULL) { - if (check_range(value, my_thresholds->warning) == TRUE) { + if (check_range(value, my_thresholds->warning) == true) { return STATE_WARNING; } } @@ -465,7 +464,7 @@ char* _np_state_calculate_location_prefix(){ /* Do not allow passing MP_STATE_PATH in setuid plugins * for security reasons */ - if (mp_suid() == FALSE) { + if (!mp_suid()) { env_dir = getenv("MP_STATE_PATH"); if(env_dir && env_dir[0] != '\0') return env_dir; @@ -541,7 +540,7 @@ void np_enable_state(char *keyname, int expected_data_version) { state_data *np_state_read() { state_data *this_state_data=NULL; FILE *statefile; - int rc = FALSE; + bool rc = false; if(this_monitoring_plugin==NULL) die(STATE_UNKNOWN, _("This requires np_init to be called")); @@ -563,7 +562,7 @@ state_data *np_state_read() { fclose(statefile); } - if(rc==FALSE) { + if(!rc) { _cleanup_state_data(); } @@ -573,8 +572,8 @@ state_data *np_state_read() { /* * Read the state file */ -int _np_state_read_file(FILE *f) { - int status=FALSE; +bool _np_state_read_file(FILE *f) { + bool status = false; size_t pos; char *line; int i; @@ -628,7 +627,7 @@ int _np_state_read_file(FILE *f) { if(this_monitoring_plugin->state->state_data->data==NULL) die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); expected=STATE_DATA_END; - status=TRUE; + status=true; break; case STATE_DATA_END: ; diff --git a/lib/utils_base.h b/lib/utils_base.h index 9abf595..80b8743 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h @@ -21,7 +21,7 @@ typedef struct range_struct { double start; - int start_infinity; /* FALSE (default) or TRUE */ + bool start_infinity; double end; int end_infinity; int alert_on; /* OUTSIDE (default) or INSIDE */ @@ -61,7 +61,7 @@ range *parse_range_string (char *); int _set_thresholds(thresholds **, char *, char *); void set_thresholds(thresholds **, char *, char *); void print_thresholds(const char *, thresholds *); -int check_range(double, range *); +bool check_range(double, range *); int get_status(double, thresholds *); /* Handle timeouts */ diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 5e95aef..483be06 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -170,9 +170,7 @@ np_find_parameter(struct parameter_list *list, const char *name) return NULL; } -void -np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact) -{ +void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { struct parameter_list *d; for (d = desired; d; d= d->name_next) { if (! d->best_match) { @@ -195,9 +193,9 @@ np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list if (! best_match) { for (me = mount_list; me; me = me->me_next) { size_t len = strlen (me->me_mountdir); - if ((exact == FALSE && (best_match_len <= len && len <= name_len && + if ((!exact && (best_match_len <= len && len <= name_len && (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0))) - || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0)) + || (exact && strcmp(me->me_mountdir, d->name)==0)) { if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { best_match = me; @@ -216,27 +214,23 @@ np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list } } -/* Returns TRUE if name is in list */ -int -np_find_name (struct name_list *list, const char *name) -{ +/* Returns true if name is in list */ +bool np_find_name (struct name_list *list, const char *name) { const struct name_list *n; if (list == NULL || name == NULL) { - return FALSE; + return false; } for (n = list; n; n = n->next) { if (!strcmp(name, n->name)) { - return TRUE; + return true; } } - return FALSE; + return false; } -/* Returns TRUE if name is in list */ -bool -np_find_regmatch (struct regex_list *list, const char *name) -{ +/* Returns true if name is in list */ +bool np_find_regmatch (struct regex_list *list, const char *name) { int len; regmatch_t m; @@ -257,25 +251,20 @@ np_find_regmatch (struct regex_list *list, const char *name) return false; } -int -np_seen_name(struct name_list *list, const char *name) -{ +bool np_seen_name(struct name_list *list, const char *name) { const struct name_list *s; for (s = list; s; s=s->next) { if (!strcmp(s->name, name)) { - return TRUE; + return true; } } - return FALSE; + return false; } -int -np_regex_match_mount_entry (struct mount_entry* me, regex_t* re) -{ +bool np_regex_match_mount_entry (struct mount_entry* me, regex_t* re) { if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 || regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) { - return TRUE; - } else { - return FALSE; + return true; } + return false; } diff --git a/lib/utils_disk.h b/lib/utils_disk.h index 6b83ac7..5b2caf2 100644 --- a/lib/utils_disk.h +++ b/lib/utils_disk.h @@ -39,14 +39,14 @@ struct parameter_list }; void np_add_name (struct name_list **list, const char *name); -int np_find_name (struct name_list *list, const char *name); -int np_seen_name (struct name_list *list, const char *name); +bool np_find_name (struct name_list *list, const char *name); +bool np_seen_name (struct name_list *list, const char *name); int np_add_regex (struct regex_list **list, const char *regex, int cflags); bool np_find_regmatch (struct regex_list *list, const char *name); struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); - + int search_parameter_list (struct parameter_list *list, const char *name); -void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact); -int np_regex_match_mount_entry (struct mount_entry* me, regex_t* re); +void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact); +bool np_regex_match_mount_entry (struct mount_entry* me, regex_t* re); -- cgit v0.10-9-g596f