Bug Summary

File:src/usr.bin/cdio/cdio.c
Warning:line 300, column 11
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name cdio.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/cdio/obj -resource-dir /usr/local/llvm16/lib/clang/16 -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/cdio/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fno-jump-tables -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/scan/2024-01-11-140451-98009-1 -x c /usr/src/usr.bin/cdio/cdio.c
1/* $OpenBSD: cdio.c,v 1.86 2023/03/08 04:43:10 guenther Exp $ */
2
3/* Copyright (c) 1995 Serge V. Vakulenko
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Serge V. Vakulenko.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Compact Disc Control Utility by Serge V. Vakulenko <vak@cronyx.ru>.
35 * Based on the non-X based CD player by Jean-Marc Zucconi and
36 * Andrey A. Chernov.
37 *
38 * Fixed and further modified on 5-Sep-1995 by Jukka Ukkonen <jau@funet.fi>.
39 *
40 * 11-Sep-1995: Jukka A. Ukkonen <jau@funet.fi>
41 * A couple of further fixes to my own earlier "fixes".
42 *
43 * 18-Sep-1995: Jukka A. Ukkonen <jau@funet.fi>
44 * Added an ability to specify addresses relative to the
45 * beginning of a track. This is in fact a variation of
46 * doing the simple play_msf() call.
47 *
48 * 11-Oct-1995: Serge V.Vakulenko <vak@cronyx.ru>
49 * New eject algorithm.
50 * Some code style reformatting.
51 *
52 * $FreeBSD: cdcontrol.c,v 1.13 1996/06/25 21:01:27 ache Exp $
53 */
54
55#include <sys/types.h>
56#include <sys/cdio.h>
57#include <sys/ioctl.h>
58#include <sys/queue.h>
59#include <sys/scsiio.h>
60#include <sys/stat.h>
61
62#include <ctype.h>
63#include <err.h>
64#include <errno(*__errno()).h>
65#include <fcntl.h>
66#include <signal.h>
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70#include <unistd.h>
71#include <limits.h>
72#include <histedit.h>
73#include <util.h>
74#include <vis.h>
75
76#include "extern.h"
77
78#define ASTS_INVALID0x00 0x00 /* Audio status byte not valid */
79#define ASTS_PLAYING0x11 0x11 /* Audio play operation in progress */
80#define ASTS_PAUSED0x12 0x12 /* Audio play operation paused */
81#define ASTS_COMPLETED0x13 0x13 /* Audio play operation successfully completed */
82#define ASTS_ERROR0x14 0x14 /* Audio play operation stopped due to error */
83#define ASTS_VOID0x15 0x15 /* No current audio status to return */
84
85#ifndef DEFAULT_CD_DRIVE"cd0"
86# define DEFAULT_CD_DRIVE"cd0" "cd0"
87#endif
88
89#define CMD_DEBUG1 1
90#define CMD_DEVICE2 2
91#define CMD_EJECT3 3
92#define CMD_HELP4 4
93#define CMD_INFO5 5
94#define CMD_PAUSE6 6
95#define CMD_PLAY7 7
96#define CMD_QUIT8 8
97#define CMD_RESUME9 9
98#define CMD_STOP10 10
99#define CMD_VOLUME11 11
100#define CMD_CLOSE12 12
101#define CMD_RESET13 13
102#define CMD_SET14 14
103#define CMD_STATUS15 15
104#define CMD_NEXT16 16
105#define CMD_PREV17 17
106#define CMD_REPLAY18 18
107#define CMD_CDDB19 19
108#define CMD_CDID20 20
109#define CMD_BLANK21 21
110#define CMD_CDRIP22 22
111#define CMD_CDPLAY23 23
112
113struct cmdtab {
114 int command;
115 char *name;
116 int min;
117 char *args;
118} cmdtab[] = {
119{ CMD_BLANK21, "blank", 1, "" },
120{ CMD_CDDB19, "cddbinfo", 2, "[n]" },
121{ CMD_CDID20, "cdid", 3, "" },
122{ CMD_CDPLAY23, "cdplay", 3, "[track1-trackN ...]" },
123{ CMD_CDRIP22, "cdrip", 3, "[track1-trackN ...]" },
124{ CMD_CLOSE12, "close", 1, "" },
125{ CMD_DEBUG1, "debug", 3, "on | off" },
126{ CMD_DEVICE2, "device", 1, "devname" },
127{ CMD_EJECT3, "eject", 1, "" },
128{ CMD_QUIT8, "exit", 2, "" },
129{ CMD_HELP4, "?", 1, 0 },
130{ CMD_HELP4, "help", 1, "" },
131{ CMD_INFO5, "info", 1, "" },
132{ CMD_NEXT16, "next", 1, "" },
133{ CMD_PAUSE6, "pause", 2, "" },
134{ CMD_PLAY7, "play", 1, "[track1[.index1] [track2[.index2]]]" },
135{ CMD_PLAY7, "play", 1, "[[tr1] m1:s1[.f1] [tr2] [m2:s2[.f2]]]" },
136{ CMD_PLAY7, "play", 1, "[#block [len]]" },
137{ CMD_PREV17, "previous", 2, "" },
138{ CMD_QUIT8, "quit", 1, "" },
139{ CMD_REPLAY18, "replay", 3, "" },
140{ CMD_RESET13, "reset", 4, "" },
141{ CMD_RESUME9, "resume", 1, "" },
142{ CMD_SET14, "set", 2, "lba | msf" },
143{ CMD_STATUS15, "status", 1, "" },
144{ CMD_STOP10, "stop", 3, "" },
145{ CMD_VOLUME11, "volume", 1, "left_channel right_channel" },
146{ CMD_VOLUME11, "volume", 1, "left | right | mono | stereo | mute" },
147{ 0, 0, 0, 0}
148};
149
150struct cd_toc_entry *toc_buffer;
151
152struct track_head tracks;
153
154char *cdname;
155int fd = -1;
156int writeperm = 0;
157u_int8_t mediacap[MMC_FEATURE_MAX0x0110 / NBBY8];
158int verbose = 1;
159int msf = 1;
160const char *cddb_host;
161char **track_names;
162volatile sig_atomic_t signo;
163
164EditLine *el = NULL((void *)0); /* line-editing structure */
165History *hist = NULL((void *)0); /* line-editing history */
166void switch_el(void);
167
168extern char *__progname;
169
170int setvol(int, int);
171int read_toc_entrys(int);
172int play_msf(int, int, int, int, int, int);
173int play_track(int, int, int, int);
174int status(int *, int *, int *, int *);
175int is_wave(int);
176__dead__attribute__((__noreturn__)) void tao(int argc, char **argv);
177int play(char *arg);
178int info(char *arg);
179int cddbinfo(char *arg);
180int pstatus(char *arg);
181int play_next(char *arg);
182int play_prev(char *arg);
183int play_same(char *arg);
184void sigint_handler(int);
185char *input(int *);
186char *prompt(void);
187void prtrack(struct cd_toc_entry *e, int lastflag, char *name);
188void lba2msf(unsigned long lba, u_char *m, u_char *s, u_char *f);
189unsigned int msf2lba(u_char m, u_char s, u_char f);
190int play_blocks(int blk, int len);
191int run(int cmd, char *arg);
192char *parse(char *buf, int *cmd);
193void help(void);
194void usage(void);
195char *strstatus(int);
196int cdid(void);
197void addmsf(u_int *, u_int *, u_int *, u_char, u_char, u_char);
198int cmpmsf(u_char, u_char, u_char, u_char, u_char, u_char);
199void toc2msf(u_int, u_char *, u_char *, u_char *);
200
201void
202help(void)
203{
204 struct cmdtab *c;
205 char *s, n;
206 int i;
207
208 for (c = cmdtab; c->name; ++c) {
209 if (!c->args)
210 continue;
211 printf("\t");
212 for (i = c->min, s = c->name; *s; s++, i--) {
213 if (i > 0)
214 n = toupper((unsigned char)*s);
215 else
216 n = *s;
217 putchar(n)(!__isthreaded ? __sputc(n, (&__sF[1])) : (putc)(n, (&
__sF[1])))
;
218 }
219 if (*c->args)
220 printf(" %s", c->args);
221 printf("\n");
222 }
223 printf("\n\tThe word \"play\" is not required for the play commands.\n");
224 printf("\tThe plain target address is taken as a synonym for play.\n");
225}
226
227void
228usage(void)
229{
230 fprintf(stderr(&__sF[2]), "usage: %s [-sv] [-d host:port] [-f device] "
231 "[command [arg ...]]\n",
232 __progname);
233 exit(1);
234}
235
236int
237main(int argc, char **argv)
238{
239 int ch, cmd;
1
'cmd' declared without an initial value
240 char *arg;
241
242 cdname = getenv("DISC");
2
Assuming the environment variable does not exist
243 if (!cdname
2.1
'cdname' is null
)
3
Taking true branch
244 cdname = getenv("CDROM");
4
Assuming the environment variable does not exist
245
246 cddb_host = getenv("CDDB");
5
Assuming the environment variable does not exist
247 if (!cddb_host
5.1
'cddb_host' is null
)
6
Taking true branch
248 cddb_host = "gnudb.gnudb.org";
249
250 while ((ch = getopt(argc, argv, "svd:f:")) != -1)
7
Assuming the condition is false
8
Loop condition is false. Execution continues on line 268
251 switch (ch) {
252 case 's':
253 verbose = 0;
254 break;
255 case 'v':
256 verbose++;
257 break;
258 case 'f':
259 cdname = optarg;
260 break;
261 case 'd':
262 cddb_host = optarg;
263 break;
264 default:
265 usage();
266 }
267
268 argc -= optind;
269 argv += optind;
270
271 if (argc > 0 && ! strcasecmp(*argv, "help"))
9
Assuming 'argc' is > 0
10
Assuming the condition is false
11
Taking false branch
272 usage();
273
274 if (!cdname
11.1
'cdname' is null
) {
12
Taking true branch
275 cdname = DEFAULT_CD_DRIVE"cd0";
276 if (verbose
12.1
'verbose' is <= 1
> 1)
277 fprintf(stderr(&__sF[2]),
278 "No CD device name specified. Defaulting to %s.\n",
279 cdname);
280 }
281
282 if (argc
12.2
'argc' is > 0
> 0 && !strcasecmp(*argv, "tao")) {
13
Assuming the condition is false
14
Taking false branch
283 tao(argc, argv);
284 /* NOTREACHED */
285 }
286 if (argc
14.1
'argc' is > 0
> 0) {
15
Taking true branch
287 char buf[80], *p;
288 int len;
289
290 for (p=buf; argc-->0; ++argv) {
16
Loop condition is true. Entering loop body
21
Assuming the condition is false
22
Loop condition is false. Execution continues on line 299
291 len = snprintf(p, buf + sizeof buf - p,
292 "%s%s", (p
16.1
'p' is <= 'buf'
> buf) ? " " : "", *argv);
17
'?' condition is false
293
294 if (len < 0 || len >= buf + sizeof buf - p)
18
Assuming 'len' is >= 0
19
Assuming the condition is false
20
Taking false branch
295 errx(1, "argument list too long.");
296
297 p += len;
298 }
299 arg = parse(buf, &cmd);
23
Calling 'parse'
30
Returning from 'parse'
300 return (run(cmd, arg));
31
1st function call argument is an uninitialized value
301 }
302
303 if (verbose == 1)
304 verbose = isatty(0);
305
306 if (verbose) {
307 printf("Compact Disc Control utility, version %s\n", VERSION"2.1");
308 printf("Type `?' for command list\n\n");
309 }
310
311 switch_el();
312
313 for (;;) {
314 arg = input(&cmd);
315 if (run(cmd, arg) < 0) {
316 if (verbose)
317 warn(NULL((void *)0));
318 close(fd);
319 fd = -1;
320 }
321 fflush(stdout(&__sF[1]));
322 }
323}
324
325int
326run(int cmd, char *arg)
327{
328 int l, r, rc;
329 static char newcdname[PATH_MAX1024];
330
331 switch (cmd) {
332
333 case CMD_QUIT8:
334 switch_el();
335 exit(0);
336
337 case CMD_INFO5:
338 if (!open_cd(cdname, 0))
339 return (0);
340
341 return info(arg);
342
343 case CMD_CDDB19:
344 if (!open_cd(cdname, 0))
345 return (0);
346
347 return cddbinfo(arg);
348
349 case CMD_CDID20:
350 if (!open_cd(cdname, 0))
351 return (0);
352 return cdid();
353
354 case CMD_STATUS15:
355 if (!open_cd(cdname, 0))
356 return (0);
357
358 return pstatus(arg);
359
360 case CMD_PAUSE6:
361 if (!open_cd(cdname, 0))
362 return (0);
363
364 return ioctl(fd, CDIOCPAUSE((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((19)))
);
365
366 case CMD_RESUME9:
367 if (!open_cd(cdname, 0))
368 return (0);
369
370 return ioctl(fd, CDIOCRESUME((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((20)))
);
371
372 case CMD_STOP10:
373 if (!open_cd(cdname, 0))
374 return (0);
375
376 rc = ioctl(fd, CDIOCSTOP((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((23)))
);
377
378 (void) ioctl(fd, CDIOCALLOW((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((25)))
);
379
380 return (rc);
381
382 case CMD_RESET13:
383 if (!open_cd(cdname, 0))
384 return (0);
385
386 rc = ioctl(fd, CDIOCRESET((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((21)))
);
387 if (rc == -1)
388 return rc;
389 close(fd);
390 fd = -1;
391 return (0);
392
393 case CMD_DEBUG1:
394 if (!open_cd(cdname, 0))
395 return (0);
396
397 if (!strcasecmp(arg, "on"))
398 return ioctl(fd, CDIOCSETDEBUG((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((17)))
);
399
400 if (!strcasecmp(arg, "off"))
401 return ioctl(fd, CDIOCCLRDEBUG((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((18)))
);
402
403 printf("%s: Invalid command arguments\n", __progname);
404
405 return (0);
406
407 case CMD_DEVICE2:
408 /* close old device */
409 if (fd > -1) {
410 (void) ioctl(fd, CDIOCALLOW((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((25)))
);
411 close(fd);
412 fd = -1;
413 }
414
415 if (strlen(arg) == 0) {
416 printf("%s: Invalid parameter\n", __progname);
417 return (0);
418 }
419
420 /* open new device */
421 if (!open_cd(arg, 0))
422 return (0);
423 (void) strlcpy(newcdname, arg, sizeof(newcdname));
424 cdname = newcdname;
425 return (1);
426
427 case CMD_EJECT3:
428 if (!open_cd(cdname, 0))
429 return (0);
430
431 (void) ioctl(fd, CDIOCALLOW((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((25)))
);
432 rc = ioctl(fd, CDIOCEJECT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((24)))
);
433 if (rc == -1)
434 return (rc);
435 close(fd);
436 fd = -1;
437 if (track_names)
438 free_names(track_names);
439 track_names = NULL((void *)0);
440 return (0);
441
442 case CMD_CLOSE12:
443 if (!open_cd(cdname, 0))
444 return (0);
445
446 (void) ioctl(fd, CDIOCALLOW((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((25)))
);
447 rc = ioctl(fd, CDIOCCLOSE((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((27)))
);
448 if (rc == -1)
449 return (rc);
450 close(fd);
451 fd = -1;
452 return (0);
453
454 case CMD_PLAY7:
455 if (!open_cd(cdname, 0))
456 return (0);
457
458 while (isspace((unsigned char)*arg))
459 arg++;
460
461 return play(arg);
462
463 case CMD_SET14:
464 if (!strcasecmp(arg, "msf"))
465 msf = 1;
466 else if (!strcasecmp(arg, "lba"))
467 msf = 0;
468 else
469 printf("%s: Invalid command arguments\n", __progname);
470 return (0);
471
472 case CMD_VOLUME11:
473 if (!open_cd(cdname, 0))
474 return (0);
475
476 if (!strncasecmp(arg, "left", strlen(arg)))
477 return ioctl(fd, CDIOCSETLEFT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((15)))
);
478
479 if (!strncasecmp(arg, "right", strlen(arg)))
480 return ioctl(fd, CDIOCSETRIGHT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((16)))
);
481
482 if (!strncasecmp(arg, "mono", strlen(arg)))
483 return ioctl(fd, CDIOCSETMONO((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((12)))
);
484
485 if (!strncasecmp(arg, "stereo", strlen(arg)))
486 return ioctl(fd, CDIOCSETSTEREO((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((13)))
);
487
488 if (!strncasecmp(arg, "mute", strlen(arg)))
489 return ioctl(fd, CDIOCSETMUTE((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((14)))
);
490
491 if (2 != sscanf(arg, "%d%d", &l, &r)) {
492 printf("%s: Invalid command arguments\n", __progname);
493 return (0);
494 }
495
496 return setvol(l, r);
497
498 case CMD_NEXT16:
499 if (!open_cd(cdname, 0))
500 return (0);
501
502 return play_next(arg);
503
504 case CMD_PREV17:
505 if (!open_cd(cdname, 0))
506 return (0);
507
508 return play_prev(arg);
509
510 case CMD_REPLAY18:
511 if (!open_cd(cdname, 0))
512 return 0;
513
514 return play_same(arg);
515 case CMD_BLANK21:
516 if (!open_cd(cdname, 1))
517 return 0;
518
519 if (get_media_capabilities(mediacap, 1) == -1) {
520 warnx("Can't determine media type");
521 return (0);
522 }
523 if (cdio_isset(mediacap, MMC_FEATURE_CDRW_WRITE)((mediacap)[(0x37)>>3] & (1<<((0x37)&(8 -
1))))
== 0 &&
524 get_media_type() != MEDIATYPE_CDRW2) {
525 warnx("The media doesn't support blanking");
526 return (0);
527 }
528
529 return blank();
530 case CMD_CDRIP22:
531 if (!open_cd(cdname, 0))
532 return (0);
533
534 while (isspace((unsigned char)*arg))
535 arg++;
536
537 return cdrip(arg);
538 case CMD_CDPLAY23:
539 if (!open_cd(cdname, 0))
540 return (0);
541
542 while (isspace((unsigned char)*arg))
543 arg++;
544
545 return cdplay(arg);
546 default:
547 case CMD_HELP4:
548 help();
549 return (0);
550
551 }
552}
553
554/*
555 * Check if audio file has RIFF WAVE format. If not, we assume it's just PCM.
556 */
557int
558is_wave(int fd)
559{
560 char buf[WAVHDRLEN44];
561 int rv;
562
563 rv = 0;
564 if (read(fd, buf, sizeof(buf)) == sizeof(buf)) {
565 if (memcmp(buf, "RIFF", 4) == 0 &&
566 memcmp(buf + 8, "WAVE", 4) == 0)
567 rv = 1;
568 }
569
570 return (rv);
571}
572
573__dead__attribute__((__noreturn__)) void
574tao(int argc, char **argv)
575{
576 struct stat sb;
577 struct track_info *cur_track;
578 struct track_info *tr;
579 off_t availblk, needblk = 0;
580 u_int blklen;
581 u_int ntracks = 0;
582 char type;
583 int ch, speed;
584 const char *errstr;
585
586 if (argc == 1)
587 usage();
588
589 SLIST_INIT(&tracks){ ((&tracks)->slh_first) = ((void *)0); };
590 type = 'd';
591 speed = DRIVE_SPEED_OPTIMAL0xffff;
592 blklen = 2048;
593 while (argc > 1) {
594 tr = malloc(sizeof(struct track_info));
595 if (tr == NULL((void *)0))
596 err(1, "tao");
597
598 optreset = 1;
599 optind = 1;
600 while ((ch = getopt(argc, argv, "ads:")) != -1) {
601 switch (ch) {
602 case 'a':
603 type = 'a';
604 blklen = 2352;
605 break;
606 case 'd':
607 type = 'd';
608 blklen = 2048;
609 break;
610 case 's':
611 if (strcmp(optarg, "auto") == 0) {
612 speed = DRIVE_SPEED_OPTIMAL0xffff;
613 } else if (strcmp(optarg, "max") == 0) {
614 speed = DRIVE_SPEED_MAX0xfffe;
615 } else {
616 speed = (int)strtonum(optarg, 1,
617 CD_MAX_SPEED380, &errstr);
618 if (errstr != NULL((void *)0)) {
619 errx(1,
620 "incorrect speed value");
621 }
622 }
623 break;
624 default:
625 usage();
626 /* NOTREACHED */
627 }
628 }
629
630 if (speed != DRIVE_SPEED_OPTIMAL0xffff && speed != DRIVE_SPEED_MAX0xfffe)
631 tr->speed = CD_SPEED_TO_KBPS(speed, blklen)((speed) * 75 * (blklen) / 1024);
632 else
633 tr->speed = speed;
634
635 tr->type = type;
636 tr->blklen = blklen;
637 argc -= optind;
638 argv += optind;
639 if (argv[0] == NULL((void *)0))
640 usage();
641 tr->file = argv[0];
642 tr->fd = open(tr->file, O_RDONLY0x0000);
643 if (tr->fd == -1)
644 err(1, "cannot open file %s", tr->file);
645 if (fstat(tr->fd, &sb) == -1)
646 err(1, "cannot stat file %s", tr->file);
647 tr->sz = sb.st_size;
648 tr->off = 0;
649 if (tr->type == 'a') {
650 if (is_wave(tr->fd)) {
651 tr->sz -= WAVHDRLEN44;
652 tr->off = WAVHDRLEN44;
653 }
654 }
655 if (SLIST_EMPTY(&tracks)(((&tracks)->slh_first) == ((void *)0)))
656 SLIST_INSERT_HEAD(&tracks, tr, track_list)do { (tr)->track_list.sle_next = (&tracks)->slh_first
; (&tracks)->slh_first = (tr); } while (0)
;
657 else
658 SLIST_INSERT_AFTER(cur_track, tr, track_list)do { (tr)->track_list.sle_next = (cur_track)->track_list
.sle_next; (cur_track)->track_list.sle_next = (tr); } while
(0)
;
659 cur_track = tr;
660 }
661
662 if (!open_cd(cdname, 1))
663 exit(1);
664 if (get_media_capabilities(mediacap, 1) == -1)
665 errx(1, "Can't determine media type");
666 if (cdio_isset(mediacap, MMC_FEATURE_CD_TAO)((mediacap)[(0x2d)>>3] & (1<<((0x2d)&(8 -
1))))
== 0)
667 errx(1, "The media can't be written in TAO mode");
668
669 get_disc_size(&availblk);
670 SLIST_FOREACH(tr, &tracks, track_list)for((tr) = ((&tracks)->slh_first); (tr) != ((void *)0)
; (tr) = ((tr)->track_list.sle_next))
{
671 needblk += tr->sz/tr->blklen;
672 ntracks++;
673 }
674 needblk += (ntracks - 1) * 150; /* transition area between tracks */
675 if (needblk > availblk)
676 errx(1, "Only %llu of the required %llu blocks available",
677 availblk, needblk);
678 if (writetao(&tracks) != 0)
679 exit(1);
680 else
681 exit(0);
682}
683
684int
685play(char *arg)
686{
687 struct ioc_toc_header h;
688 unsigned char tm, ts, tf;
689 unsigned int tr1, tr2, m1, m2, s1, s2, f1, f2, i1, i2;
690 unsigned int blk, len, n;
691 char c;
692 int rc;
693
694 rc = ioctl(fd, CDIOREADTOCHEADER((unsigned long)0x40000000 | ((sizeof(struct ioc_toc_header) &
0x1fff) << 16) | ((('c')) << 8) | ((4)))
, &h);
695
696 if (rc == -1)
697 return (rc);
698
699 if (h.starting_track > h.ending_track) {
700 printf("TOC starting_track > TOC ending_track\n");
701 return (0);
702 }
703
704 n = h.ending_track - h.starting_track + 1;
705 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
706
707 if (rc < 0)
708 return (rc);
709
710 /*
711 * Truncate trailing white space. Then by adding %c to the end of the
712 * sscanf() formats we catch any errant trailing characters.
713 */
714 rc = strlen(arg) - 1;
715 while (rc >= 0 && isspace((unsigned char)arg[rc])) {
716 arg[rc] = '\0';
717 rc--;
718 }
719
720 if (!arg || ! *arg) {
721 /* Play the whole disc */
722 return (play_track(h.starting_track, 1, h.ending_track, 1));
723 }
724
725 if (strchr(arg, '#')) {
726 /* Play block #blk [ len ] */
727 if (2 != sscanf(arg, "#%u%u%c", &blk, &len, &c) &&
728 1 != sscanf(arg, "#%u%c", &blk, &c)) {
729 printf("%s: Invalid command arguments\n", __progname);
730 return (0);
731 }
732
733 if (len == 0) {
734 if (msf)
735 len = msf2lba(toc_buffer[n].addr.msf.minute,
736 toc_buffer[n].addr.msf.second,
737 toc_buffer[n].addr.msf.frame) - blk;
738 else
739 len = toc_buffer[n].addr.lba - blk;
740 }
741 return play_blocks(blk, len);
742 }
743
744 if (strchr(arg, ':') == NULL((void *)0)) {
745 /*
746 * Play track tr1[.i1] [tr2[.i2]]
747 */
748 if (4 == sscanf(arg, "%u.%u%u.%u%c", &tr1, &i1, &tr2, &i2, &c))
749 goto play_track;
750
751 i2 = 1;
752 if (3 == sscanf(arg, "%u.%u%u%c", &tr1, &i1, &tr2, &c))
753 goto play_track;
754
755 i1 = 1;
756 if (3 == sscanf(arg, "%u%u.%u%c", &tr1, &tr2, &i2, &c))
757 goto play_track;
758
759 tr2 = 0;
760 i2 = 1;
761 if (2 == sscanf(arg, "%u.%u%c", &tr1, &i1, &c))
762 goto play_track;
763
764 i1 = i2 = 1;
765 if (2 == sscanf(arg, "%u%u%c", &tr1, &tr2, &c))
766 goto play_track;
767
768 i1 = i2 = 1;
769 tr2 = 0;
770 if (1 == sscanf(arg, "%u%c", &tr1, &c))
771 goto play_track;
772
773 printf("%s: Invalid command arguments\n", __progname);
774 return (0);
775
776play_track:
777 if (tr1 > n || tr2 > n) {
778 printf("Track number must be between 0 and %u\n", n);
779 return (0);
780 } else if (tr2 == 0)
781 tr2 = h.ending_track;
782
783 if (tr1 > tr2) {
784 printf("starting_track > ending_track\n");
785 return (0);
786 }
787
788 return (play_track(tr1, i1, tr2, i2));
789 }
790
791 /*
792 * Play MSF [tr1] m1:s1[.f1] [tr2] [m2:s2[.f2]]
793 *
794 * Start Time End Time
795 * ---------- --------
796 * tr1 m1:s1.f1 tr2 m2:s2.f2
797 * tr1 m1:s1 tr2 m2:s2.f2
798 * tr1 m1:s1.f1 tr2 m2:s2
799 * tr1 m1:s1 tr2 m2:s2
800 * m1:s1.f1 tr2 m2:s2.f2
801 * m1:s1 tr2 m2:s2.f2
802 * m1:s1.f1 tr2 m2:s2
803 * m1:s1 tr2 m2:s2
804 * tr1 m1:s1.f1 m2:s2.f2
805 * tr1 m1:s1 m2:s2.f2
806 * tr1 m1:s1.f1 m2:s2
807 * tr1 m1:s1 m2:s2
808 * m1:s1.f1 m2:s2.f2
809 * m1:s1 m2:s2.f2
810 * m1:s1.f1 m2:s2
811 * m1:s1 m2:s2
812 * tr1 m1:s1.f1 tr2
813 * tr1 m1:s1 tr2
814 * m1:s1.f1 tr2
815 * m1:s1 tr2
816 * tr1 m1:s1.f1 <end of disc>
817 * tr1 m1:s1 <end of disc>
818 * m1:s1.f1 <end of disc>
819 * m1:s1 <end of disc>
820 */
821
822 /* tr1 m1:s1.f1 tr2 m2:s2.f2 */
823 if (8 == sscanf(arg, "%u%u:%u.%u%u%u:%u.%u%c",
824 &tr1, &m1, &s1, &f1, &tr2, &m2, &s2, &f2, &c))
825 goto play_msf;
826
827 /* tr1 m1:s1 tr2 m2:s2.f2 */
828 f1 = 0;
829 if (7 == sscanf(arg, "%u%u:%u%u%u:%u.%u%c",
830 &tr1, &m1, &s1, &tr2, &m2, &s2, &f2, &c))
831 goto play_msf;
832
833 /* tr1 m1:s1.f1 tr2 m2:s2 */
834 f2 =0;
835 if (7 == sscanf(arg, "%u%u:%u.%u%u%u:%u%c",
836 &tr1, &m1, &s1, &f1, &tr2, &m2, &s2, &c))
837 goto play_msf;
838
839 /* m1:s1.f1 tr2 m2:s2.f2 */
840 tr1 = 0;
841 if (7 == sscanf(arg, "%u:%u.%u%u%u:%u.%u%c",
842 &m1, &s1, &f1, &tr2, &m2, &s2, &f2, &c))
843 goto play_msf;
844
845 /* tr1 m1:s1.f1 m2:s2.f2 */
846 tr2 = 0;
847 if (7 == sscanf(arg, "%u%u:%u.%u%u:%u.%u%c",
848 &tr1, &m1, &s1, &f1, &m2, &s2, &f2, &c))
849 goto play_msf;
850
851 /* m1:s1 tr2 m2:s2.f2 */
852 tr1 = f1 = 0;
853 if (6 == sscanf(arg, "%u:%u%u%u:%u.%u%c",
854 &m1, &s1, &tr2, &m2, &s2, &f2, &c))
855 goto play_msf;
856
857 /* m1:s1.f1 tr2 m2:s2 */
858 tr1 = f2 = 0;
859 if (6 == sscanf(arg, "%u:%u.%u%u%u:%u%c",
860 &m1, &s1, &f1, &tr2, &m2, &s2, &c))
861 goto play_msf;
862
863 /* m1:s1.f1 m2:s2.f2 */
864 tr1 = tr2 = 0;
865 if (6 == sscanf(arg, "%u:%u.%u%u:%u.%u%c",
866 &m1, &s1, &f1, &m2, &s2, &f2, &c))
867 goto play_msf;
868
869 /* tr1 m1:s1.f1 m2:s2 */
870 tr2 = f2 = 0;
871 if (6 == sscanf(arg, "%u%u:%u.%u%u:%u%c",
872 &tr1, &m1, &s1, &f1, &m2, &s2, &c))
873 goto play_msf;
874
875 /* tr1 m1:s1 m2:s2.f2 */
876 tr2 = f1 = 0;
877 if (6 == sscanf(arg, "%u%u:%u%u:%u.%u%c",
878 &tr1, &m1, &s1, &m2, &s2, &f2, &c))
879 goto play_msf;
880
881 /* tr1 m1:s1 tr2 m2:s2 */
882 f1 = f2 = 0;
883 if (6 == sscanf(arg, "%u%u:%u%u%u:%u%c",
884 &tr1, &m1, &s1, &tr2, &m2, &s2, &c))
885 goto play_msf;
886
887 /* m1:s1 tr2 m2:s2 */
888 tr1 = f1 = f2 = 0;
889 if (5 == sscanf(arg, "%u:%u%u%u:%u%c", &m1, &s1, &tr2, &m2, &s2, &c))
890 goto play_msf;
891
892 /* tr1 m1:s1 m2:s2 */
893 f1 = tr2 = f2 = 0;
894 if (5 == sscanf(arg, "%u%u:%u%u:%u%c", &tr1, &m1, &s1, &m2, &s2, &c))
895 goto play_msf;
896
897 /* m1:s1 m2:s2.f2 */
898 tr1 = f1 = tr2 = 0;
899 if (5 == sscanf(arg, "%u:%u%u:%u.%u%c", &m1, &s1, &m2, &s2, &f2, &c))
900 goto play_msf;
901
902 /* m1:s1.f1 m2:s2 */
903 tr1 = tr2 = f2 = 0;
904 if (5 == sscanf(arg, "%u:%u.%u%u:%u%c", &m1, &s1, &f1, &m2, &s2, &c))
905 goto play_msf;
906
907 /* tr1 m1:s1.f1 tr2 */
908 m2 = s2 = f2 = 0;
909 if (5 == sscanf(arg, "%u%u:%u.%u%u%c", &tr1, &m1, &s1, &f1, &tr2, &c))
910 goto play_msf;
911
912 /* m1:s1 m2:s2 */
913 tr1 = f1 = tr2 = f2 = 0;
914 if (4 == sscanf(arg, "%u:%u%u:%u%c", &m1, &s1, &m2, &s2, &c))
915 goto play_msf;
916
917 /* tr1 m1:s1.f1 <end of disc> */
918 tr2 = m2 = s2 = f2 = 0;
919 if (4 == sscanf(arg, "%u%u:%u.%u%c", &tr1, &m1, &s1, &f1, &c))
920 goto play_msf;
921
922 /* tr1 m1:s1 tr2 */
923 f1 = m2 = s2 = f2 = 0;
924 if (4 == sscanf(arg, "%u%u:%u%u%c", &tr1, &m1, &s1, &tr2, &c))
925 goto play_msf;
926
927 /* m1:s1.f1 tr2 */
928 tr1 = m2 = s2 = f2 = 0;
929 if (4 == sscanf(arg, "%u%u:%u%u%c", &m1, &s1, &f1, &tr2, &c))
930 goto play_msf;
931
932 /* m1:s1.f1 <end of disc> */
933 tr1 = tr2 = m2 = s2 = f2 = 0;
934 if (3 == sscanf(arg, "%u:%u.%u%c", &m1, &s1, &f1, &c))
935 goto play_msf;
936
937 /* tr1 m1:s1 <end of disc> */
938 f1 = tr2 = m2 = s2 = f2 = 0;
939 if (3 == sscanf(arg, "%u%u:%u%c", &tr1, &m1, &s1, &c))
940 goto play_msf;
941
942 /* m1:s1 tr2 */
943 tr1 = f1 = m2 = s2 = f2 = 0;
944 if (3 == sscanf(arg, "%u:%u%u%c", &m1, &s1, &tr2, &c))
945 goto play_msf;
946
947 /* m1:s1 <end of disc> */
948 tr1 = f1 = tr2 = m2 = s2 = f2 = 0;
949 if (2 == sscanf(arg, "%u:%u%c", &m1, &s1, &c))
950 goto play_msf;
951
952 printf("%s: Invalid command arguments\n", __progname);
953 return (0);
954
955play_msf:
956 if (tr1 > n || tr2 > n) {
957 printf("Track number must be between 0 and %u\n", n);
958 return (0);
959 } else if (m1 > 99 || m2 > 99) {
960 printf("Minutes must be between 0 and 99\n");
961 return (0);
962 } else if (s1 > 59 || s2 > 59) {
963 printf("Seconds must be between 0 and 59\n");
964 return (0);
965 } else if (f1 > 74 || f2 > 74) {
966 printf("Frames number must be between 0 and 74\n");
967 return (0);
968 }
969
970 if (tr1 > 0) {
971 /*
972 * Start time is relative to tr1, Add start time of tr1
973 * to (m1,s1,f1) to yield absolute start time.
974 */
975 toc2msf(tr1, &tm, &ts, &tf);
976 addmsf(&m1, &s1, &f1, tm, ts, tf);
977
978 /* Compare (m1,s1,f1) to start time of next track. */
979 toc2msf(tr1+1, &tm, &ts, &tf);
980 if (cmpmsf(m1, s1, f1, tm, ts, tf) == 1) {
981 printf("Track %u is not that long.\n", tr1);
982 return (0);
983 }
984 }
985
986 toc2msf(n+1, &tm, &ts, &tf);
987 if (cmpmsf(m1, s1, f1, tm, ts, tf) == 1) {
988 printf("Start time is after end of disc.\n");
989 return (0);
990 }
991
992 if (tr2 > 0) {
993 /*
994 * End time is relative to tr2, Add start time of tr2
995 * to (m2,s2,f2) to yield absolute end time.
996 */
997 toc2msf(tr2, &tm, &ts, &tf);
998 addmsf(&m2, &s2, &f2, tm, ts, tf);
999
1000 /* Compare (m2,s2,f2) to start time of next track. */
1001 toc2msf(tr2+1, &tm, &ts, &tf);
1002 if (cmpmsf(m2, s2, f2, tm, ts, tf) == 1) {
1003 printf("Track %u is not that long.\n", tr2);
1004 return (0);
1005 }
1006 }
1007
1008 toc2msf(n+1, &tm, &ts, &tf);
1009
1010 if (!(tr2 || m2 || s2 || f2)) {
1011 /* Play to end of disc. */
1012 m2 = tm;
1013 s2 = ts;
1014 f2 = tf;
1015 } else if (cmpmsf(m2, s2, f2, tm, ts, tf) == 1) {
1016 printf("End time is after end of disc.\n");
1017 return (0);
1018 }
1019
1020 if (cmpmsf(m1, s1, f1, m2, s2, f2) == 1) {
1021 printf("Start time is after end time.\n");
1022 return (0);
1023 }
1024
1025 return play_msf(m1, s1, f1, m2, s2, f2);
1026}
1027
1028int
1029play_prev(char *arg)
1030{
1031 int trk, min, sec, frm, rc;
1032 struct ioc_toc_header h;
1033
1034 if (status(&trk, &min, &sec, &frm) >= 0) {
1035 trk--;
1036
1037 rc = ioctl(fd, CDIOREADTOCHEADER((unsigned long)0x40000000 | ((sizeof(struct ioc_toc_header) &
0x1fff) << 16) | ((('c')) << 8) | ((4)))
, &h);
1038 if (rc == -1) {
1039 warn("getting toc header");
1040 return (rc);
1041 }
1042
1043 if (trk < h.starting_track)
1044 return play_track(h.starting_track, 1,
1045 h.ending_track + 1, 1);
1046 return play_track(trk, 1, h.ending_track, 1);
1047 }
1048
1049 return (0);
1050}
1051
1052int
1053play_same(char *arg)
1054{
1055 int trk, min, sec, frm, rc;
1056 struct ioc_toc_header h;
1057
1058 if (status (&trk, &min, &sec, &frm) >= 0) {
1059 rc = ioctl(fd, CDIOREADTOCHEADER((unsigned long)0x40000000 | ((sizeof(struct ioc_toc_header) &
0x1fff) << 16) | ((('c')) << 8) | ((4)))
, &h);
1060 if (rc == -1) {
1061 warn("getting toc header");
1062 return (rc);
1063 }
1064
1065 return play_track(trk, 1, h.ending_track, 1);
1066 }
1067
1068 return (0);
1069}
1070
1071int
1072play_next(char *arg)
1073{
1074 int trk, min, sec, frm, rc;
1075 struct ioc_toc_header h;
1076
1077 if (status(&trk, &min, &sec, &frm) >= 0) {
1078 trk++;
1079 rc = ioctl(fd, CDIOREADTOCHEADER((unsigned long)0x40000000 | ((sizeof(struct ioc_toc_header) &
0x1fff) << 16) | ((('c')) << 8) | ((4)))
, &h);
1080 if (rc == -1) {
1081 warn("getting toc header");
1082 return (rc);
1083 }
1084
1085 if (trk > h.ending_track) {
1086 printf("%s: end of CD\n", __progname);
1087
1088 rc = ioctl(fd, CDIOCSTOP((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((23)))
);
1089
1090 (void) ioctl(fd, CDIOCALLOW((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('c')) << 8) | ((25)))
);
1091
1092 return (rc);
1093 }
1094
1095 return play_track(trk, 1, h.ending_track, 1);
1096 }
1097
1098 return (0);
1099}
1100
1101char *
1102strstatus(int sts)
1103{
1104 switch (sts) {
1105 case ASTS_INVALID0x00:
1106 return ("invalid");
1107 case ASTS_PLAYING0x11:
1108 return ("playing");
1109 case ASTS_PAUSED0x12:
1110 return ("paused");
1111 case ASTS_COMPLETED0x13:
1112 return ("completed");
1113 case ASTS_ERROR0x14:
1114 return ("error");
1115 case ASTS_VOID0x15:
1116 return ("void");
1117 default:
1118 return ("??");
1119 }
1120}
1121
1122int
1123pstatus(char *arg)
1124{
1125 struct ioc_vol v;
1126 struct ioc_read_subchannel ss;
1127 struct cd_sub_channel_info data;
1128 int rc, trk, m, s, f;
1129 char vis_catalog[1 + 4 * 15];
1130
1131 rc = status(&trk, &m, &s, &f);
1132 if (rc >= 0) {
1133 if (verbose) {
1134 if (track_names)
1135 printf("Audio status = %d<%s>, "
1136 "current track = %d (%s)\n"
1137 "\tcurrent position = %d:%02d.%02d\n",
1138 rc, strstatus(rc), trk,
1139 trk ? track_names[trk-1] : "", m, s, f);
1140 else
1141 printf("Audio status = %d<%s>, "
1142 "current track = %d, "
1143 "current position = %d:%02d.%02d\n",
1144 rc, strstatus(rc), trk, m, s, f);
1145 } else
1146 printf("%d %d %d:%02d.%02d\n", rc, trk, m, s, f);
1147 } else
1148 printf("No current status info available\n");
1149
1150 bzero(&ss, sizeof (ss));
1151 ss.data = &data;
1152 ss.data_len = sizeof (data);
1153 ss.address_format = msf ? CD_MSF_FORMAT2 : CD_LBA_FORMAT1;
1154 ss.data_format = CD_MEDIA_CATALOG2;
1155 rc = ioctl(fd, CDIOCREADSUBCHANNEL(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct ioc_read_subchannel) & 0x1fff) << 16) | (((
'c')) << 8) | ((3)))
, (char *) &ss);
1156 if (rc >= 0) {
1157 printf("Media catalog is %sactive",
1158 ss.data->what.media_catalog.mc_valid ? "": "in");
1159 if (ss.data->what.media_catalog.mc_valid &&
1160 ss.data->what.media_catalog.mc_number[0]) {
1161 strvisx(vis_catalog,
1162 (char *)ss.data->what.media_catalog.mc_number,
1163 15, VIS_SAFE0x20);
1164 printf(", number \"%.15s\"", vis_catalog);
1165 }
1166 putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n',
(&__sF[1])))
;
1167 } else
1168 printf("No media catalog info available\n");
1169
1170 rc = ioctl(fd, CDIOCGETVOL((unsigned long)0x40000000 | ((sizeof(struct ioc_vol) & 0x1fff
) << 16) | ((('c')) << 8) | ((10)))
, &v);
1171 if (rc >= 0) {
1172 if (verbose)
1173 printf("Left volume = %d, right volume = %d\n",
1174 v.vol[0], v.vol[1]);
1175 else
1176 printf("%d %d\n", v.vol[0], v.vol[1]);
1177 } else
1178 printf("No volume level info available\n");
1179 return(0);
1180}
1181
1182int
1183cdid(void)
1184{
1185 unsigned long id;
1186 struct ioc_toc_header h;
1187 int rc, n;
1188
1189 rc = ioctl(fd, CDIOREADTOCHEADER((unsigned long)0x40000000 | ((sizeof(struct ioc_toc_header) &
0x1fff) << 16) | ((('c')) << 8) | ((4)))
, &h);
1190 if (rc == -1) {
1191 warn("getting toc header");
1192 return (rc);
1193 }
1194
1195 n = h.ending_track - h.starting_track + 1;
1196 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
1197 if (rc < 0)
1198 return (rc);
1199
1200 id = cddb_discid(n, toc_buffer);
1201 if (id) {
1202 if (verbose)
1203 printf("CDID=");
1204 printf("%08lx\n", id);
1205 }
1206 return id ? 0 : 1;
1207}
1208
1209int
1210info(char *arg)
1211{
1212 struct ioc_toc_header h;
1213 int rc, i, n;
1214
1215 if (get_media_capabilities(mediacap, 1) == -1)
1216 errx(1, "Can't determine media type");
1217
1218 rc = ioctl(fd, CDIOREADTOCHEADER((unsigned long)0x40000000 | ((sizeof(struct ioc_toc_header) &
0x1fff) << 16) | ((('c')) << 8) | ((4)))
, &h);
1219 if (rc >= 0) {
1220 if (verbose)
1221 printf("Starting track = %d, ending track = %d, TOC size = %d bytes\n",
1222 h.starting_track, h.ending_track, h.len);
1223 else
1224 printf("%d %d %d\n", h.starting_track,
1225 h.ending_track, h.len);
1226 } else {
1227 warn("getting toc header");
1228 return (rc);
1229 }
1230
1231 n = h.ending_track - h.starting_track + 1;
1232 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
1233 if (rc < 0)
1234 return (rc);
1235
1236 if (verbose) {
1237 printf("track start duration block length type\n");
1238 printf("-------------------------------------------------\n");
1239 }
1240
1241 for (i = 0; i < n; i++) {
1242 printf("%5d ", toc_buffer[i].track);
1243 prtrack(toc_buffer + i, 0, NULL((void *)0));
1244 }
1245 printf("%5d ", toc_buffer[n].track);
1246 prtrack(toc_buffer + n, 1, NULL((void *)0));
1247 return (0);
1248}
1249
1250int
1251cddbinfo(char *arg)
1252{
1253 struct ioc_toc_header h;
1254 int rc, i, n;
1255
1256 rc = ioctl(fd, CDIOREADTOCHEADER((unsigned long)0x40000000 | ((sizeof(struct ioc_toc_header) &
0x1fff) << 16) | ((('c')) << 8) | ((4)))
, &h);
1257 if (rc == -1) {
1258 warn("getting toc header");
1259 return (rc);
1260 }
1261
1262 n = h.ending_track - h.starting_track + 1;
1263 rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
1264 if (rc < 0)
1265 return (rc);
1266
1267 if (track_names)
1268 free_names(track_names);
1269 track_names = NULL((void *)0);
1270
1271 track_names = cddb(cddb_host, n, toc_buffer, arg);
1272 if (!track_names)
1273 return(0);
1274
1275 printf("-------------------------------------------------\n");
1276
1277 for (i = 0; i < n; i++) {
1278 printf("%5d ", toc_buffer[i].track);
1279 prtrack(toc_buffer + i, 0, track_names[i]);
1280 }
1281 printf("%5d ", toc_buffer[n].track);
1282 prtrack(toc_buffer + n, 1, "");
1283 return (0);
1284}
1285
1286void
1287lba2msf(unsigned long lba, u_char *m, u_char *s, u_char *f)
1288{
1289 lba += 150; /* block start offset */
1290 lba &= 0xffffff; /* negative lbas use only 24 bits */
1291 *m = lba / (60 * 75);
1292 lba %= (60 * 75);
1293 *s = lba / 75;
1294 *f = lba % 75;
1295}
1296
1297unsigned int
1298msf2lba(u_char m, u_char s, u_char f)
1299{
1300 return (((m * 60) + s) * 75 + f) - 150;
1301}
1302
1303unsigned long
1304entry2time(struct cd_toc_entry *e)
1305{
1306 int block;
1307 u_char m, s, f;
1308
1309 if (msf) {
1310 return (e->addr.msf.minute * 60 + e->addr.msf.second);
1311 } else {
1312 block = e->addr.lba;
1313 lba2msf(block, &m, &s, &f);
1314 return (m*60+s);
1315 }
1316}
1317
1318unsigned long
1319entry2frames(struct cd_toc_entry *e)
1320{
1321 int block;
1322 unsigned char m, s, f;
1323
1324 if (msf) {
1325 return e->addr.msf.frame + e->addr.msf.second * 75 +
1326 e->addr.msf.minute * 60 * 75;
1327 } else {
1328 block = e->addr.lba;
1329 lba2msf(block, &m, &s, &f);
1330 return f + s * 75 + m * 60 * 75;
1331 }
1332}
1333
1334void
1335prtrack(struct cd_toc_entry *e, int lastflag, char *name)
1336{
1337 int block, next, len;
1338 u_char m, s, f;
1339
1340 if (msf) {
1341 if (!name || lastflag)
1342 /* Print track start */
1343 printf("%2d:%02d.%02d ", e->addr.msf.minute,
1344 e->addr.msf.second, e->addr.msf.frame);
1345
1346 block = msf2lba(e->addr.msf.minute, e->addr.msf.second,
1347 e->addr.msf.frame);
1348 } else {
1349 block = e->addr.lba;
1350 if (!name || lastflag) {
1351 lba2msf(block, &m, &s, &f);
1352 /* Print track start */
1353 printf("%2d:%02d.%02d ", m, s, f);
1354 }
1355 }
1356 if (lastflag) {
1357 if (!name)
1358 /* Last track -- print block */
1359 printf(" - %6d - -\n", block);
1360 else
1361 printf("\n");
1362 return;
1363 }
1364
1365 if (msf)
1366 next = msf2lba(e[1].addr.msf.minute, e[1].addr.msf.second,
1367 e[1].addr.msf.frame);
1368 else
1369 next = e[1].addr.lba;
1370 len = next - block;
1371 lba2msf(len - 150, &m, &s, &f);
1372
1373 if (name)
1374 printf("%2d:%02d.%02d %s\n", m, s, f, name);
1375 /* Print duration, block, length, type */
1376 else
1377 printf("%2d:%02d.%02d %6d %6d %5s\n", m, s, f, block, len,
1378 (e->control & 4) ? "data" : "audio");
1379}
1380
1381int
1382play_track(int tstart, int istart, int tend, int iend)
1383{
1384 struct ioc_play_track t;
1385
1386 t.start_track = tstart;
1387 t.start_index = istart;
1388 t.end_track = tend;
1389 t.end_index = iend;
1390
1391 return ioctl(fd, CDIOCPLAYTRACKS((unsigned long)0x80000000 | ((sizeof(struct ioc_play_track) &
0x1fff) << 16) | ((('c')) << 8) | ((1)))
, &t);
1392}
1393
1394int
1395play_blocks(int blk, int len)
1396{
1397 struct ioc_play_blocks t;
1398
1399 t.blk = blk;
1400 t.len = len;
1401
1402 return ioctl(fd, CDIOCPLAYBLOCKS((unsigned long)0x80000000 | ((sizeof(struct ioc_play_blocks)
& 0x1fff) << 16) | ((('c')) << 8) | ((2)))
, &t);
1403}
1404
1405int
1406setvol(int left, int right)
1407{
1408 struct ioc_vol v;
1409
1410 v.vol[0] = left;
1411 v.vol[1] = right;
1412 v.vol[2] = 0;
1413 v.vol[3] = 0;
1414
1415 return ioctl(fd, CDIOCSETVOL((unsigned long)0x80000000 | ((sizeof(struct ioc_vol) & 0x1fff
) << 16) | ((('c')) << 8) | ((11)))
, &v);
1416}
1417
1418int
1419read_toc_entrys(int len)
1420{
1421 struct ioc_read_toc_entry t;
1422
1423 if (toc_buffer) {
1424 free(toc_buffer);
1425 toc_buffer = 0;
1426 }
1427
1428 toc_buffer = malloc(len);
1429
1430 if (!toc_buffer) {
1431 errno(*__errno()) = ENOMEM12;
1432 return (-1);
1433 }
1434
1435 t.address_format = msf ? CD_MSF_FORMAT2 : CD_LBA_FORMAT1;
1436 t.starting_track = 0;
1437 t.data_len = len;
1438 t.data = toc_buffer;
1439
1440 return (ioctl(fd, CDIOREADTOCENTRYS(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct ioc_read_toc_entry) & 0x1fff) << 16) | ((('c'
)) << 8) | ((5)))
, (char *) &t));
1441}
1442
1443int
1444play_msf(int start_m, int start_s, int start_f, int end_m, int end_s, int end_f)
1445{
1446 struct ioc_play_msf a;
1447
1448 a.start_m = start_m;
1449 a.start_s = start_s;
1450 a.start_f = start_f;
1451 a.end_m = end_m;
1452 a.end_s = end_s;
1453 a.end_f = end_f;
1454
1455 return ioctl(fd, CDIOCPLAYMSF((unsigned long)0x80000000 | ((sizeof(struct ioc_play_msf) &
0x1fff) << 16) | ((('c')) << 8) | ((25)))
, (char *) &a);
1456}
1457
1458int
1459status(int *trk, int *min, int *sec, int *frame)
1460{
1461 struct ioc_read_subchannel s;
1462 struct cd_sub_channel_info data;
1463 u_char mm, ss, ff;
1464
1465 bzero(&s, sizeof (s));
1466 s.data = &data;
1467 s.data_len = sizeof (data);
1468 s.address_format = msf ? CD_MSF_FORMAT2 : CD_LBA_FORMAT1;
1469 s.data_format = CD_CURRENT_POSITION1;
1470
1471 if (ioctl(fd, CDIOCREADSUBCHANNEL(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct ioc_read_subchannel) & 0x1fff) << 16) | (((
'c')) << 8) | ((3)))
, (char *) &s) == -1)
1472 return -1;
1473
1474 *trk = s.data->what.position.track_number;
1475 if (msf) {
1476 *min = s.data->what.position.reladdr.msf.minute;
1477 *sec = s.data->what.position.reladdr.msf.second;
1478 *frame = s.data->what.position.reladdr.msf.frame;
1479 } else {
1480 /*
1481 * NOTE: CDIOCREADSUBCHANNEL does not put the lba info into
1482 * host order like CDIOREADTOCENTRYS does.
1483 */
1484 lba2msf(betoh32(s.data->what.position.reladdr.lba)(__uint32_t)(__builtin_constant_p(s.data->what.position.reladdr
.lba) ? (__uint32_t)(((__uint32_t)(s.data->what.position.reladdr
.lba) & 0xff) << 24 | ((__uint32_t)(s.data->what
.position.reladdr.lba) & 0xff00) << 8 | ((__uint32_t
)(s.data->what.position.reladdr.lba) & 0xff0000) >>
8 | ((__uint32_t)(s.data->what.position.reladdr.lba) &
0xff000000) >> 24) : __swap32md(s.data->what.position
.reladdr.lba))
, &mm, &ss,
1485 &ff);
1486 *min = mm;
1487 *sec = ss;
1488 *frame = ff;
1489 }
1490
1491 return s.data->header.audio_status;
1492}
1493
1494void
1495sigint_handler(int signo_arg)
1496{
1497 signo = signo_arg;
1498}
1499
1500char *
1501input(int *cmd)
1502{
1503 struct sigaction sa;
1504 char *buf;
1505 int siz = 0;
1506 char *p;
1507 HistEvent hev;
1508
1509 memset(&sa, 0, sizeof(sa));
1510 do {
1511 signo = 0;
1512 sa.sa_handler__sigaction_u.__sa_handler = sigint_handler;
1513 if (sigaction(SIGINT2, &sa, NULL((void *)0)) == -1)
1514 err(1, "sigaction");
1515 buf = (char *)el_gets(el, &siz);
1516 sa.sa_handler__sigaction_u.__sa_handler = SIG_DFL(void (*)(int))0;
1517 if (sigaction(SIGINT2, &sa, NULL((void *)0)) == -1)
1518 err(1, "sigaction");
1519 if (buf == NULL((void *)0) || siz <= 0) {
1520 fprintf(stderr(&__sF[2]), "\r\n");
1521 if (siz < 0 && errno(*__errno()) == EINTR4 && signo == SIGINT2)
1522 continue;
1523 *cmd = CMD_QUIT8;
1524 return (0);
1525 }
1526 if (strlen(buf) > 1)
1527 history(hist, &hev, H_ENTER10, buf);
1528 p = parse(buf, cmd);
1529 } while (!p);
1530 return (p);
1531}
1532
1533char *
1534parse(char *buf, int *cmd)
1535{
1536 struct cmdtab *c;
1537 char *p;
1538 size_t len;
1539
1540 for (p=buf; isspace((unsigned char)*p); p++)
24
Assuming the character is not a whitespace character
1541 continue;
1542
1543 if (isdigit((unsigned char)*p) ||
25
Assuming the character is not a digit
1544 (p[0] == '#' && isdigit((unsigned char)p[1]))) {
26
Assuming the condition is false
1545 *cmd = CMD_PLAY7;
1546 return (p);
1547 }
1548
1549 for (buf = p; *p && ! isspace((unsigned char)*p); p++)
27
Assuming the condition is false
1550 continue;
1551
1552 len = p - buf;
1553 if (!len
27.1
'len' is 0
)
28
Taking true branch
1554 return (0);
29
Returning without writing to '*cmd'
1555
1556 if (*p) { /* It must be a spacing character! */
1557 char *q;
1558
1559 *p++ = 0;
1560 for (q=p; *q && *q != '\n' && *q != '\r'; q++)
1561 continue;
1562 *q = 0;
1563 }
1564
1565 *cmd = -1;
1566 for (c=cmdtab; c->name; ++c) {
1567 /* Is it an exact match? */
1568 if (!strcasecmp(buf, c->name)) {
1569 *cmd = c->command;
1570 break;
1571 }
1572
1573 /* Try short hand forms then... */
1574 if (len >= c->min && ! strncasecmp(buf, c->name, len)) {
1575 if (*cmd != -1 && *cmd != c->command) {
1576 fprintf(stderr(&__sF[2]), "Ambiguous command\n");
1577 return (0);
1578 }
1579 *cmd = c->command;
1580 }
1581 }
1582
1583 if (*cmd == -1) {
1584 fprintf(stderr(&__sF[2]), "%s: Invalid command, enter ``help'' for commands.\n",
1585 __progname);
1586 return (0);
1587 }
1588
1589 while (isspace((unsigned char)*p))
1590 p++;
1591 return p;
1592}
1593
1594int
1595open_cd(char *dev, int needwrite)
1596{
1597 char *realdev;
1598 int tries;
1599
1600 if (fd > -1) {
1601 if (needwrite && !writeperm) {
1602 close(fd);
1603 fd = -1;
1604 } else
1605 return (1);
1606 }
1607
1608 for (tries = 0; fd < 0 && tries < 10; tries++) {
1609 if (needwrite)
1610 fd = opendev(dev, O_RDWR0x0002, OPENDEV_PART0x01, &realdev);
1611 else
1612 fd = opendev(dev, O_RDONLY0x0000, OPENDEV_PART0x01, &realdev);
1613 if (fd == -1) {
1614 if (errno(*__errno()) == ENXIO6) {
1615 /* ENXIO has an overloaded meaning here.
1616 * The original "Device not configured" should
1617 * be interpreted as "No disc in drive %s". */
1618 warnx("No disc in drive %s.", realdev);
1619 return (0);
1620 } else if (errno(*__errno()) != EIO5) {
1621 /* EIO may simply mean the device is not ready
1622 * yet which is common with CD changers. */
1623 warn("Can't open %s", realdev);
1624 return (0);
1625 }
1626 }
1627 sleep(1);
1628 }
1629 if (fd == -1) {
1630 warn("Can't open %s", realdev);
1631 return (0);
1632 }
1633 writeperm = needwrite;
1634 return (1);
1635}
1636
1637char *
1638prompt(void)
1639{
1640 return (verbose ? "cdio> " : "");
1641}
1642
1643void
1644switch_el(void)
1645{
1646 HistEvent hev;
1647
1648 if (el == NULL((void *)0) && hist == NULL((void *)0)) {
1649 el = el_init(__progname, stdin(&__sF[0]), stdout(&__sF[1]), stderr(&__sF[2]));
1650 hist = history_init();
1651 history(hist, &hev, H_SETSIZE1, 100);
1652 el_set(el, EL_HIST10, history, hist);
1653 el_set(el, EL_EDITOR2, "emacs");
1654 el_set(el, EL_PROMPT0, prompt);
1655 el_set(el, EL_SIGNAL3, 1);
1656 el_source(el, NULL((void *)0));
1657
1658 } else {
1659 if (hist != NULL((void *)0)) {
1660 history_end(hist);
1661 hist = NULL((void *)0);
1662 }
1663 if (el != NULL((void *)0)) {
1664 el_end(el);
1665 el = NULL((void *)0);
1666 }
1667 }
1668}
1669
1670void
1671addmsf(u_int *m, u_int *s, u_int *f, u_char m_inc, u_char s_inc, u_char f_inc)
1672{
1673 *f += f_inc;
1674 if (*f > 75) {
1675 *s += *f / 75;
1676 *f %= 75;
1677 }
1678
1679 *s += s_inc;
1680 if (*s > 60) {
1681 *m += *s / 60;
1682 *s %= 60;
1683 }
1684
1685 *m += m_inc;
1686}
1687
1688int
1689cmpmsf(u_char m1, u_char s1, u_char f1, u_char m2, u_char s2, u_char f2)
1690{
1691 if (m1 > m2)
1692 return (1);
1693 else if (m1 < m2)
1694 return (-1);
1695
1696 if (s1 > s2)
1697 return (1);
1698 else if (s1 < s2)
1699 return (-1);
1700
1701 if (f1 > f2)
1702 return (1);
1703 else if (f1 < f2)
1704 return (-1);
1705
1706 return (0);
1707}
1708
1709void
1710toc2msf(u_int track, u_char *m, u_char *s, u_char *f)
1711{
1712 struct cd_toc_entry *ctep;
1713
1714 ctep = &toc_buffer[track - 1];
1715
1716 if (msf) {
1717 *m = ctep->addr.msf.minute;
1718 *s = ctep->addr.msf.second;
1719 *f = ctep->addr.msf.frame;
1720 } else
1721 lba2msf(ctep->addr.lba, m, s, f);
1722}