commit c8ed2fb544ff786fbe9fe095edf58d25248fa78a
parent 02a49ca2da7e7def1e268082ae5396cd5f4a848c
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date: Fri, 25 Sep 2020 16:19:59 +1000
bspatch: simplify bzip functions
- All the different bz2err vars are changed into just one.
- Closing the files at the end is done using a new macro BZ_CLOSE,
along with added error reporting.
Diffstat:
1 file changed, 15 insertions(+), 11 deletions(-)
diff --git a/bspatch.c b/bspatch.c
@@ -68,7 +68,7 @@ main(int argc, char **argv)
{
FILE *f, *cpf, *dpf, *epf;
BZFILE *cpfbz2, *dpfbz2, *epfbz2;
- int cbz2err, dbz2err, ebz2err;
+ int bzerr;
FILE *newfile;
struct stat st;
off_t oldsize, newsize;
@@ -145,8 +145,8 @@ main(int argc, char **argv)
while (newpos < newsize) {
/* Read control data */
for (i = 0; i <= 2; i++) {
- lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
- if (lenread < 8 || (cbz2err != BZ_OK && cbz2err != BZ_STREAM_END))
+ lenread = BZ2_bzRead(&bzerr, cpfbz2, buf, 8);
+ if (lenread < 8 || (bzerr != BZ_OK && bzerr != BZ_STREAM_END))
errx(1, "Corrupt patch");
ctrl[i] = offtin(buf);
}
@@ -158,8 +158,8 @@ main(int argc, char **argv)
errx(1, "Corrupt patch");
/* Read diff string */
- lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
- if (lenread < ctrl[0] || (dbz2err != BZ_OK && dbz2err != BZ_STREAM_END))
+ lenread = BZ2_bzRead(&bzerr, dpfbz2, new + newpos, ctrl[0]);
+ if (lenread < ctrl[0] || (bzerr != BZ_OK && bzerr != BZ_STREAM_END))
errx(1, "Corrupt patch");
/* Add old data to diff string */
@@ -176,8 +176,8 @@ main(int argc, char **argv)
errx(1, "Corrupt patch");
/* Read extra string */
- lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
- if (lenread < ctrl[1] || (ebz2err != BZ_OK && ebz2err != BZ_STREAM_END))
+ lenread = BZ2_bzRead(&bzerr, epfbz2, new + newpos, ctrl[1]);
+ if (lenread < ctrl[1] || (bzerr != BZ_OK && bzerr != BZ_STREAM_END))
errx(1, "Corrupt patch");
/* Adjust pointers */
@@ -186,11 +186,15 @@ main(int argc, char **argv)
}
/* Clean up the bzip2 reads */
- BZ2_bzReadClose(&cbz2err, cpfbz2);
- BZ2_bzReadClose(&dbz2err, dpfbz2);
- BZ2_bzReadClose(&ebz2err, epfbz2);
- if (fclose(cpf) || fclose(dpf) || fclose(epf))
+#define BZ_CLOSE(f, bzf) \
+ BZ2_bzReadClose(&bzerr, (bzf)); \
+ if (bzerr != BZ_OK) \
+ err(1, "BZ2_bzReadClose(%s) returned %d", argv[3], bzerr); \
+ if (fclose((f)) == EOF) \
err(1, "fclose(%s)", argv[3]);
+ BZ_CLOSE(cpf, cpfbz2)
+ BZ_CLOSE(dpf, dpfbz2)
+ BZ_CLOSE(epf, epfbz2)
/* Write the new file */
if (fwrite(new, 1, newsize, newfile) != (size_t)newsize)