summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Nicholson <dbn.lists@gmail.com>2008-06-04 08:52:56 -0700
committerDan Nicholson <dbn.lists@gmail.com>2008-06-04 08:52:56 -0700
commit4a52f1a86b17cafef71f51793c0778f430323f11 (patch)
tree1ecb5def97d90f15d91b89eecb131fdd02b3212f
parent04471bd33edb8100b9604ab5e304104ad2055d44 (diff)
Add basename and dirname wrapper utilities
Added wrappers for the basename() and dirname() functions that handle the copying of the strings correctly. The initd_parse routine has been adjusted to use this wrapper instead of the libc version.
-rw-r--r--lib/Makefile.am3
-rw-r--r--lib/parse.c10
-rw-r--r--lib/util.c50
-rw-r--r--lib/util.h7
4 files changed, 65 insertions, 5 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index a211c9d..b88925a 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,3 +1,4 @@
noinst_LTLIBRARIES = libinitd.la
libinitd_la_SOURCES = str.h str.c strarg.c types.h parse.c active.c \
- initd.h initd.c initd-list.c rdep.h rdep.c installrm.h installrm.c
+ initd.h initd.c initd-list.c rdep.h rdep.c util.h util.c \
+ installrm.h installrm.c
diff --git a/lib/parse.c b/lib/parse.c
index 87d04c2..010b10b 100644
--- a/lib/parse.c
+++ b/lib/parse.c
@@ -7,8 +7,8 @@
#include <error.h>
#include <errno.h>
#include <ctype.h>
-#include <libgen.h>
#include "initd.h"
+#include "util.h"
static FILE *initd_open(const char *path);
static void initd_close(FILE *ifd);
@@ -23,14 +23,16 @@ static void initd_parse_line_tokens(initd_t *ip, const char *line,
script was not valid. */
initd_t *initd_parse(const char *path)
{
- char line[INITD_LINE_MAX];
+ char line[INITD_LINE_MAX], *name;
size_t nchar;
int in_header, is_initd;
initd_key_t key;
- initd_t *ip = NULL;
+ initd_t *ip;
FILE *ifd = initd_open(path);
- ip = initd_new(basename((char *)path));
+ name = initd_basename(path);
+ ip = initd_new(name);
+ free(name);
in_header = is_initd = 0;
key = KEY_NONE;
diff --git a/lib/util.c b/lib/util.c
new file mode 100644
index 0000000..120b844
--- /dev/null
+++ b/lib/util.c
@@ -0,0 +1,50 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include <string.h>
+#include <stdlib.h>
+#include <libgen.h>
+#include <error.h>
+#include <errno.h>
+#include "util.h"
+
+/* Wrappers around basename() and dirname() */
+char *initd_basename(const char *path)
+{
+ char *tbase, *base;
+
+ if (path) {
+ tbase = strdup(path);
+ if (!tbase)
+ error(EXIT_FAILURE, errno, "strdup");
+ } else {
+ tbase = NULL;
+ }
+
+ base = strdup(basename(tbase));
+ if (!base)
+ error(EXIT_FAILURE, errno, "strdup");
+
+ free(tbase);
+ return base;
+}
+
+char *initd_dirname(const char *path)
+{
+ char *tdir, *dir;
+
+ if (path) {
+ tdir = strdup(path);
+ if (!tdir)
+ error(EXIT_FAILURE, errno, "strdup");
+ } else {
+ tdir = NULL;
+ }
+
+ dir = strdup(dirname(tdir));
+ if (!dir)
+ error(EXIT_FAILURE, errno, "strdup");
+
+ free(tdir);
+ return dir;
+}
diff --git a/lib/util.h b/lib/util.h
new file mode 100644
index 0000000..e603a0c
--- /dev/null
+++ b/lib/util.h
@@ -0,0 +1,7 @@
+#ifndef _util_h_
+#define _util_h_
+
+extern char *initd_basename(const char *path);
+extern char *initd_dirname(const char *path);
+
+#endif /* _util_h_ */