From 06363cc126fbc47fcb860c1ce308b4f8a6f6804b Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Wed, 7 Dec 2011 13:51:40 -0200 Subject: [PATCH] Add helper path_make_absolute_cwd() --- libkmod/libkmod-private.h | 1 + libkmod/libkmod-util.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/libkmod/libkmod-private.h b/libkmod/libkmod-private.h index 58ac0db..578edf2 100644 --- a/libkmod/libkmod-private.h +++ b/libkmod/libkmod-private.h @@ -117,6 +117,7 @@ int read_str_long(int fd, long *value, int base) __must_check __attribute__((non int read_str_ulong(int fd, unsigned long *value, int base) __must_check __attribute__((nonnull(2))); char *strchr_replace(char *s, int c, char r); bool path_is_absolute(const char *p) __must_check __attribute__((nonnull(1))); +char *path_make_absolute_cwd(const char *p) __must_check __attribute__((nonnull(1))); #endif diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c index 477139f..f855bc7 100644 --- a/libkmod/libkmod-util.c +++ b/libkmod/libkmod-util.c @@ -221,3 +221,32 @@ bool path_is_absolute(const char *p) return p[0] == '/'; } + +char *path_make_absolute_cwd(const char *p) +{ + char *cwd, *r; + size_t plen; + size_t cwdlen; + + if (path_is_absolute(p)) + return strdup(p); + + cwd = get_current_dir_name(); + if (cwd == NULL) + return NULL; + + plen = strlen(p); + cwdlen = strlen(cwd); + + /* cwd + '/' + p + '\0' */ + r = realloc(cwd, cwdlen + 1 + plen + 1); + if (r == NULL) { + free(cwd); + return NULL; + } + + r[plen] = '/'; + memcpy(&r[plen + 1], p, plen + 1); + + return r; +} -- 2.7.4