|
|
@ -133,12 +133,12 @@ void path_free(char** path) { |
|
|
|
Creates a new path pointing to the parent. |
|
|
|
Creates a new path pointing to the parent. |
|
|
|
Be sure to path_free() on the base as well after use. |
|
|
|
Be sure to path_free() on the base as well after use. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
char* path_parent(const char* path) { |
|
|
|
char* path_parent_copy(const char* path) { |
|
|
|
if (!path) { |
|
|
|
if (!path) { |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
char* base_path = NULL; |
|
|
|
char* parent_path = NULL; |
|
|
|
char* clean_path = NULL; |
|
|
|
char* clean_path = NULL; |
|
|
|
size_t path_len; |
|
|
|
size_t path_len; |
|
|
|
size_t last_separator_index = 0; |
|
|
|
size_t last_separator_index = 0; |
|
|
@ -160,20 +160,53 @@ char* path_parent(const char* path) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (last_separator_index == 0) { |
|
|
|
|
|
|
|
return clean_path; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// allocate memory to hold up to last separator bytes
|
|
|
|
// allocate memory to hold up to last separator bytes
|
|
|
|
base_path = malloc(sizeof(char) * last_separator_index); |
|
|
|
parent_path = malloc(sizeof(char) * last_separator_index); |
|
|
|
if (!base_path) { |
|
|
|
if (!parent_path) { |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// copy over
|
|
|
|
// copy over
|
|
|
|
memcpy(base_path, clean_path, sizeof(char) * last_separator_index); |
|
|
|
memcpy(parent_path, clean_path, sizeof(char) * last_separator_index); |
|
|
|
base_path[last_separator_index] = '\0'; |
|
|
|
parent_path[last_separator_index] = '\0'; |
|
|
|
|
|
|
|
|
|
|
|
// free temporary cleaned path
|
|
|
|
// free temporary cleaned path
|
|
|
|
path_free(&clean_path); |
|
|
|
path_free(&clean_path); |
|
|
|
|
|
|
|
|
|
|
|
return base_path; |
|
|
|
return parent_path; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Shrinks given path so that it points to the parent. |
|
|
|
|
|
|
|
Reallocates memory. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
void path_parent(char* path) { |
|
|
|
|
|
|
|
if (!path) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
path_clean(path); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t path_len = strlen(path); |
|
|
|
|
|
|
|
size_t last_separator_index = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// find the last separator index
|
|
|
|
|
|
|
|
for (size_t i = 0; i < path_len; i++) { |
|
|
|
|
|
|
|
if (path[i] == PATH_SEPARATOR) { |
|
|
|
|
|
|
|
last_separator_index = i; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (last_separator_index == 0) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
path = realloc(path, sizeof(char) * last_separator_index); |
|
|
|
|
|
|
|
path[last_separator_index] = '\0'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|