Welcome to little lamb

Code » qmdoc » commit eb64d6e

Add -o/--overwrite to overwrite destination files

author Olivier Brunel
2023-01-01 20:45:36 UTC
committer Olivier Brunel
2023-01-04 15:09:23 UTC
parent d591055b1ff627d988ac9a356e5d350e6baf9074

Add -o/--overwrite to overwrite destination files

main.c +16 -3

diff --git a/main.c b/main.c
index 533a293..bedbd8a 100644
--- a/main.c
+++ b/main.c
@@ -24,6 +24,7 @@ const char *PROG = "qmdoc";
 enum {
     OPT_NO_CSS      = (1 << 0),
     OPT_INLINE_CSS  = (1 << 1),
+    OPT_OVERWRITE   = (1 << 2),
 };
 
 enum {
@@ -895,7 +896,11 @@ convert_page(struct ctx *ctx, int fddest)
     struct page *p = &ctx->pages[ctx->cur_page];
 
     const char *dst = ctx->sa.s + p->fileoff;
-    int fd = openat_excl(fddest, dst);
+    int fd;
+    if (ctx->options & OPT_OVERWRITE)
+        fd = openat_trunc(fddest, dst);
+    else
+        fd = openat_excl(fddest, dst);
     if (fd < 0) ret_strerr_warnwu1sys(ERR_IO, "create destination");
 
     size_t salen = ctx->sa.len;
@@ -1049,6 +1054,7 @@ help(void)
          " -h, --help                   Show this help screen and exit\n"
          " -I, --inline-css             Use inline CSS instead of external files\n"
          " -l, --lang LNG               Set LNG as language attribute\n"
+         " -o, --overwrite              Overwrite destination files if already exist\n"
          " -t, --title TITLE            Set TITLE as general (across all pages) title\n"
         );
 }
@@ -1080,10 +1086,11 @@ main (int argc, char *argv[])
         { "help",           no_argument,        NULL,   'h' },
         { "inline-css",     no_argument,        NULL,   'I' },
         { "lang",           no_argument,        NULL,   'l' },
+        { "overwrite",      no_argument,        NULL,   'o' },
         { "title",          required_argument,  NULL,   't' },
         { NULL,             0,                  NULL,    0  },
     };
-    while ((c = getopt_long(argc, argv, "a:Cd:hIl:t:", opts, NULL)) != -1) switch (c) {
+    while ((c = getopt_long(argc, argv, "a:Cd:hIl:ot:", opts, NULL)) != -1) switch (c) {
         case 'a':
             author = optarg;
             break;
@@ -1101,6 +1108,9 @@ main (int argc, char *argv[])
         case 'l':
             lang = optarg;
             break;
+        case 'o':
+            options |= OPT_OVERWRITE;
+            break;
         case 't':
             title = optarg;
             break;
@@ -1157,7 +1167,10 @@ main (int argc, char *argv[])
                 int from, to;
                 from = open_read(css[i].file);
                 if (from < 0) strerr_diefu3sys(-ERR_IO, "open '", css[i].file, "'");
-                to = openat_excl(fddest, css[i].file);
+                if (options & OPT_OVERWRITE)
+                    to = openat_trunc(fddest, css[i].file);
+                else
+                    to = openat_excl(fddest, css[i].file);
                 if (to < 0) strerr_diefu5sys(-ERR_IO, "create '", destdir, "/", css[i].file, "'");
                 if (fd_cat(from, to) < 0)
                     strerr_diefu5sys(-ERR_IO, "copy CSS to '", destdir, "/", css[i].file, "'");