summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Nicholson <dbn.lists@gmail.com>2008-06-10 09:03:37 -0700
committerDan Nicholson <dbn.lists@gmail.com>2008-06-10 09:03:37 -0700
commitcf1e6b512835784b8672623e524fd355e85d41df (patch)
treeec3855cc96e544d7fe45090c8dcd06615fb479e1
parent05df7b59743fb083fb97ade53e01e823f3d88166 (diff)
Use common set_initd_dir routine to determine init.d directory
Add a new function set_initd_dir() to find the init.d directory from a combination of a previously set string (the -d parameter), the directory names of the arguments, or the default init.d directory.
-rw-r--r--src/install_initd.c5
-rw-r--r--src/remove_initd.c5
-rw-r--r--src/util.c44
-rw-r--r--src/util.h2
4 files changed, 50 insertions, 6 deletions
diff --git a/src/install_initd.c b/src/install_initd.c
index dc1ca9d..e9c48bf 100644
--- a/src/install_initd.c
+++ b/src/install_initd.c
@@ -66,8 +66,9 @@ int main(int argc, char *argv[])
while (optind < argc)
dep_add(need, argv[optind++]);
- if (!id_dir)
- id_dir = DEF_INITD_DIR;
+ /* Set the initd directory from the arguments */
+ if (!set_initd_dir(&id_dir, need))
+ exit(EXIT_FAILURE);
all = initd_list_from_dir(id_dir);
diff --git a/src/remove_initd.c b/src/remove_initd.c
index 24568c7..6b5b9ab 100644
--- a/src/remove_initd.c
+++ b/src/remove_initd.c
@@ -66,8 +66,9 @@ int main(int argc, char *argv[])
while (optind < argc)
dep_add(need, argv[optind++]);
- if (!id_dir)
- id_dir = DEF_INITD_DIR;
+ /* Set the initd directory from the arguments */
+ if (!set_initd_dir(&id_dir, need))
+ exit(EXIT_FAILURE);
all = initd_list_from_dir(id_dir);
diff --git a/src/util.c b/src/util.c
index aaa5480..f58c8b0 100644
--- a/src/util.c
+++ b/src/util.c
@@ -2,12 +2,54 @@
# include <config.h>
#endif
#include <string.h>
+#include <error.h>
#include "initd.h"
#include "util.h"
+static char *get_arg_dir(const char *path);
+
+/* Provided a list of arguments, find the initd directory from their
+ * directory names. If the arguments specify a directory differning from
+ * the currently set directory, return false. If the directory is NULL
+ * after all arguments have been processed, set to DEF_INITD_DIR */
+bool set_initd_dir(char **initd_dir, const strarg_t *args)
+{
+ int n;
+ char *srv, *srvdir;
+
+ if (!initd_dir) {
+ error(0, 0, "No string address passed to %s", __FUNCTION__);
+ return false;
+ }
+
+ for (n = 0; n < strarg_get_num(args); n++) {
+ srv = strarg_get_str(args, n);
+ srvdir = get_arg_dir(srv);
+
+ if (!srvdir)
+ continue;
+
+ /* See if it conflicts with a previous setting */
+ if (*initd_dir && (strcmp(srvdir, *initd_dir) != 0)) {
+ error(0, 0, "Directory %s conflicts with "
+ "previous setting %s",
+ srvdir, *initd_dir);
+ return false;
+ }
+
+ *initd_dir = srvdir;
+ }
+
+ /* If still NULL, set to DEF_INITD_DIR */
+ if (!*initd_dir)
+ *initd_dir = DEF_INITD_DIR;
+
+ return true;
+}
+
/* Given a path, return the directory name or NULL if there is no
* directory component. This slightly alters the behavior of dirname() */
-char *get_initd_dir(const char *path)
+static char *get_arg_dir(const char *path)
{
/* return NULL for empty string */
if ((!path) || (*path == '\0'))
diff --git a/src/util.h b/src/util.h
index cb90ba7..40c80f3 100644
--- a/src/util.h
+++ b/src/util.h
@@ -2,7 +2,7 @@
#define _util_h_
#include <stdbool.h>
-extern char *get_initd_dir(const char *path);
+extern bool set_initd_dir(char **initd_dir, const strarg_t *args);
extern void set_verbose(bool verbose);
#endif /* _util_h_ */