From d0bd265427872bba3161c4d1b4a73b8058d0cb9f Mon Sep 17 00:00:00 2001
From: Robert Pengelly <robertapengelly@hotmail.com>
Date: Thu, 18 Jul 2024 12:33:17 +0100
Subject: [PATCH] Added --update flag to see if we need to try opening the
 output before creating it

---
 Makefile.zpg | 27 +++++++++++++++++++++++++++
 lib.c        | 13 ++++++++++++-
 parted.c     |  2 +-
 parted.h     |  2 +-
 4 files changed, 41 insertions(+), 3 deletions(-)
 create mode 100644 Makefile.zpg

diff --git a/Makefile.zpg b/Makefile.zpg
new file mode 100644
index 0000000..f2b4730
--- /dev/null
+++ b/Makefile.zpg
@@ -0,0 +1,27 @@
+#******************************************************************************
+# @file             Makefile.zpg
+#******************************************************************************
+
+# makefile.zpg should have been run in pdpclib first
+
+CC=gcc370
+COPTS=-S -Os -fno-common -U__MVS__ -D__MF32__ -D__PDOSGEN__ \
+    -D__NOBIVA__ -D__PDOS__ \
+    -I . -I../pdos/pdpclib -I../pdos/src -I../pdos/generic
+AS=as370
+LD=pdld --oformat mainframe -e __crt0
+
+COBJ=lib.obj parted.obj report.obj write7x.obj
+
+all: clean parted.exe
+
+parted.exe: $(COBJ)
+  $(LD) -s -o parted.exe ../pdos/pdpclib/pgastart.o $(COBJ)
+
+.c.obj:
+  $(CC) $(COPTS) $<
+  $(AS) -o $@ $*.s
+  rm -f $*.s
+
+clean:
+  rm -f *.obj parted.exe
diff --git a/lib.c b/lib.c
index 2f9c947..091fcac 100644
--- a/lib.c
+++ b/lib.c
@@ -28,7 +28,8 @@ enum options {
     OPTION_BOOT,
     OPTION_HELP,
     OPTION_LABEL,
-    OPTION_PART
+    OPTION_PART,
+    OPTION_UPDATE
 
 };
 
@@ -36,7 +37,10 @@ static struct option opts[] = {
 
     { "-arca",      OPTION_ARCA,        OPTION_NO_ARG   },
     { "-boot",      OPTION_BOOT,        OPTION_HAS_ARG  },
+    
     { "-help",      OPTION_HELP,        OPTION_NO_ARG   },
+    { "-update",    OPTION_UPDATE,      OPTION_NO_ARG   },
+    
     { 0,            0,                  0               }
 
 };
@@ -488,6 +492,13 @@ void parse_args (int *pargc, char ***pargv, int optind) {
             
             }
             
+            case OPTION_UPDATE: {
+            
+                state->update = 1;
+                break;
+            
+            }
+            
             default: {
             
                 report_at (program_name, 0, REPORT_ERROR, "unsupported option '%s'", r);
diff --git a/parted.c b/parted.c
index dd809e7..acb2701 100644
--- a/parted.c
+++ b/parted.c
@@ -430,7 +430,7 @@ int main (int argc, char **argv) {
     state->image_size += 512;
     state->image_size += sizeof (footer);
     
-    if ((ofp = fopen (state->outfile, "r+b")) == NULL) {
+    if (!state->update || (ofp = fopen (state->outfile, "r+b")) == NULL) {
     
         void *zero;
         long len;
diff --git a/parted.h b/parted.h
index bee4997..07cbcf7 100644
--- a/parted.h
+++ b/parted.h
@@ -19,7 +19,7 @@ struct parted_state {
     long nb_parts;
     
     long image_size;
-    int chs_align;
+    int chs_align, update;
 
 };
 
-- 
2.34.1