diff options
author | Colin Watson <cjwatson@debian.org> | 2019-01-26 14:49:46 +0000 |
---|---|---|
committer | Colin Watson <cjwatson@debian.org> | 2019-01-26 14:49:46 +0000 |
commit | 449ac91e70ad7ff7583eaab5b000de3563148f45 (patch) | |
tree | c96cbc907afa13c26f65f9cd45c049b62b7dd453 | |
parent | 2beae0a82b4a70ddc565960840ebe53142c8746b (diff) |
Use bool type where appropriate
Now that we're using <stdbool.h> anyway due to gl_list (with Gnulib
providing <stdbool.h> if necessary), it makes sense to use it for our
own functions that have essentially boolean semantics.
* lib/encodings.c (compatible_encodings, is_roff_device): Return bool.
* lib/pathsearch.c (pathsearch, pathsearch_executable,
directory_on_path): Likewise.
* lib/sandbox.c (search_ld_preload, can_load_seccomp): Likewise.
* lib/security.c (running_setuid): Likewise.
* lib/wordfnmatch.c (word_fnmatch): Likewise. Update all callers.
* src/check_mandirs.c (sanity_check_db): Likewise.
* src/man.c (duplicate_candidates): Likewise.
* src/manp.c (is_global_mandir): Likewise. Update all callers.
* src/whatis.c (suitable_manpath, match): Likewise.
(any_set, all_set): Likewise. Update all callers.
* lib/encodings.h (is_roff_device): Update prototype.
* lib/pathsearch.h (pathsearch_executable, directory_on_path): Likewise.
* lib/security.h (running_setuid): Likewise.
* lib/wordfnmatch.h (word_fnmatch): Likewise.
* src/manp.h (is_global_mandir): Likewise.
* src/mandb.c (mandb, process_manpath): Change global_manpath parameter
type to bool.
-rw-r--r-- | lib/encodings.c | 21 | ||||
-rw-r--r-- | lib/encodings.h | 4 | ||||
-rw-r--r-- | lib/pathsearch.c | 25 | ||||
-rw-r--r-- | lib/pathsearch.h | 12 | ||||
-rw-r--r-- | lib/sandbox.c | 21 | ||||
-rw-r--r-- | lib/security.c | 5 | ||||
-rw-r--r-- | lib/security.h | 3 | ||||
-rw-r--r-- | lib/wordfnmatch.c | 7 | ||||
-rw-r--r-- | lib/wordfnmatch.h | 4 | ||||
-rw-r--r-- | libdb/db_lookup.c | 3 | ||||
-rw-r--r-- | src/check_mandirs.c | 9 | ||||
-rw-r--r-- | src/man.c | 23 | ||||
-rw-r--r-- | src/mandb.c | 8 | ||||
-rw-r--r-- | src/manp.c | 7 | ||||
-rw-r--r-- | src/manp.h | 4 | ||||
-rw-r--r-- | src/whatis.c | 31 |
16 files changed, 104 insertions, 83 deletions
diff --git a/lib/encodings.c b/lib/encodings.c index ec8fb6bc..c033543f 100644 --- a/lib/encodings.c +++ b/lib/encodings.c @@ -25,6 +25,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <string.h> #include <stdlib.h> #include <unistd.h> @@ -640,27 +641,27 @@ out: /* Can we take this input encoding and produce this output encoding, perhaps * with the help of some iconv pipes? */ -static int compatible_encodings (const char *input, const char *output) +static bool compatible_encodings (const char *input, const char *output) { if (STREQ (input, output)) - return 1; + return true; /* If the input is ASCII, recoding should be easy. Try it. */ if (STREQ (input, "ANSI_X3.4-1968")) - return 1; + return true; /* If the input is UTF-8, it's either a simple recoding of whatever * we want or else it probably won't work at all no matter what we * do. We might as well try it for now. */ if (STREQ (input, "UTF-8")) - return 1; + return true; /* If the output is ASCII, this is probably because the caller * explicitly asked for it, so we have little choice but to try. */ if (STREQ (output, "ANSI_X3.4-1968")) - return 1; + return true; #ifdef MULTIBYTE_GROFF /* Special case for some CJK UTF-8 locales, which take UTF-8 input @@ -673,10 +674,10 @@ static int compatible_encodings (const char *input, const char *output) STREQ (input, "EUC-KR") || STREQ (input, "EUC-TW")) && STREQ (output, "UTF-8")) - return 1; + return true; #endif /* MULTIBYTE_GROFF */ - return 0; + return false; } /* Return the default groff device for the given character set. This may be @@ -723,16 +724,16 @@ const char *get_default_device (const char *charset_from_locale, } /* Is this a known *roff device name? */ -int is_roff_device (const char *device) +bool is_roff_device (const char *device) { const struct device_entry *entry; for (entry = device_table; entry->roff_device; ++entry) { if (STREQ (entry->roff_device, device)) - return 1; + return true; } - return 0; + return false; } /* Find the input encoding expected by groff, and set the LESSCHARSET diff --git a/lib/encodings.h b/lib/encodings.h index 9967a895..b745dae1 100644 --- a/lib/encodings.h +++ b/lib/encodings.h @@ -20,6 +20,8 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include <stdbool.h> + const char *get_groff_preconv (void); char *get_page_encoding (const char *lang); const char *get_source_encoding (const char *lang); @@ -28,7 +30,7 @@ const char *get_locale_charset (void); char *find_charset_locale (const char *charset); const char *get_default_device (const char *locale_charset, const char *source_encoding); -int is_roff_device (const char *device); +bool is_roff_device (const char *device); const char *get_roff_encoding (const char *device, const char *source_encoding); const char *get_output_encoding (const char *device); diff --git a/lib/pathsearch.c b/lib/pathsearch.c index 402f7d1a..c733c3a7 100644 --- a/lib/pathsearch.c +++ b/lib/pathsearch.c @@ -24,6 +24,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <string.h> #include <stdlib.h> @@ -37,26 +38,26 @@ #include "manconfig.h" #include "pathsearch.h" -static int pathsearch (const char *name, const mode_t bits) +static bool pathsearch (const char *name, const mode_t bits) { char *cwd = NULL; char *path = getenv ("PATH"); char *pathtok; const char *element; struct stat st; - int ret = 0; + bool ret = false; if (!path) /* Eh? Oh well. */ - return 0; + return false; if (strchr (name, '/')) { /* Qualified name; look directly. */ if (stat (name, &st) == -1) - return 0; + return false; if (S_ISREG (st.st_mode) && (st.st_mode & bits)) - return 1; - return 0; + return true; + return false; } pathtok = path = xstrdup (path); @@ -81,7 +82,7 @@ static int pathsearch (const char *name, const mode_t bits) free (filename); if (S_ISREG (st.st_mode) && (st.st_mode & bits)) { - ret = 1; + ret = true; break; } } @@ -91,22 +92,22 @@ static int pathsearch (const char *name, const mode_t bits) return ret; } -int pathsearch_executable (const char *name) +bool pathsearch_executable (const char *name) { return pathsearch (name, 0111); } -int directory_on_path (const char *dir) +bool directory_on_path (const char *dir) { char *cwd = NULL; char *path = getenv ("PATH"); char *pathtok; const char *element; - int ret = 0; + bool ret = false; if (!path) /* Eh? Oh well. */ - return 0; + return false; pathtok = path = xstrdup (path); @@ -119,7 +120,7 @@ int directory_on_path (const char *dir) } if (STREQ (element, dir)) { - ret = 1; + ret = true; break; } } diff --git a/lib/pathsearch.h b/lib/pathsearch.h index 970fbeef..d9b024d1 100644 --- a/lib/pathsearch.h +++ b/lib/pathsearch.h @@ -23,12 +23,14 @@ #ifndef PATHSEARCH_H #define PATHSEARCH_H -/* Return non-zero if NAME is found as an executable regular file on the - * $PATH. +#include <stdbool.h> + +/* Return true if NAME is found as an executable regular file on the $PATH, + * otherwise false. */ -int pathsearch_executable (const char *name); +bool pathsearch_executable (const char *name); -/* Return non-zero if DIR matches an entry on the $PATH. */ -int directory_on_path (const char *dir); +/* Return true if DIR matches an entry on the $PATH, otherwise false. */ +bool directory_on_path (const char *dir); #endif /* PATHSEARCH_H */ diff --git a/lib/sandbox.c b/lib/sandbox.c index c097482b..204bda4c 100644 --- a/lib/sandbox.c +++ b/lib/sandbox.c @@ -43,6 +43,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <errno.h> #include <stdlib.h> #include <string.h> @@ -86,14 +87,14 @@ static void gripe_seccomp_filter_unavailable (void) "CONFIG_SECCOMP_FILTER\n"); } -static int search_ld_preload (const char *needle) +static bool search_ld_preload (const char *needle) { const char *ld_preload_env; static char *ld_preload_file = NULL; ld_preload_env = getenv ("LD_PRELOAD"); if (ld_preload_env && strstr (ld_preload_env, needle) != NULL) - return 1; + return true; if (!ld_preload_file) { int fd; @@ -118,9 +119,9 @@ static int search_ld_preload (const char *needle) * for you. */ if (strstr (ld_preload_file, needle) != NULL) - return 1; + return true; - return 0; + return false; } /* Can we load a seccomp filter into this process? @@ -128,20 +129,20 @@ static int search_ld_preload (const char *needle) * This guard allows us to call sandbox_load in code paths that may * conditionally do so again. */ -static int can_load_seccomp (void) +static bool can_load_seccomp (void) { const char *man_disable_seccomp; int seccomp_status; if (seccomp_filter_unavailable) { gripe_seccomp_filter_unavailable (); - return 0; + return false; } man_disable_seccomp = getenv ("MAN_DISABLE_SECCOMP"); if (man_disable_seccomp && *man_disable_seccomp) { debug ("seccomp filter disabled by user request\n"); - return 0; + return false; } /* Valgrind causes the child process to make some system calls we @@ -158,13 +159,13 @@ static int can_load_seccomp (void) if (search_ld_preload ("/vgpreload")) { debug ("seccomp filter disabled while running under " "Valgrind\n"); - return 0; + return false; } seccomp_status = prctl (PR_GET_SECCOMP); if (seccomp_status == 0) - return 1; + return true; if (seccomp_status == -1) { if (errno == EINVAL) @@ -177,7 +178,7 @@ static int can_load_seccomp (void) else debug ("unknown return value from PR_GET_SECCOMP: %d\n", seccomp_status); - return 0; + return false; } #endif /* HAVE_LIBSECCOMP */ diff --git a/lib/security.c b/lib/security.c index e739d503..7d66eaf4 100644 --- a/lib/security.c +++ b/lib/security.c @@ -27,6 +27,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <string.h> #include <stdlib.h> #include <stdio.h> @@ -94,12 +95,12 @@ void init_security (void) #endif /* MAN_OWNER */ } -int running_setuid (void) +bool running_setuid (void) { #ifdef MAN_OWNER return ruid != euid; #else /* !MAN_OWNER */ - return 0; + return false; #endif } diff --git a/lib/security.h b/lib/security.h index 63ef5332..fdfadc4d 100644 --- a/lib/security.h +++ b/lib/security.h @@ -22,6 +22,7 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include <stdbool.h> #include <pwd.h> /* security.c */ @@ -29,7 +30,7 @@ extern void drop_effective_privs (void); extern void regain_effective_privs (void); extern void drop_privs (void *data); extern void init_security (void); -extern int running_setuid (void); +extern bool running_setuid (void); #ifdef MAN_OWNER extern struct passwd *get_man_owner (void); #endif /* MAN_OWNER */ diff --git a/lib/wordfnmatch.c b/lib/wordfnmatch.c index 0ec26500..73fad8e1 100644 --- a/lib/wordfnmatch.c +++ b/lib/wordfnmatch.c @@ -24,6 +24,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <stdlib.h> #include <ctype.h> @@ -37,7 +38,7 @@ /* TODO: How on earth do we allow multiple-word matches without * reimplementing fnmatch()? */ -int word_fnmatch (const char *lowpattern, const char *string) +bool word_fnmatch (const char *lowpattern, const char *string) { char *lowstring = lower (string); char *begin = lowstring, *p; @@ -53,12 +54,12 @@ int word_fnmatch (const char *lowpattern, const char *string) *p = '\0'; if (fnmatch (lowpattern, begin, 0) == 0) { free (lowstring); - return 1; + return true; } begin = p + 1; } } free (lowstring); - return 0; + return false; } diff --git a/lib/wordfnmatch.h b/lib/wordfnmatch.h index 0fc29f0c..82e7cf7b 100644 --- a/lib/wordfnmatch.h +++ b/lib/wordfnmatch.h @@ -20,4 +20,6 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -int word_fnmatch (const char *lowpattern, const char *string); +#include <stdbool.h> + +bool word_fnmatch (const char *lowpattern, const char *string); diff --git a/libdb/db_lookup.c b/libdb/db_lookup.c index 140a5050..09892e93 100644 --- a/libdb/db_lookup.c +++ b/libdb/db_lookup.c @@ -26,6 +26,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <stdio.h> #include <errno.h> #include <ctype.h> @@ -405,7 +406,7 @@ struct mandata *dblookup_pattern (MYDBM_FILE dbf, const char *pattern, #endif /* !BTREE */ struct mandata info; char *tab; - int got_match; + bool got_match; memset (&info, 0, sizeof (info)); diff --git a/src/check_mandirs.c b/src/check_mandirs.c index c8c33dce..b3dbfb4a 100644 --- a/src/check_mandirs.c +++ b/src/check_mandirs.c @@ -31,6 +31,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <string.h> #include <stdlib.h> #include <stdio.h> @@ -652,12 +653,12 @@ int create_db (const char *manpath, const char *catpath) } /* Make sure an existing database is essentially sane. */ -static int sanity_check_db (MYDBM_FILE dbf) +static bool sanity_check_db (MYDBM_FILE dbf) { datum key; if (dbver_rd (dbf)) - return 0; + return false; key = MYDBM_FIRSTKEY (dbf); while (MYDBM_DPTR (key) != NULL) { @@ -668,7 +669,7 @@ static int sanity_check_db (MYDBM_FILE dbf) debug ("warning: %s has a key with no content (%s); " "rebuilding\n", database, MYDBM_DPTR (key)); MYDBM_FREE_DPTR (key); - return 0; + return false; } MYDBM_FREE_DPTR (content); nextkey = MYDBM_NEXTKEY (dbf, key); @@ -676,7 +677,7 @@ static int sanity_check_db (MYDBM_FILE dbf) key = nextkey; } - return 1; + return true; } /* routine to update the db, ensure that it is consistent with the @@ -40,6 +40,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <string.h> #include <stdlib.h> #include <stdio.h> @@ -138,7 +139,7 @@ char *lang; #define TFMT_PROG "mandb_tfmt" #undef ALT_EXT_FORMAT /* allow external formatters located in cat hierarchy */ -static int global_manpath = -1; /* global or user manual page hierarchy? */ +static bool global_manpath; /* global or user manual page hierarchy? */ static int skip; /* page exists but has been skipped */ #if defined _AIX || defined __sgi @@ -2693,25 +2694,25 @@ static int get_ult_flags (char from_db, char id) } /* Is this candidate substantially a duplicate of a previous one? - * Returns non-zero if so, otherwise zero. + * Returns true if so, otherwise false. */ -static int duplicate_candidates (struct candidate *left, - struct candidate *right) +static bool duplicate_candidates (struct candidate *left, + struct candidate *right) { const char *slash1, *slash2; struct locale_bits bits1, bits2; - int ret; + bool ret; if (left->ult && right->ult && STREQ (left->ult, right->ult)) - return 1; /* same ultimate source file */ + return true; /* same ultimate source file */ if (!STREQ (left->source->name, right->source->name) || !STREQ (left->source->sec, right->source->sec) || !STREQ (left->source->ext, right->source->ext)) - return 0; /* different name/section/extension */ + return false; /* different name/section/extension */ if (STREQ (left->path, right->path)) - return 1; /* same path */ + return true; /* same path */ /* Figure out if we've had a sufficiently similar candidate for this * language already. @@ -2721,7 +2722,7 @@ static int duplicate_candidates (struct candidate *left, if (!slash1 || !slash2 || !STRNEQ (left->path, right->path, MAX (slash1 - left->path, slash2 - right->path))) - return 0; /* different path base */ + return false; /* different path base */ unpack_locale_bits (++slash1, &bits1); unpack_locale_bits (++slash2, &bits2); @@ -2729,12 +2730,12 @@ static int duplicate_candidates (struct candidate *left, if (!STREQ (bits1.language, bits2.language) || !STREQ (bits1.territory, bits2.territory) || !STREQ (bits1.modifier, bits2.modifier)) - ret = 0; /* different language/territory/modifier */ + ret = false; /* different language/territory/modifier */ else /* Everything seems to be the same; we can find nothing to * choose between them. */ - ret = 1; + ret = true; free_locale_bits (&bits1); free_locale_bits (&bits2); diff --git a/src/mandb.c b/src/mandb.c index 0ed5748b..9299f891 100644 --- a/src/mandb.c +++ b/src/mandb.c @@ -30,6 +30,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <string.h> #include <stdlib.h> #include <stdio.h> @@ -438,7 +439,8 @@ static void cleanup (void *arg) /* sort out the database names */ static int mandb (struct dbpaths *dbpaths, - const char *catpath, const char *manpath, int global_manpath) + const char *catpath, const char *manpath, + bool global_manpath) { int ret, amount; char *dbname; @@ -552,7 +554,7 @@ static int mandb (struct dbpaths *dbpaths, return amount; } -static int process_manpath (const char *manpath, int global_manpath, +static int process_manpath (const char *manpath, bool global_manpath, struct hashtable *tried_catdirs) { char *catpath; @@ -860,7 +862,7 @@ int main (int argc, char *argv[]) mpiter = gl_list_iterator (manpathlist); while (gl_list_iterator_next (&mpiter, (const void **) &mp, NULL)) { - int global_manpath = is_global_mandir (mp); + bool global_manpath = is_global_mandir (mp); int ret; DIR *dir; struct dirent *subdirent; @@ -47,6 +47,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <stdio.h> #include <ctype.h> #include <sys/types.h> @@ -1368,15 +1369,15 @@ char *get_catpath (const char *name, int cattype) /* Check to see if the supplied man directory is a system-wide mandir. * Obviously, user directories must not be included here. */ -int is_global_mandir (const char *dir) +bool is_global_mandir (const char *dir) { struct list *list; for (list = namestore; list; list = list->next) if (list->flag == MANDB_MAP && STRNEQ (dir, list->key, strlen (list->key))) - return 1; - return 0; + return true; + return false; } /* Accept a manpath (not a full pathname to a file) and return an FSSTND @@ -22,6 +22,8 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include <stdbool.h> + #include "gl_list.h" struct locale_bits { @@ -42,7 +44,7 @@ extern gl_list_t create_pathlist (const char *manp); extern void free_pathlist (gl_list_t list); extern char *get_mandb_manpath (void); extern char *get_catpath (const char *name, int cattype); -extern int is_global_mandir (const char *dir); +extern bool is_global_mandir (const char *dir); extern void read_config_file (int optional); extern const char *get_def (const char *thing, const char *def); extern const char *get_def_user (const char *thing, const char *def); diff --git a/src/whatis.c b/src/whatis.c index 22bbb12b..5bf34190 100644 --- a/src/whatis.c +++ b/src/whatis.c @@ -34,6 +34,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include <stdbool.h> #include <stdlib.h> #include <string.h> #include <stdio.h> @@ -517,23 +518,23 @@ static int do_whatis_section (MYDBM_FILE dbf, return count; } -static int suitable_manpath (const char *manpath, const char *page_dir) +static bool suitable_manpath (const char *manpath, const char *page_dir) { char *page_manp, *pm; gl_list_t page_manpathlist; - int ret; + bool ret; page_manp = get_manpath_from_path (page_dir, 0); if (!page_manp || !*page_manp) { free (page_manp); - return 0; + return false; } pm = locale_manpath (page_manp); free (page_manp); page_manp = pm; page_manpathlist = create_pathlist (page_manp); - ret = gl_list_search (page_manpathlist, manpath) ? 1 : 0; + ret = gl_list_search (page_manpathlist, manpath) ? true : false; free_pathlist (page_manpathlist); free (page_manp); @@ -591,24 +592,24 @@ static void do_whatis (MYDBM_FILE dbf, } } -static int any_set (int num_pages, int *found_here) +static bool any_set (int num_pages, int *found_here) { int i; for (i = 0; i < num_pages; ++i) if (found_here[i]) - return 1; - return 0; + return true; + return false; } -static int all_set (int num_pages, int *found_here) +static bool all_set (int num_pages, int *found_here) { int i; for (i = 0; i < num_pages; ++i) if (!found_here[i]) - return 0; - return 1; + return false; + return true; } static void parse_name (const char * const *pages, int num_pages, @@ -642,8 +643,8 @@ static void parse_name (const char * const *pages, int num_pages, } } -/* return 1 on word match */ -static int match (const char *lowpage, const char *whatis) +/* return true on word match */ +static bool match (const char *lowpage, const char *whatis) { char *lowwhatis = lower (whatis); size_t len = strlen (lowpage); @@ -659,13 +660,13 @@ static int match (const char *lowpage, const char *whatis) if ((p == begin || (!CTYPE (islower, *left) && *left != '_')) && (!*right || (!CTYPE (islower, *right) && *right != '_'))) { free (begin); - return 1; + return true; } lowwhatis = p + 1; } free (begin); - return 0; + return false; } static void parse_whatis (const char * const *pages, char * const *lowpages, @@ -714,7 +715,7 @@ static void do_apropos (MYDBM_FILE dbf, datum key, cont; char **lowpages; int *found_here; - int (*combine) (int, int *); + bool (*combine) (int, int *); int i; #ifndef BTREE datum nextkey; |