aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2023-12-10 01:06:51 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2023-12-10 01:06:51 -0500
commit58df72e651e6b45ba60b0ab3cc29538976cd9697 (patch)
treed187acd911f41b1183b3a40730dc2a92508eab10 /src
parentebdcd4652d55097e56d2a66aebf382663d99089b (diff)
downloadstasis-58df72e651e6b45ba60b0ab3cc29538976cd9697.tar.gz
Add path_dirname() function
Diffstat (limited to 'src')
-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;