diff options
author | Aaron Ball <nullspoon@oper.io> | 2018-04-19 18:31:37 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2018-04-19 18:31:37 -0600 |
commit | 0dbb12eefe4837aed741adf49d9822b3c783bd71 (patch) | |
tree | b03c7c04463be2160e5bac2470db83904cecd589 | |
parent | 3e57ef6ffb7f639bb4dee14c890311259a6cdb36 (diff) | |
download | gpgsecure-0dbb12eefe4837aed741adf49d9822b3c783bd71.tar.gz gpgsecure-0dbb12eefe4837aed741adf49d9822b3c783bd71.tar.xz |
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.
-rw-r--r-- | c/src/main.c | 16 |
1 files changed, 15 insertions, 1 deletions
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 <string.h> #include <unistd.h> #include <libgen.h> // Provides basename and dirname +#include <errno.h> #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, "/"); |