3 const char *prog = "<unset>";
5 void set_prog(const char *p)
6 { const char *q = strrchr(p, '/'); prog = q ? q + 1 : p; }
8 void vmoan(const char *fmt, va_list ap)
9 { vmoan_syserr(0, fmt, ap); }
11 void vmoan_syserr(int err, const char *fmt, va_list ap)
13 fprintf(stderr, "%s: ", prog);
14 vfprintf(stderr, fmt, ap);
15 if (err) fprintf(stderr, ": %s", strerror(errno));
19 void moan(const char *fmt, ...)
20 { va_list ap; va_start(ap, fmt); vmoan(fmt, ap); va_end(ap); }
22 void moan_syserr(int err, const char *fmt, ...)
23 { va_list ap; va_start(ap, fmt); vmoan_syserr(err, fmt, ap); va_end(ap); }
25 void bail(const char *fmt, ...)
26 { va_list ap; va_start(ap, fmt); vmoan(fmt, ap); va_end(ap); exit(2); }
28 void bail_syserr(int err, const char *fmt, ...)
32 va_start(ap, fmt); vmoan_syserr(err, fmt, ap); va_end(ap);
36 double parse_float(const char **p_inout, unsigned f,
37 double min, double max, const char *what)
44 err = errno; errno = 0;
47 if (errno || x < min || x > max || (!(f&PNF_JUNK) && *q))
48 bail("bad %s `%s'", what, p);
49 *p_inout = q; errno = err;
53 long parse_int(const char **p_inout, unsigned f,
54 long min, long max, const char *what)
61 err = errno; errno = 0;
63 x = strtoul(p, &q, 0);
64 if (errno || x < min || x > max || (!(f&PNF_JUNK) && *q))
65 bail("bad %s `%s'", what, p);
66 *p_inout = q; errno = err;
73 double whole = floor(t);
76 tv.tv_sec = whole; tv.tv_usec = floor((t - whole)*1.0e6) + 1;
77 if (select(0, 0, 0, 0, &tv) < 0) bail_syserr(errno, "failed to sleep");
81 void carefully_write(int fd, const void *buf, size_t sz)
83 const unsigned char *p = buf;
90 if (errno == EINTR) continue;
91 bail_syserr(errno, "failed to write to output file");
93 if (!n) bail("unexpected short write to output file");
98 void open_file_on_demand(const char *file, FILE **fp_inout, const char *what)
103 fp = fopen(file, "w");
104 if (!fp) bail_syserr(errno, "failed to open %s file `%s'", what, file);
105 fprintf(fp, "## %s\n\n", what);
110 void check_write(FILE *fp, const char *what)
113 if (ferror(fp)) bail_syserr(errno, "error writing %s file", what);
116 void carefully_fclose(FILE *fp, const char *what)
118 if (fp && (ferror(fp) || fclose(fp)))
119 bail_syserr(errno, "error writing %s file", what);
122 off_t device_size(int fd, const char *file, int *blksz_out)
128 bail_syserr(errno, "failed to obtain status for `%s'", file);
129 if (S_ISREG(st.st_mode))
131 else if (S_ISBLK(st.st_mode)) {
132 if (ioctl(fd, BLKGETSIZE64, &volsz))
133 bail_syserr(errno, "failed to get volume size for `%s'", file);
134 if (ioctl(fd, BLKSSZGET, blksz_out))
135 bail_syserr(errno, "failed to get block size for `%s'", file);
137 bail("can't read size for `%s': expected file or block device", file);
138 return ((off_t)volsz);
141 void store_filename(char *buf, ident id)
143 switch (id_kind(id)) {
145 sprintf(buf, "#<raw device>");
148 if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.IFO");
149 else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.IFO", id_title(id));
152 if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.BUP");
153 else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.BUP", id_title(id));
156 if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.VOB");
158 sprintf(buf, "/VIDEO_TS/VTS_%02u_%u.VOB", id_title(id), id_part(id));
165 static char *copy_string(char *p, const char *q)
167 while (*q) *p++ = *q++;
171 static char *copy_hex(char *p, const unsigned char *q, size_t sz)
174 sprintf(p, "%02x", *q);
180 int dvd_id(char *p, dvd_reader_t *dvd, unsigned f, const char *file)
183 unsigned char volsetid[16], discid[16];
187 rc = DVDUDFVolumeInfo(dvd,
188 volid, sizeof(volid),
189 volsetid, sizeof(volsetid));
191 p = copy_string(p, volid);
193 for (n = sizeof(volsetid); n && !volsetid[n - 1]; n--);
194 p = copy_hex(p, volsetid, n);
195 } else if (f&DIF_MUSTVOLINF) {
196 if (file) moan("failed to read volume info for `%s'", file);
197 else moan("failed to read volume info");
200 p = copy_string(p, "<error reading volume info>");
203 rc = DVDDiscID(dvd, discid);
205 p = copy_hex(p, discid, sizeof(discid));
206 else if (f&DIF_MUSTIFOHASH) {
207 if (file) moan("failed to determine disc id of `%s'", file);
208 else moan("failed to determine disc id");
211 p = copy_string(p, "<error reading disc-id>");
216 struct progress_state progress = PROGRESS_STATE_INIT;
217 static struct banner_progress_item banner_progress;
219 static void render_banner_progress(struct progress_item *item,
220 struct progress_render_state *render)
222 struct banner_progress_item *bi = (struct banner_progress_item *)item;
224 progress_putleft(render, " %s", bi->msg);
225 progress_shownotice(render, 4, 7);
228 void show_banner(const char *msg)
230 banner_progress._base.render = render_banner_progress;
231 progress_additem(&progress, &banner_progress._base);
232 banner_progress.msg = msg;
233 progress_update(&progress);
236 void hide_banner(void)
238 if (!progress_removeitem(&progress, &banner_progress._base))
239 progress_update(&progress);
243 static void logfn(void *p, dvd_logger_level_t lev,
244 const char *fmt, va_list ap)
247 case DVD_LOGGER_LEVEL_ERROR:
248 fprintf("%s (libdvdread error): ", prog);
250 case DVD_LOGGER_LEVEL_WARN:
251 fprintf("%s (libdvdread warning): ", prog);
256 vfprintf(stderr, fmt, ap);
259 static const dvd_logger_cb logger = { logfn };
262 void open_dvd(const char *device, int *fd_out, dvd_reader_t **dvd_out)
269 fd = open(device, O_RDONLY);
270 if (fd >= 0 || errno != ENOMEDIUM) break;
272 show_banner("Waiting for disc to be inserted...");
277 if (bannerp) hide_banner();
278 if (fd < 0) bail_syserr(errno, "failed to open device `%s'", device);
281 dvd = DVDOpen2(0, &logger, device);
283 dvd = DVDOpen(device);
285 if (!dvd) bail("failed to open DVD on `%s'", device);
288 if (fd_out) *fd_out = fd;