summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2015-06-21 00:19:22 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2015-06-21 00:19:22 +0200
commit10760e447f95a4b6dbeaae90456a7e9efd0a796c (patch)
treeb6cfde690c7c3d0271e0cbd660d28109cc0cde35
parent5646132f6b0389d7d1b0b1b464e9fde95c25381c (diff)
add xbasename for filename to feedname
-rw-r--r--util.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/util.c b/util.c
index 8e41a15..df0a114 100644
--- a/util.c
+++ b/util.c
@@ -1,6 +1,7 @@
#include <ctype.h>
#include <err.h>
#include <errno.h>
+#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
@@ -203,3 +204,24 @@ printcontent(const char *s, FILE *fp)
}
}
}
+
+/* Some implementations of basename(3) return a pointer to a static
+ * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
+ * This is a wrapper function that is compatible with both versions.
+ * The program will error out if basename(3) failed, this can only happen
+ * with the OpenBSD version.
+ */
+char *
+xbasename(const char *path)
+{
+ char *p, *b;
+
+ if(!(p = strdup(path)))
+ err(1, "strdup");
+ if(!(b = basename(p)))
+ err(1, "basename");
+ if(!(b = strdup(b)))
+ err(1, "strdup");
+ free(p);
+ return b;
+}