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)
(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;
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);
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)