From: Lucas De Marchi Date: Wed, 7 Dec 2011 15:51:40 +0000 (-0200) Subject: Add helper path_make_absolute_cwd() X-Git-Tag: v1~75 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=06363cc126fbc47fcb860c1ce308b4f8a6f6804b;p=platform%2Fupstream%2Fkmod.git Add helper path_make_absolute_cwd() --- 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; +}