aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/utils.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
index 5889d70..7fe98b8 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -169,6 +169,28 @@ char *path_basename(char *path) {
return result;
}
+/**
+ * Return parent directory of file, or the parent of a directory
+ *
+ * @param path
+ * @return success=directory, failure=empty string
+ */
+char *path_dirname(char *path) {
+ if (!path) {
+ return "";
+ }
+ if (strlen(path) == 1 && *path == '/') {
+ return "/";
+ }
+ char *pos = strrchr(path, '/');
+ if (!pos) {
+ return ".";
+ }
+ *path = '\0';
+
+ return path;
+}
+
char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn *readerFn) {
FILE *fp = NULL;
char **result = NULL;