From 00a5be1d63549628d56e33118d54f1a0f650b312 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sun, 6 Mar 2022 01:08:01 +0000 Subject: [PATCH] dvd-sector-copy.c, lib.[ch]: Move the file hacking functions to the library. --- dvd-sector-copy.c | 43 ------------------------------------------- lib.c | 41 +++++++++++++++++++++++++++++++++++++++++ lib.h | 6 ++++++ 3 files changed, 47 insertions(+), 43 deletions(-) diff --git a/dvd-sector-copy.c b/dvd-sector-copy.c index 76dfa13..ea34461 100644 --- a/dvd-sector-copy.c +++ b/dvd-sector-copy.c @@ -15,49 +15,6 @@ static double tvdiff(const struct timeval *tv_lo, (tv_hi->tv_usec - tv_lo->tv_usec)/1.0e6); } -static void carefully_write(int fd, const void *buf, size_t sz) -{ - const unsigned char *p = buf; - ssize_t n; - - if (fd < 0) return; - while (sz) { - n = write(fd, p, sz); - if (n < 0) { - if (errno == EINTR) continue; - bail_syserr(errno, "failed to write to output file"); - } - if (!n) bail("unexpected short write to output file"); - p += n; sz -= n; - } -} - -static void open_file_on_demand(const char *file, FILE **fp_inout, - const char *what) -{ - FILE *fp; - - if (!*fp_inout) { - fp = fopen(file, "w"); - if (!fp) - bail_syserr(errno, "failed to open %s file `%s'", what, file); - fprintf(fp, "## %s\n\n", what); - *fp_inout = fp; - } -} - -static void check_write(FILE *fp, const char *what) -{ - fflush(fp); - if (ferror(fp)) bail_syserr(errno, "error writing %s file", what); -} - -static void carefully_fclose(FILE *fp, const char *what) -{ - if (fp && (ferror(fp) || fclose(fp))) - bail_syserr(errno, "error writing %s file", what); -} - #define DEFVEC(vtype, etype) \ typedef struct { etype *v; size_t n, sz; } vtype #define VEC_INIT { 0, 0, 0 } diff --git a/lib.c b/lib.c index 1c815d0..7fecbc4 100644 --- a/lib.c +++ b/lib.c @@ -49,6 +49,47 @@ void sit(double t) } } +void carefully_write(int fd, const void *buf, size_t sz) +{ + const unsigned char *p = buf; + ssize_t n; + + if (fd < 0) return; + while (sz) { + n = write(fd, p, sz); + if (n < 0) { + if (errno == EINTR) continue; + bail_syserr(errno, "failed to write to output file"); + } + if (!n) bail("unexpected short write to output file"); + p += n; sz -= n; + } +} + +void open_file_on_demand(const char *file, FILE **fp_inout, const char *what) +{ + FILE *fp; + + if (!*fp_inout) { + fp = fopen(file, "w"); + if (!fp) bail_syserr(errno, "failed to open %s file `%s'", what, file); + fprintf(fp, "## %s\n\n", what); + *fp_inout = fp; + } +} + +void check_write(FILE *fp, const char *what) +{ + fflush(fp); + if (ferror(fp)) bail_syserr(errno, "error writing %s file", what); +} + +void carefully_fclose(FILE *fp, const char *what) +{ + if (fp && (ferror(fp) || fclose(fp))) + bail_syserr(errno, "error writing %s file", what); +} + void store_filename(char *buf, ident id) { switch (id_kind(id)) { diff --git a/lib.h b/lib.h index 1f08848..c3ed607 100644 --- a/lib.h +++ b/lib.h @@ -72,6 +72,12 @@ extern PRINTF_LIKE(2, 3) NORETURN extern void sit(double t); +extern void carefully_write(int fd, const void *buf, size_t sz); +extern void open_file_on_demand(const char *file, FILE **fp_inout, + const char *what); +extern void check_write(FILE *fp, const char *what); +extern void carefully_fclose(FILE *fp, const char *what); + enum { RAW, IFO, VOB, BUP }; typedef uint_least32_t ident; -- 2.11.0