diff options
Diffstat (limited to 'src/librc-misc.c')
-rw-r--r-- | src/librc-misc.c | 96 |
1 files changed, 58 insertions, 38 deletions
diff --git a/src/librc-misc.c b/src/librc-misc.c index b0d654ca..bd92e584 100644 --- a/src/librc-misc.c +++ b/src/librc-misc.c @@ -57,18 +57,18 @@ char *rc_xstrdup (const char *str) } librc_hidden_def(rc_xstrdup) -int rc_is_env (const char *var, const char *val) +bool rc_is_env (const char *var, const char *val) { char *v; if (! var) - return (-1); + return (false); v = getenv (var); if (! v) - return (val == NULL ? 0 : -1); + return (val == NULL ? true : false); - return (strcasecmp (v, val)); + return (strcasecmp (v, val) == 0 ? true : false); } librc_hidden_def(rc_is_env) @@ -123,58 +123,78 @@ char *rc_strcatpaths (const char *path1, const char *paths, ...) } librc_hidden_def(rc_strcatpaths) -int rc_exists (const char *pathname) +bool rc_exists (const char *pathname) { struct stat buf; if (! pathname) - return (-1); + return (false); + + if (stat (pathname, &buf) == 0) + return (true); - return (stat (pathname, &buf)); + errno = 0; + return (false); } librc_hidden_def(rc_exists) -int rc_is_file (const char *pathname) +bool rc_is_file (const char *pathname) { struct stat buf; - if (pathname && stat (pathname, &buf) == 0) - return (S_ISREG (buf.st_mode) ? 0 : -1); + if (! pathname) + return (false); - return (-1); + if (stat (pathname, &buf) == 0) + return (S_ISREG (buf.st_mode)); + + errno = 0; + return (false); } librc_hidden_def(rc_is_file) -int rc_is_dir (const char *pathname) +bool rc_is_dir (const char *pathname) { struct stat buf; - if (pathname && stat (pathname, &buf) == 0) - return (S_ISDIR (buf.st_mode) ? 0 : -1); + if (! pathname) + return (false); + + if (stat (pathname, &buf) == 0) + return (S_ISDIR (buf.st_mode)); - return (-1); + errno = 0; + return (false); } librc_hidden_def(rc_is_dir) -int rc_is_link (const char *pathname) +bool rc_is_link (const char *pathname) { struct stat buf; - if (pathname && lstat (pathname, &buf) == 0) - return (S_ISLNK (buf.st_mode) ? 0 : -1); + if (! pathname) + return (false); + + if (lstat (pathname, &buf) == 0) + return (S_ISLNK (buf.st_mode)); - return (-1); + errno = 0; + return (false); } librc_hidden_def(rc_is_link) -int rc_is_exec (const char *pathname) +bool rc_is_exec (const char *pathname) { struct stat buf; - if (pathname && lstat (pathname, &buf) == 0) - return (buf.st_mode & S_IXUGO ? 0 : -1); + if (! pathname) + return (false); + + if (lstat (pathname, &buf) == 0) + return (buf.st_mode & S_IXUGO); - return (-1); + errno = 0; + return (false); } librc_hidden_def(rc_is_exec) @@ -199,9 +219,9 @@ char **rc_ls_dir (const char *dir, int options) int l = strlen (d->d_name); char *init = rc_strcatpaths (RC_INITDIR, d->d_name, (char *) NULL); - int ok = rc_exists (init); + bool ok = rc_exists (init); free (init); - if (ok != 0) + if (! ok) continue; /* .sh files are not init scripts */ @@ -225,17 +245,17 @@ char **rc_ls_dir (const char *dir, int options) } librc_hidden_def(rc_ls_dir) -int rc_rm_dir (const char *pathname, bool top) +bool rc_rm_dir (const char *pathname, bool top) { DIR *dp; struct dirent *d; if (! pathname) - return (-1); + return (false); if ((dp = opendir (pathname)) == NULL) { eerror ("failed to opendir `%s': %s", pathname, strerror (errno)); - return (-1); + return (false); } errno = 0; @@ -243,18 +263,18 @@ int rc_rm_dir (const char *pathname, bool top) if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) { char *tmp = rc_strcatpaths (pathname, d->d_name, (char *) NULL); if (d->d_type == DT_DIR) { - if (rc_rm_dir (tmp, true) != 0) + if (! rc_rm_dir (tmp, true)) { free (tmp); closedir (dp); - return (-1); + return (false); } } else { if (unlink (tmp)) { eerror ("failed to unlink `%s': %s", tmp, strerror (errno)); free (tmp); closedir (dp); - return (-1); + return (false); } } free (tmp); @@ -266,10 +286,10 @@ int rc_rm_dir (const char *pathname, bool top) if (top && rmdir (pathname) != 0) { eerror ("failed to rmdir `%s': %s", pathname, strerror (errno)); - return (-1); + return false; } - return (0); + return (true); } librc_hidden_def(rc_rm_dir) @@ -442,7 +462,7 @@ char **rc_filter_env (void) if (! whitelist) return (NULL); - if (rc_is_file (PROFILE_ENV) == 0) + if (rc_is_file (PROFILE_ENV)) profile = rc_get_config (PROFILE_ENV); STRLIST_FOREACH (whitelist, env_name, count) { @@ -524,7 +544,7 @@ static bool file_regex (const char *file, const char *regex) bool retval = false; int result; - if (rc_exists (file) != 0) + if (! rc_exists (file)) return (false); if (! (fp = fopen (file, "r"))) { @@ -572,7 +592,7 @@ char **rc_make_env (void) /* Don't trust environ for softlevel yet */ snprintf (buffer, PATH_MAX, "%s.%s", RC_CONFIG, runlevel); - if (rc_exists (buffer) == 0) + if (rc_exists (buffer)) config = rc_get_config (buffer); else config = rc_get_config (RC_CONFIG); @@ -619,7 +639,7 @@ char **rc_make_env (void) rc_strlist_add (&env, line); free (line); - if (rc_exists (RC_KSOFTLEVEL) == 0) { + if (rc_exists (RC_KSOFTLEVEL)) { if (! (fp = fopen (RC_KSOFTLEVEL, "r"))) eerror ("fopen `%s': %s", RC_KSOFTLEVEL, strerror (errno)); else { @@ -645,7 +665,7 @@ char **rc_make_env (void) We store this special system in RC_SYS so our scripts run fast */ memset (sys, 0, sizeof (sys)); - if (rc_is_dir ("/proc/xen") == 0) { + if (rc_is_dir ("/proc/xen")) { fp = fopen ("/proc/xen/capabilities", "r"); if (fp) { fclose (fp); |