From 8a7b58a73d0ef5a9a7f583bf73b1dc917f2a29de Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Wed, 26 Sep 2007 07:05:33 +0000 Subject: rc_is_env is now rc_env_bool and just works with boolean values. --- src/fstabinfo.c | 2 +- src/librc-misc.c | 25 +++++++++++++++++++------ src/librc.h | 2 +- src/mountinfo.c | 2 +- src/rc.c | 12 ++++++------ src/rc.h | 9 +++++---- src/runscript.c | 22 +++++++++++----------- src/start-stop-daemon.c | 2 +- 8 files changed, 45 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/fstabinfo.c b/src/fstabinfo.c index ab00cd19..85640ccf 100644 --- a/src/fstabinfo.c +++ b/src/fstabinfo.c @@ -181,7 +181,7 @@ int fstabinfo (int argc, char **argv) } /* No point in outputting if quiet */ - if (rc_is_env ("RC_QUIET", "yes")) + if (rc_env_bool ("RC_QUIET")) continue; switch (output) { diff --git a/src/librc-misc.c b/src/librc-misc.c index bd92e584..f2ecd165 100644 --- a/src/librc-misc.c +++ b/src/librc-misc.c @@ -57,20 +57,33 @@ char *rc_xstrdup (const char *str) } librc_hidden_def(rc_xstrdup) -bool rc_is_env (const char *var, const char *val) +bool rc_env_bool (const char *var) { char *v; if (! var) return (false); - v = getenv (var); - if (! v) - return (val == NULL ? true : false); + if (! (v = getenv (var))) { + errno = ENOENT; + return (false); + } + + if (strcasecmp (var, "true") == 0 || + strcasecmp (var, "y") == 0 || + strcasecmp (var, "yes") == 0 || + strcasecmp (var, "1") == 0) + return (true); + + if (strcasecmp (var, "false") != 0 && + strcasecmp (var, "n") != 0 && + strcasecmp (var, "no") != 0 && + strcasecmp (var, "0") != 0) + errno = EINVAL; - return (strcasecmp (v, val) == 0 ? true : false); + return (false); } -librc_hidden_def(rc_is_env) +librc_hidden_def(rc_env_bool) char *rc_strcatpaths (const char *path1, const char *paths, ...) { diff --git a/src/librc.h b/src/librc.h index 397113c6..4ba76524 100644 --- a/src/librc.h +++ b/src/librc.h @@ -50,6 +50,7 @@ #define librc_hidden_def(x) hidden_def(x) librc_hidden_proto(rc_allow_plug) +librc_hidden_proto(rc_env_bool) librc_hidden_proto(rc_exists) librc_hidden_proto(rc_filter_env) librc_hidden_proto(rc_find_pids) @@ -64,7 +65,6 @@ librc_hidden_proto(rc_get_runlevel) librc_hidden_proto(rc_get_runlevels) librc_hidden_proto(rc_get_service_option) librc_hidden_proto(rc_is_dir) -librc_hidden_proto(rc_is_env) librc_hidden_proto(rc_is_exec) librc_hidden_proto(rc_is_file) librc_hidden_proto(rc_is_link) diff --git a/src/mountinfo.c b/src/mountinfo.c index b57e7d55..92f2275e 100644 --- a/src/mountinfo.c +++ b/src/mountinfo.c @@ -364,7 +364,7 @@ int mountinfo (int argc, char **argv) continue; if (skip_point_regex && regexec (skip_point_regex, n, 0, NULL, 0) == 0) continue; - if (! rc_is_env ("RC_QUIET", "yes")) + if (rc_env_bool ("RC_QUIET")) printf ("%s\n", n); result = EXIT_SUCCESS; } diff --git a/src/rc.c b/src/rc.c index da805ccb..9b142076 100644 --- a/src/rc.c +++ b/src/rc.c @@ -457,7 +457,7 @@ static bool want_interactive (void) strcmp (PREVLEVEL, "1") != 0) return (false); - if (! rc_is_env ("RC_INTERACTIVE", "yes")) + if (! rc_env_bool ("RC_INTERACTIVE")) return (false); c = read_key (false); @@ -919,7 +919,7 @@ int main (int argc, char **argv) ecolor (ecolor_good), uts.sysname, ecolor (ecolor_bracket), ecolor (ecolor_normal)); - if (rc_is_env ("RC_INTERACTIVE", "yes")) + if (rc_env_bool ("RC_INTERACTIVE")) printf ("Press %sI%s to enter interactive boot mode\n\n", ecolor (ecolor_good), ecolor (ecolor_normal)); @@ -1056,7 +1056,7 @@ int main (int argc, char **argv) if (newlevel && strcmp (newlevel, bootlevel) == 0 && (strcmp (runlevel, RC_LEVEL_SINGLE) == 0 || strcmp (runlevel, RC_LEVEL_SYSINIT) == 0) && - rc_is_env ("RC_COLDPLUG", "yes")) + rc_env_bool ("RC_COLDPLUG")) { #if defined(__DragonFly__) || defined(__FreeBSD__) /* The net interfaces are easy - they're all in net /dev/net :) */ @@ -1186,7 +1186,7 @@ int main (int argc, char **argv) /* We always stop the service when in these runlevels */ if (going_down) { pid_t pid = rc_stop_service (service); - if (pid > 0 && ! rc_is_env ("RC_PARALLEL", "yes")) + if (pid > 0 && ! rc_env_bool ("RC_PARALLEL")) rc_waitpid (pid); continue; } @@ -1251,7 +1251,7 @@ int main (int argc, char **argv) /* After all that we can finally stop the blighter! */ if (! found) { pid_t pid = rc_stop_service (service); - if (pid > 0 && ! rc_is_env ("RC_PARALLEL", "yes")) + if (pid > 0 && ! rc_env_bool ("RC_PARALLEL")) rc_waitpid (pid); } } @@ -1352,7 +1352,7 @@ interactive_option: if ((pid = rc_start_service (service))) add_pid (pid); - if (! rc_is_env ("RC_PARALLEL", "yes")) { + if (! rc_env_bool ("RC_PARALLEL")) { rc_waitpid (pid); remove_pid (pid); } diff --git a/src/rc.h b/src/rc.h index 324d13fa..4004afad 100644 --- a/src/rc.h +++ b/src/rc.h @@ -367,11 +367,12 @@ char *rc_xstrdup (const char *str); * @param paths NULL terminated list of paths to add * @return pointer to the new path */ char *rc_strcatpaths (const char *path1, const char *paths, ...) SENTINEL; -/*! Check if an environment variable matches the given value +/*! Check if an environment variable is a boolean and return it's value. + * If variable is not a boolean then we set errno to be ENOENT when it does + * not exist or EINVAL if it's not a boolean. * @param variable to check - * @param value it should be - * @return true if it matches */ -bool rc_is_env (const char *variable, const char *value); + * @return true if it matches true, yes or 1, false if otherwise. */ +bool rc_env_bool (const char *variable); /*! Check if the file exists or not * @param pathname to check * @return true if it exists, otherwise false */ diff --git a/src/runscript.c b/src/runscript.c index 0d839b3f..dab01ebb 100644 --- a/src/runscript.c +++ b/src/runscript.c @@ -562,7 +562,7 @@ static void svc_start (bool deps) rc_plugin_run (rc_hook_service_start_in, applet); hook_out = rc_hook_service_start_out; - if (rc_is_env ("IN_HOTPLUG", "1") || in_background) { + if (rc_env_bool ("IN_HOTPLUG") || in_background) { if (! rc_service_state (service, rc_service_inactive) && ! rc_service_state (service, rc_service_stopped)) exit (EXIT_FAILURE); @@ -584,7 +584,7 @@ static void svc_start (bool deps) make_exclusive (service); - if (rc_is_env ("RC_DEPEND_STRICT", "yes")) + if (rc_env_bool ("RC_DEPEND_STRICT")) depoptions |= RC_DEP_STRICT; if (rc_runlevel_starting ()) @@ -630,7 +630,7 @@ static void svc_start (bool deps) STRLIST_FOREACH (use_services, svc, i) if (rc_service_state (svc, rc_service_stopped)) { pid_t pid = rc_start_service (svc); - if (! rc_is_env ("RC_PARALLEL", "yes")) + if (! rc_env_bool ("RC_PARALLEL")) rc_waitpid (pid); } } @@ -802,7 +802,7 @@ static void svc_stop (bool deps) rc_service_state (service, rc_service_failed)) exit (EXIT_FAILURE); - if (rc_is_env ("IN_HOTPLUG", "1") || in_background) + if (rc_env_bool ("IN_HOTPLUG") || in_background) if (! rc_service_state (service, rc_service_started) && ! rc_service_state (service, rc_service_inactive)) exit (EXIT_FAILURE); @@ -827,7 +827,7 @@ static void svc_stop (bool deps) char *svc; int i; - if (rc_is_env ("RC_DEPEND_STRICT", "yes")) + if (rc_env_bool ("RC_DEPEND_STRICT")) depoptions |= RC_DEP_STRICT; if (rc_runlevel_stopping ()) @@ -857,7 +857,7 @@ static void svc_stop (bool deps) rc_service_state (svc, rc_service_inactive)) { pid_t pid = rc_stop_service (svc); - if (! rc_is_env ("RC_PARALLEL", "yes")) + if (! rc_env_bool ("RC_PARALLEL")) rc_waitpid (pid); rc_strlist_add (&tmplist, svc); } @@ -1088,7 +1088,7 @@ int runscript (int argc, char **argv) setenv ("RC_RUNSCRIPT_PID", pid, 1); /* eprefix is kinda klunky, but it works for our purposes */ - if (rc_is_env ("RC_PARALLEL", "yes")) { + if (rc_env_bool ("RC_PARALLEL")) { int l = 0; int ll; @@ -1138,13 +1138,13 @@ int runscript (int argc, char **argv) /* Save the IN_BACKGROUND env flag so it's ONLY passed to the service that is being called and not any dependents */ if (getenv ("IN_BACKGROUND")) { - in_background = rc_is_env ("IN_BACKGROUND", "true"); + in_background = rc_env_bool ("IN_BACKGROUND"); ibsave = rc_xstrdup (getenv ("IN_BACKGROUND")); unsetenv ("IN_BACKGROUND"); } - if (rc_is_env ("IN_HOTPLUG", "1")) { - if (! rc_is_env ("RC_HOTPLUG", "yes") || ! rc_allow_plug (applet)) + if (rc_env_bool ("IN_HOTPLUG")) { + if (! rc_env_bool ("RC_HOTPLUG") || ! rc_allow_plug (applet)) eerrorx ("%s: not allowed to be hotplugged", applet); } @@ -1196,7 +1196,7 @@ int runscript (int argc, char **argv) strcmp (optarg, "iprovide") == 0) { int depoptions = RC_DEP_TRACE; - if (rc_is_env ("RC_DEPEND_STRICT", "yes")) + if (rc_env_bool ("RC_DEPEND_STRICT")) depoptions |= RC_DEP_STRICT; if (! deptree && ((deptree = rc_load_deptree ()) == NULL)) diff --git a/src/start-stop-daemon.c b/src/start-stop-daemon.c index 20d2cec8..f61c2d90 100644 --- a/src/start-stop-daemon.c +++ b/src/start-stop-daemon.c @@ -684,7 +684,7 @@ int start_stop_daemon (int argc, char **argv) case_RC_COMMON_GETOPT } - quiet = rc_is_env ("RC_QUIET", "yes"); + quiet = rc_env_bool ("RC_QUIET"); verbose = rc_is_env ("RC_VERBOSE", "yes"); /* Allow start-stop-daemon --signal HUP --exec /usr/sbin/dnsmasq -- cgit v1.2.3