X-Git-Url: https://git.distorted.org.uk/~mdw/dvdrip/blobdiff_plain/be15bd145f610c88dc2e17698b3863904872a3f0..refs/heads/mdw/cleanup:/lib.c diff --git a/lib.c b/lib.c index 55c4b48..5bf0394 100644 --- a/lib.c +++ b/lib.c @@ -98,6 +98,26 @@ long parse_int(const char **p_inout, unsigned f, return (x); } +/*----- Resizing buffers and arrays ---------------------------------------*/ + +void buf__grow(struct buf *b) +{ + b->sz = b->sz ? 2*b->sz : 32; + b->p = realloc(b->p, b->sz); + if (!b->p) bail("out of memory allocating %zu bytes", b->sz); +} + +void *vec__grow(void *p, size_t esz, size_t *sz_inout) +{ + size_t sz = *sz_inout, want; + + sz = sz ? 2*sz : 32; + want = sz*esz; + p = realloc(p, want); + if (!p) bail("out of memory allocating %zu bytes", want); + *sz_inout = sz; return (p); +} + /*----- System utilities --------------------------------------------------*/ void sit(double t) @@ -111,6 +131,25 @@ void sit(double t) } } +double tvdiff(const struct timeval *tv_lo, const struct timeval *tv_hi) +{ + return ((tv_hi->tv_sec - tv_lo->tv_sec) + + (tv_hi->tv_usec - tv_lo->tv_usec)/1.0e6); +} + +int read_line(FILE *fp, struct buf *b) +{ + int ch; + + ch = getc(fp); + if (ch == EOF) + return (-1); + else if (ch != '\n') + do { buf_putc(b, ch); ch = getc(fp); } while (ch != EOF && ch != '\n'); + buf_putz(b); + return (0); +} + void carefully_write(int fd, const void *buf, size_t sz) { const unsigned char *p = buf; @@ -221,12 +260,12 @@ static void logfn(void *p, dvd_logger_level_t lev, static const dvd_logger_cb logger = { logfn }; #endif -void open_dvd(const char *device, int mode, - int *fd_out, dvd_reader_t **dvd_out) +int open_dvd(const char *device, int mode, + int *fd_out, dvd_reader_t **dvd_out) { - int fd; - dvd_reader_t *dvd; - int bannerp = 0; + int fd = -1; + dvd_reader_t *dvd = 0; + int bannerp = 0, rc; for (;;) { fd = open(device, mode); @@ -238,18 +277,32 @@ void open_dvd(const char *device, int mode, sit(0.2); } if (bannerp) hide_banner(); - if (fd < 0) bail_syserr(errno, "failed to open device `%s'", device); + + if (fd < 0) { + moan_syserr(errno, "failed to open device `%s'", device); + rc = -1; goto end; + } + if (dvd_out) { #ifdef notdef dvd = DVDOpen2(0, &logger, device); #else dvd = DVDOpen(device); #endif - if (!dvd) bail("failed to open DVD on `%s'", device); - *dvd_out = dvd; + if (!dvd) { + moan("failed to open DVD on `%s'", device); + rc = -1; goto end; + } } - if (fd_out) *fd_out = fd; - else close(fd); + + if (fd_out) { *fd_out = fd; fd = -1; } + if (dvd_out) { *dvd_out = dvd; dvd = 0; } + rc = 0; + +end: + if (fd >= 0) close(fd); + DVDClose(dvd); + return (rc); } void store_filename(char *buf, ident id)