diff options
-rw-r--r-- | c/src/main.c | 50 |
1 files changed, 45 insertions, 5 deletions
diff --git a/c/src/main.c b/c/src/main.c index 1c2aa16..341e6a4 100644 --- a/c/src/main.c +++ b/c/src/main.c @@ -18,13 +18,32 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> +#include <libgen.h> // Provides basename and dirname #include "encarchive.h" +int pathtoabs(char* path, char* apath) { + char startdir[1024]; + getcwd(startdir, 1024); + + // Change dir to the parent directory + chdir(dirname(path)); + + getcwd(&apath[0], 2048); + strcat(apath, "/"); + strcat(apath, basename(path)); + + chdir(startdir); + return 0; +} + int main(int argc, char* argv[]) { - char* action; // Action to perform on the archive - char* archive; // Path to the archive to open. - char* tmp; // Temp (memory) path to extract into + char *action; // Action to perform on the archive + char *archive; // Path to the archive to open. + char archabs[2048]; + char *tmp; // Temp (memory) path to extract into + char tmptemplate[256] = "/tmp/gpgsecure.XXXXXX"; // Path template for tmp if(argc == 1) { printf("Archive action required (enum, extract)\n"); @@ -37,12 +56,33 @@ int main(int argc, char* argv[]) { } action = argv[1]; archive = argv[2]; - tmp = "/tmp/gpgsecure/"; if(strcmp(action, "enum") == 0) { return enc_archive_enum(archive); } else if(strcmp(action, "extract") == 0) { - return enc_archive_extract(archive, tmp); + // Convert archive path to absolute path + pathtoabs(archive, archabs); + + // Create temp dir for extraction + tmp = mkdtemp(tmptemplate); + + // Change to the temp dir for extraction + if(chdir(tmp) != 0) { + printf("An error occured switching to temp directory '%s'\n", tmp); + return 1; + } + + printf("Archive will be extracted to '%s'\n", tmp); + + // Extract + if(enc_archive_extract(archabs, tmp) == 0) { + + // printf("Cleaning up\n"); + //return rmdir(tmp); + return 0; + } + + return 1; } printf("Unknown action \"%s\"\n", action); |