summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2010-01-27 22:37:50 +0100
committerLennart Poettering <lennart@poettering.net>2010-01-27 22:37:50 +0100
commit4a72ff34c62f18559e96b35d69c665e07290f228 (patch)
tree0a5ba2ca54f6f6a596d6391a554bd0b865b8eb22 /util.c
parentfa06836725dd6ec9f8f1c4b76608f7147008ecd9 (diff)
implement new utility functions strstrip() and file_in_same_dir()
Diffstat (limited to 'util.c')
-rw-r--r--util.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/util.c b/util.c
index 4774610265..56f53eafd6 100644
--- a/util.c
+++ b/util.c
@@ -490,3 +490,51 @@ int reset_all_signal_handlers(void) {
return 0;
}
+
+char *strstrip(char *s) {
+ char *e, *l = NULL;
+
+ /* Drops trailing whitespace. Modifies the string in
+ * place. Returns pointer to first non-space character */
+
+ s += strspn(s, WHITESPACE);
+
+ for (e = s; *e; e++)
+ if (!strchr(WHITESPACE, *e))
+ l = e;
+
+ if (l)
+ *(l+1) = 0;
+ else
+ *s = 0;
+
+ return s;
+
+}
+
+char *file_in_same_dir(const char *path, const char *filename) {
+ char *e, *r;
+ size_t k;
+
+ assert(path);
+ assert(filename);
+
+ /* This removes the last component of path and appends
+ * filename, unless the latter is absolute anyway or the
+ * former isn't */
+
+ if (path_is_absolute(filename))
+ return strdup(filename);
+
+ if (!(e = strrchr(path, '/')))
+ return strdup(filename);
+
+ k = strlen(filename);
+ if (!(r = new(char, e-path+1+k+1)))
+ return NULL;
+
+ memcpy(r, path, e-path+1);
+ memcpy(r+(e-path)+1, filename, k+1);
+
+ return r;
+}