summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Nicholson <dbn.lists@gmail.com>2008-06-04 19:03:59 -0700
committerDan Nicholson <dbn.lists@gmail.com>2008-06-04 19:03:59 -0700
commit05df7b59743fb083fb97ade53e01e823f3d88166 (patch)
tree37a4bd40b8f09935c2996698153d9047c1dc5740
parenta149717c9ca9ad800280607fd9200e5a71c1b4c3 (diff)
Split out common program functions into separate object file
Some routines will be exactly the same between install_initd and remove_initd. This splits them out into a common util.c file. This also adds a new function, get_initd_dir, which wraps dirname, but returns NULL instead of "." when there is no directory component.
-rw-r--r--src/Makefile.am4
-rw-r--r--src/install_initd.c21
-rw-r--r--src/remove_initd.c21
-rw-r--r--src/util.c29
-rw-r--r--src/util.h8
5 files changed, 55 insertions, 28 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index fa4a621..85f7654 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,8 +2,8 @@ INCLUDES = -I$(top_srcdir)/lib
sbin_PROGRAMS = install_initd remove_initd
-install_initd_SOURCES = install_initd.c
+install_initd_SOURCES = install_initd.c util.h util.c
install_initd_LDADD = $(top_builddir)/lib/libinitd.la
-remove_initd_SOURCES = remove_initd.c
+remove_initd_SOURCES = remove_initd.c util.h util.c
remove_initd_LDADD = $(top_builddir)/lib/libinitd.la
diff --git a/src/install_initd.c b/src/install_initd.c
index 03b9b2a..dc1ca9d 100644
--- a/src/install_initd.c
+++ b/src/install_initd.c
@@ -7,6 +7,7 @@
#include <errno.h>
#include <getopt.h>
#include "initd.h"
+#include "util.h"
#ifndef _GNU_SOURCE
char *program_invocation_name = NULL;
@@ -22,7 +23,6 @@ static struct option long_opts[] = {
};
static void usage(FILE *stream);
-static void set_verbose(void);
int main(int argc, char *argv[])
{
@@ -46,7 +46,7 @@ int main(int argc, char *argv[])
usage(stdout);
exit(EXIT_SUCCESS);
case 'v':
- set_verbose();
+ set_verbose(true);
break;
case '?':
error(EXIT_FAILURE, 0,
@@ -56,14 +56,15 @@ int main(int argc, char *argv[])
}
}
- if (optind < argc) {
- while (optind < argc)
- dep_add(need, argv[optind++]);
- } else {
+ /* At least one service must be provided */
+ if (optind >= argc)
error(EXIT_FAILURE, 0, "No services supplied\n"
"See `%s --help' for usage",
program_invocation_name);
- }
+
+ /* Process the services */
+ while (optind < argc)
+ dep_add(need, argv[optind++]);
if (!id_dir)
id_dir = DEF_INITD_DIR;
@@ -108,9 +109,3 @@ static void usage(FILE *stream)
program_invocation_name,
DEF_INITD_DIR);
}
-
-static void set_verbose(void)
-{
- initd_recurse_set_verbose(true);
- initd_installrm_set_verbose(true);
-}
diff --git a/src/remove_initd.c b/src/remove_initd.c
index 2cdd241..24568c7 100644
--- a/src/remove_initd.c
+++ b/src/remove_initd.c
@@ -7,6 +7,7 @@
#include <errno.h>
#include <getopt.h>
#include "initd.h"
+#include "util.h"
#ifndef _GNU_SOURCE
char *program_invocation_name = NULL;
@@ -22,7 +23,6 @@ static struct option long_opts[] = {
};
static void usage(FILE *stream);
-static void set_verbose(void);
int main(int argc, char *argv[])
{
@@ -46,7 +46,7 @@ int main(int argc, char *argv[])
usage(stdout);
exit(EXIT_SUCCESS);
case 'v':
- set_verbose();
+ set_verbose(true);
break;
case '?':
error(EXIT_FAILURE, 0,
@@ -56,14 +56,15 @@ int main(int argc, char *argv[])
}
}
- if (optind < argc) {
- while (optind < argc)
- dep_add(need, argv[optind++]);
- } else {
+ /* At least one service must be provided */
+ if (optind >= argc)
error(EXIT_FAILURE, 0, "No services supplied\n"
"See `%s --help' for usage",
program_invocation_name);
- }
+
+ /* Process the services */
+ while (optind < argc)
+ dep_add(need, argv[optind++]);
if (!id_dir)
id_dir = DEF_INITD_DIR;
@@ -108,9 +109,3 @@ static void usage(FILE *stream)
program_invocation_name,
DEF_INITD_DIR);
}
-
-static void set_verbose(void)
-{
- initd_recurse_set_verbose(true);
- initd_installrm_set_verbose(true);
-}
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..aaa5480
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,29 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <string.h>
+#include "initd.h"
+#include "util.h"
+
+/* 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)
+{
+ /* return NULL for empty string */
+ if ((!path) || (*path == '\0'))
+ return NULL;
+
+ /* return NULL if there is no directory component */
+ if (!strchr(path, '/'))
+ return NULL;
+
+ /* otherwise, use dirname */
+ return initd_dirname(path);
+}
+
+/* Turn on verbose messages in the various initd functions */
+void set_verbose(bool verbose)
+{
+ initd_recurse_set_verbose(verbose);
+ initd_installrm_set_verbose(verbose);
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..cb90ba7
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,8 @@
+#ifndef _util_h_
+#define _util_h_
+#include <stdbool.h>
+
+extern char *get_initd_dir(const char *path);
+extern void set_verbose(bool verbose);
+
+#endif /* _util_h_ */