diff options
Diffstat (limited to 'src/path.c')
| -rw-r--r-- | src/path.c | 22 |
1 files changed, 7 insertions, 15 deletions
@@ -1,5 +1,7 @@ #include "path.h" +#include "util.h" + #include <ctype.h> #include <stdio.h> #include <stdlib.h> @@ -25,16 +27,12 @@ int ecex_path_copy(char *out, size_t out_size, const char *text) { char *ecex_path_expand_user(const char *path) { if (!path) return NULL; if (path[0] != '~' || (path[1] != '\0' && path[1] != '/')) { - char *copy = malloc(strlen(path) + 1); - if (copy) strcpy(copy, path); - return copy; + return ecex_strdup(path); } const char *home = getenv("HOME"); if (!home || !home[0]) { - char *copy = malloc(strlen(path) + 1); - if (copy) strcpy(copy, path); - return copy; + return ecex_strdup(path); } size_t home_len = strlen(home); @@ -49,9 +47,7 @@ char *ecex_path_expand_user(const char *path) { char *ecex_path_join(const char *dir, const char *name) { if (!name) return NULL; if (name[0] == '/') { - char *copy = malloc(strlen(name) + 1); - if (copy) strcpy(copy, name); - return copy; + return ecex_strdup(name); } if (!dir || !dir[0]) dir = "."; @@ -68,9 +64,7 @@ char *ecex_path_join(const char *dir, const char *name) { char *ecex_path_dirname(const char *path) { if (!path || !path[0]) { - char *dot = malloc(2); - if (dot) strcpy(dot, "."); - return dot; + return ecex_strdup("."); } char *expanded = ecex_path_expand_user(path); @@ -98,9 +92,7 @@ char *ecex_path_basename_dup(const char *path) { else if (slash && slash == path) base = "/"; else base = path; } - char *out = malloc(strlen(base) + 1); - if (out) strcpy(out, base); - return out; + return ecex_strdup(base); } char *ecex_path_normalize(const char *path) { |
