diff options
author | Roy Marples <roy@marples.name> | 2008-03-16 17:00:56 +0000 |
---|---|---|
committer | Roy Marples <roy@marples.name> | 2008-03-16 17:00:56 +0000 |
commit | cb9da6a262b60255cd037f20b4cde3ab2c8a1e6a (patch) | |
tree | c5c57d5adedf009fdb02b53677e2cdf940bdb47c /src/rc/rc-update.c | |
parent | 40e12f6ba026af9c24d5c3d8e36512719ed5faee (diff) |
Remove null terminated char ** lists in favour of RC_STRINGLIST, using TAILQ from queue(3). Refactor code style around the BSD KNF.
Diffstat (limited to 'src/rc/rc-update.c')
-rw-r--r-- | src/rc/rc-update.c | 153 |
1 files changed, 84 insertions, 69 deletions
diff --git a/src/rc/rc-update.c b/src/rc/rc-update.c index fdd1417c..e09f521c 100644 --- a/src/rc/rc-update.c +++ b/src/rc/rc-update.c @@ -42,7 +42,6 @@ #include "einfo.h" #include "rc.h" #include "rc-misc.h" -#include "strlist.h" extern const char *applet; @@ -68,7 +67,7 @@ static int add (const char *runlevel, const char *service) eerror ("%s: failed to add service `%s' to runlevel `%s': %s", applet, service, runlevel, strerror (errno)); - return (retval); + return retval; } static int delete (const char *runlevel, const char *service) @@ -88,44 +87,47 @@ static int delete (const char *runlevel, const char *service) eerror ("%s: failed to remove service `%s' from runlevel `%s': %s", applet, service, runlevel, strerror (errno)); - return (retval); + return retval; } -static void show (char **runlevels, bool verbose) +static void show (RC_STRINGLIST *runlevels, bool verbose) { - char *service; - char **services = rc_services_in_runlevel (NULL); - char *runlevel; - int i; - int j; - - STRLIST_FOREACH (services, service, i) { - char **in = NULL; - bool inone = false; - - STRLIST_FOREACH (runlevels, runlevel, j) { - if (rc_service_in_runlevel (service, runlevel)) { - rc_strlist_add (&in, runlevel); + RC_STRINGLIST *services = rc_services_in_runlevel(NULL); + RC_STRING *service; + RC_STRING *runlevel; + RC_STRINGLIST *in; + bool inone; + char buffer[PATH_MAX]; + size_t l; + + TAILQ_FOREACH(service, services, entries) { + in = rc_stringlist_new(); + inone = false; + + TAILQ_FOREACH(runlevel, runlevels, entries) { + if (rc_service_in_runlevel(service->value, + runlevel->value)) + { + rc_stringlist_add(in, runlevel->value); inone = true; } else { - char buffer[PATH_MAX]; - memset (buffer, ' ', strlen (runlevel)); - buffer[strlen (runlevel)] = 0; - rc_strlist_add (&in, buffer); + l = strlen(runlevel->value); + memset (buffer, ' ', l); + buffer[l] = 0; + rc_stringlist_add (in, buffer); } } - if (! inone && ! verbose) - continue; - - printf (" %20s |", service); - STRLIST_FOREACH (in, runlevel, j) - printf (" %s", runlevel); - printf ("\n"); - rc_strlist_free (in); + if (inone || verbose) { + printf(" %20s |", service->value); + TAILQ_FOREACH(runlevel, in, entries) + printf (" %s", runlevel->value); + printf ("\n"); + } + rc_stringlist_free(in); } - rc_strlist_free (services); + rc_stringlist_free (services); } #include "_usage.h" @@ -146,111 +148,124 @@ static const char * const longopts_help[] = { #define DODELETE (1 << 2) #define DOSHOW (1 << 3) -int rc_update (int argc, char **argv) +int rc_update(int argc, char **argv) { - int i; + RC_STRINGLIST *runlevels; + RC_STRING *runlevel; char *service = NULL; - char **runlevels = NULL; - char *runlevel; + char *p; int action = 0; bool verbose = false; int opt; int retval = EXIT_FAILURE; + int num_updated = 0; + int (*actfunc)(const char *, const char *); + int ret; - while ((opt = getopt_long (argc, argv, getoptstring, + while ((opt = getopt_long(argc, argv, getoptstring, longopts, (int *) 0)) != -1) - { switch (opt) { - case_RC_COMMON_GETOPT + case_RC_COMMON_GETOPT } - } - verbose = rc_yesno (getenv ("EINFO_VERBOSE")); + verbose = rc_yesno(getenv ("EINFO_VERBOSE")); if ((action & DOSHOW && action != DOSHOW) || (action & DOADD && action != DOADD) || (action & DODELETE && action != DODELETE)) - eerrorx ("%s: cannot mix commands", applet); + eerrorx("%s: cannot mix commands", applet); /* We need to be backwards compatible */ if (optind < argc) { - if (strcmp (argv[optind], "add") == 0) + if (strcmp(argv[optind], "add") == 0) action = DOADD; - else if (strcmp (argv[optind], "delete") == 0 || - strcmp (argv[optind], "del") == 0) + else if (strcmp(argv[optind], "delete") == 0 || + strcmp(argv[optind], "del") == 0) action = DODELETE; - else if (strcmp (argv[optind], "show") == 0) + else if (strcmp(argv[optind], "show") == 0) action = DOSHOW; if (action) optind++; else - eerrorx ("%s: invalid command `%s'", applet, argv[optind]); + eerrorx("%s: invalid command `%s'", applet, argv[optind]); } if (! action) action = DOSHOW; + runlevels = rc_stringlist_new(); + if (optind >= argc) { if (! action & DOSHOW) - eerrorx ("%s: no service specified", applet); + eerrorx("%s: no service specified", applet); } else { service = argv[optind]; optind++; while (optind < argc) - if (rc_runlevel_exists (argv[optind])) - rc_strlist_add (&runlevels, argv[optind++]); + if (rc_runlevel_exists(argv[optind])) + rc_stringlist_add(runlevels, argv[optind++]); else { - rc_strlist_free (runlevels); - eerrorx ("%s: `%s' is not a valid runlevel", applet, argv[optind]); + rc_stringlist_free(runlevels); + eerrorx ("%s: `%s' is not a valid runlevel", + applet, argv[optind]); } } retval = EXIT_SUCCESS; if (action & DOSHOW) { if (service) - rc_strlist_add (&runlevels, service); - if (! runlevels) - runlevels = rc_runlevel_list (); + rc_stringlist_add(runlevels, service); + if (! TAILQ_FIRST(runlevels)) { + free(runlevels); + runlevels = rc_runlevel_list(); + } show (runlevels, verbose); } else { if (! service) eerror ("%s: no service specified", applet); else { - int num_updated = 0; - int (*actfunc)(const char *, const char *); - int ret; - if (action & DOADD) { actfunc = add; } else if (action & DODELETE) { actfunc = delete; - } else + } else { + rc_stringlist_free(runlevels); eerrorx ("%s: invalid action", applet); + } - if (! runlevels) - rc_strlist_add (&runlevels, rc_runlevel_get ()); + if (! TAILQ_FIRST(runlevels)) { + p = rc_runlevel_get(); + rc_stringlist_add(runlevels, p); + free(p); + } - if (! runlevels) + if (! TAILQ_FIRST(runlevels)) { + free(runlevels); eerrorx ("%s: no runlevels found", applet); + } - STRLIST_FOREACH (runlevels, runlevel, i) { - if (! rc_runlevel_exists (runlevel)) { - eerror ("%s: runlevel `%s' does not exist", applet, runlevel); + TAILQ_FOREACH (runlevel, runlevels, entries) { + if (! rc_runlevel_exists(runlevel->value)) { + eerror ("%s: runlevel `%s' does not exist", + applet, runlevel->value); continue; } - ret = actfunc (runlevel, service); + ret = actfunc(runlevel->value, service); if (ret < 0) retval = EXIT_FAILURE; num_updated += ret; } - if (retval == EXIT_SUCCESS && num_updated == 0 && action & DODELETE) - ewarnx ("%s: service `%s' not found in any of the specified runlevels", applet, service); + if (retval == EXIT_SUCCESS && + num_updated == 0 && action & DODELETE) + ewarnx("%s: service `%s' not found in any" + " of the specified runlevels", + applet, service); } } - rc_strlist_free (runlevels); - return (retval); + rc_stringlist_free(runlevels); + return retval; } |