]> git.seodisparate.com - SimpleArchiver/commitdiff
Refactor parser, setup for different temp dir
authorStephen Seo <seo.disparate@gmail.com>
Thu, 25 Jul 2024 01:26:34 +0000 (10:26 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 25 Jul 2024 01:26:34 +0000 (10:26 +0900)
src/main.c
src/parser.c
src/parser.h

index 592ed503f3f31f192bf735421692eb62a850b174..40eef6520fc37f75a99948eda180f29c1e09ecc7 100644 (file)
@@ -44,7 +44,10 @@ int main(int argc, const char **argv) {
       cleanup(simple_archiver_free_parsed))) SDArchiverParsed parsed =
       simple_archiver_create_parsed();
 
-  simple_archiver_parse_args(argc, argv, &parsed);
+  if (simple_archiver_parse_args(argc, argv, &parsed)) {
+    fprintf(stderr, "Failed to parse args.\n");
+    return 7;
+  }
 
   if (!parsed.filename && (parsed.flags & 0x10) == 0) {
     fprintf(stderr, "ERROR: Filename not specified!\n");
index bb7c8c08a9d2f89fbc75f649648fd691ff31e8a1..747e1aebd382ef9738ca35f44c17e01ce9f78cc0 100644 (file)
@@ -153,6 +153,9 @@ void simple_archiver_print_usage(void) {
   fprintf(stderr, "--overwrite-extract : allows overwriting when extracting\n");
   fprintf(stderr,
           "--no-abs-symlink : do not store absolute paths for symlinks\n");
+  fprintf(stderr,
+          "--temp-files-dir <dir> : where to store temporary files created "
+          "when compressing (defaults to current working directory)\n");
   fprintf(stderr,
           "-- : specifies remaining arguments are files to archive/extract\n");
   fprintf(
@@ -214,7 +217,12 @@ int simple_archiver_parse_args(int argc, const char **argv,
         out->flags &= 0xFFFFFFFC;
         // set second bit.
         out->flags |= 0x2;
-      } else if (strcmp(argv[0], "-f") == 0 && argc > 1) {
+      } else if (strcmp(argv[0], "-f") == 0) {
+        if (argc < 2) {
+          fprintf(stderr, "ERROR: -f specified but missing argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
         if (strcmp(argv[1], "-") == 0) {
           out->flags |= 0x10;
           if (out->filename) {
@@ -229,13 +237,23 @@ int simple_archiver_parse_args(int argc, const char **argv,
         }
         --argc;
         ++argv;
-      } else if (strcmp(argv[0], "--compressor") == 0 && argc > 1) {
+      } else if (strcmp(argv[0], "--compressor") == 0) {
+        if (argc < 2) {
+          fprintf(stderr, "--compressor specfied but missing argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
         int size = strlen(argv[1]) + 1;
         out->compressor = malloc(size);
         strncpy(out->compressor, argv[1], size);
         --argc;
         ++argv;
-      } else if (strcmp(argv[0], "--decompressor") == 0 && argc > 1) {
+      } else if (strcmp(argv[0], "--decompressor") == 0) {
+        if (argc < 2) {
+          fprintf(stderr, "--decompressor specfied but missing argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
         int size = strlen(argv[1]) + 1;
         out->decompressor = malloc(size);
         strncpy(out->decompressor, argv[1], size);
@@ -247,6 +265,15 @@ int simple_archiver_parse_args(int argc, const char **argv,
         out->flags |= 0x8;
       } else if (strcmp(argv[0], "--no-abs-symlink") == 0) {
         out->flags |= 0x20;
+      } else if (strcmp(argv[0], "--temp-files-dir") == 0) {
+        if (argc < 2) {
+          fprintf(stderr, "ERROR: --temp-files-dir is missing an argument!\n");
+          simple_archiver_print_usage();
+          return 1;
+        }
+        out->temp_dir = argv[1];
+        --argc;
+        ++argv;
       } else if (argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == 0) {
         is_remaining_args = 1;
       } else if (argv[0][0] != '-') {
index f5a8400902233a9a1819c71b37b944b57fbb6aa8..9ba42a7027de1dca23d67be4031c2c56dad2ca63 100644 (file)
@@ -42,6 +42,9 @@ typedef struct SDArchiverParsed {
   /// Last entry should be NULL.
   /// Determines a "white-list" of files to extract when extracting.
   char **working_files;
+  /// Determines where to place temporary files. If NULL, temporary files are
+  /// created in the current working directory.
+  const char *temp_dir;
 } SDArchiverParsed;
 
 typedef struct SDArchiverFileInfo {
@@ -57,6 +60,7 @@ SDArchiverParsed simple_archiver_create_parsed(void);
 /// Expects the user to pass a pointer to an SDArchiverParsed.
 /// This means the user should have a SDArchiverParsed variable
 /// and it should be passed with e.g. "&var".
+/// Returns 0 on success.
 int simple_archiver_parse_args(int argc, const char **argv,
                                SDArchiverParsed *out);