chkdvdimg.c: Factor out `set_status' subroutine.
[dvdrip] / dvd-sector-copy.c
index 4c19389..a608410 100644 (file)
@@ -8,30 +8,6 @@ static void usage(FILE *fp)
          prog);
 }
 
-static 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);
-}
-
-#define DEFVEC(vtype, etype)                                           \
-       typedef struct { etype *v; size_t n, sz; } vtype
-#define VEC_INIT { 0, 0, 0 }
-#define VEC_FREE(vv) do {                                              \
-  free((vv)->v); (vv)->v 0; (vv)->n = (vv)->sz = 0;                    \
-} while (0)
-#define VEC_PUSH(p, vv) do {                                           \
-  size_t _want;                                                                \
-  if ((vv)->n >= (vv)->sz) {                                           \
-    (vv)->sz = (vv)->sz ? 2*(vv)->sz : 32;                             \
-    _want = (vv)->sz*sizeof(*(vv)->v);                                 \
-    (vv)->v = realloc((vv)->v, _want);                                 \
-    if (!(vv)->v) bail("out of memory allocating %zu bytes", _want);   \
-  }                                                                    \
-  (p) = &(vv)->v[(vv)->n++];                                           \
-} while (0)
-
 #define MAXFILES (1 + 2*99 + 1)
 struct file {
   ident id;
@@ -347,7 +323,10 @@ static ssize_t read_sectors(secaddr pos, void *buf, secaddr want)
 #endif
       if (pos < bad->start) { D( printf("high\n"); ) best = bad; hi = mid; }
       else if (pos >= bad->end) { D( printf("low\n"); ) lo = mid + 1; }
-      else { D( printf("match!\n"); ) errno = EIO; return (-1); }
+      else {
+       D( printf("match!\n"); )
+       errno = EIO; sit(bad_block_delay); return (-1);
+      }
     }
 #ifdef DEBUG
     if (best)
@@ -730,16 +709,16 @@ static ssize_t find_good_sector(secaddr *pos_inout, secaddr end,
     if (n == want) good = pos;
     else bad_hi = pos + n + 1;
   }
-  recovered(bad_lo, bad_hi); *pos_inout = good;
-  if (good < r.pos + r.start || r.pos + r.end <= good)
+  recovered(bad_lo, bad_hi); *pos_inout = bad_hi;
+  if (bad_hi < r.pos + r.start || r.pos + r.end <= bad_hi)
     n = 0;
   else {
-    n = r.pos + r.end - good;
-    rearrange_sectors(&r, 0, good - r.pos, n);
+    n = r.pos + r.end - bad_hi;
+    rearrange_sectors(&r, 0, bad_hi - r.pos, n);
   }
 #ifdef DEBUG
   show_recovery_buffer_map(&r, "returning %zd good sectors at %"PRIuSEC"",
-                          n, good);
+                          n, bad_hi);
 #endif
   return (n);
 }
@@ -826,41 +805,6 @@ static void emit(secaddr start, secaddr end)
 #undef BUFSECTORS
 }
 
-struct buf {
-  char *p;
-  size_t n, sz;
-};
-#define BUF_INIT { 0, 0, 0 }
-#define BUF_REWIND(b) do { (b)->n = 0; } while (0)
-#define BUF_FREE(b) do {                                               \
-  buf *_b = (b);                                                       \
-  free(_b->p); _b->p = 0; _b->n = _b->sz = 0;                          \
-} while (0)
-#define BUF_PUTC(b, ch) do {                                           \
-  struct buf *_b = (b);                                                        \
-  if (_b->n >= _b->sz) {                                               \
-    _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);    \
-  }                                                                    \
-  _b->p[_b->n] = (ch);                                                 \
-} while (0)
-
-static 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); b->n++;
-    ch = getc(fp);
-  } while (ch != EOF && ch != '\n');
-  BUF_PUTC(b, 0);
-  return (0);
-}
-
 #define PRF_HYPHEN 1u
 static int parse_range(const char *p, unsigned f,
                       secaddr *start_out, secaddr *end_out)
@@ -1023,7 +967,7 @@ int main(int argc, char *argv[])
          bail_syserr(errno, "failed to open ranges file `%s'", optarg);
        i = 0; last = -1;
        for (;;) {
-         BUF_REWIND(&buf); if (read_line(fp, &buf)) break;
+         buf_rewind(&buf); if (read_line(fp, &buf)) break;
          p = buf.p; i++;
          while (ISSPACE(*p)) p++;
          if (!*p || *p == '#') continue;
@@ -1049,7 +993,7 @@ int main(int argc, char *argv[])
          bail_syserr(errno, "failed to open bad-blocks file `%s'", optarg);
        i = 0; last = -1;
        for (;;) {
-         BUF_REWIND(&buf); if (read_line(fp, &buf)) break;
+         buf_rewind(&buf); if (read_line(fp, &buf)) break;
          p = buf.p; i++;
          while (ISSPACE(*p)) p++;
          if (!*p || *p == '#') continue;
@@ -1099,7 +1043,7 @@ int main(int argc, char *argv[])
 #endif
   }
 
-  open_dvd(device, &dvdfd, &dvd);
+  open_dvd(device, O_RDONLY, &dvdfd, &dvd);
 
   blksz = SECTORSZ; volsz = device_size(dvdfd, device, &blksz);
   if (blksz != SECTORSZ)
@@ -1109,7 +1053,7 @@ int main(int argc, char *argv[])
         device, volsz, SECTORSZ);
 
   if (f&f_checkid) {
-    open_dvd(outfile, 0, &dvd_out);
+    open_dvd(outfile, O_RDONLY, 0, &dvd_out);
     if (dvd_id(id_in, dvd, DIF_MUSTIFOHASH, device) ||
        dvd_id(id_out, dvd_out, DIF_MUSTIFOHASH, device))
       exit(2);