From 0dbb12eefe4837aed741adf49d9822b3c783bd71 Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Thu, 19 Apr 2018 18:31:37 -0600 Subject: pathtoabs: Added comments and better error handling If chdir returns anything other than 0 now, we return errno to indicate that something went wrong with determination of the absolute path. Also added errno.h include to handle errno values. --- c/src/main.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/c/src/main.c b/c/src/main.c index 341e6a4..0f30b24 100644 --- a/c/src/main.c +++ b/c/src/main.c @@ -20,15 +20,29 @@ #include #include #include // Provides basename and dirname +#include #include "encarchive.h" + +/** + * pathtoabs: + * Converts the specified path, relative or absolute, to the absolute path + * equivelant. + * Uses the unistd getcwd and chdir to determine the absolute path, so the + * specified path must exist. for successful conversion + * + * @path Path to convert to absolute + * @apath Pointer to absolute path buffer (must be 2048 long) + */ int pathtoabs(char* path, char* apath) { char startdir[1024]; getcwd(startdir, 1024); // Change dir to the parent directory - chdir(dirname(path)); + // Return errno on failure + if(chdir(dirname(path)) != 0) + return errno; getcwd(&apath[0], 2048); strcat(apath, "/"); -- cgit v1.2.3