1 /* zdiscid.c -- Print the CDDB disc ID for an audio CD */
4 * Copyright (C) 2006 - 2011 Steve Youngs
8 * Author: Steve Youngs <steve@steveyoungs.com>
9 * Maintainer: Steve Youngs <steve@steveyoungs.com>
10 * Created: <2006-08-13>
14 * This file is part of zcdrip.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
27 * 3. Neither the name of the author nor the names of any contributors
28 * may be used to endorse or promote products derived from this
29 * software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
38 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
40 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
41 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 * Print the CDDB disc ID for an audio CD.
54 #include <sys/ioctl.h>
55 #include <linux/cdrom.h>
57 #define FAILURE_NOCD -1
65 unsigned get_cddb_id(int);
70 int drive = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
71 struct cdrom_tochdr tochdr;
72 struct cdrom_tocentry tocentry;
75 if (ioctl(drive, CDROMREADTOCHDR, &tochdr) != 0) {
76 fprintf(stderr, "No disc in drive\n");
81 for (i = tochdr.cdth_trk0; i <= tochdr.cdth_trk1; i++) {
82 tocentry.cdte_track = i;
83 tocentry.cdte_format = CDROM_MSF;
84 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
85 cdtoc[i-1].min = tocentry.cdte_addr.msf.minute;
86 cdtoc[i-1].sec = tocentry.cdte_addr.msf.second;
87 cdtoc[i-1].frame = tocentry.cdte_addr.msf.frame;
88 cdtoc[i-1].frame += cdtoc[i-1].min * 60 * 75;
89 cdtoc[i-1].frame += cdtoc[i-1].sec * 75;
91 tocentry.cdte_track = 0xAA;
92 tocentry.cdte_format = CDROM_MSF;
93 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
94 cdtoc[tochdr.cdth_trk1].min = tocentry.cdte_addr.msf.minute;
95 cdtoc[tochdr.cdth_trk1].sec = tocentry.cdte_addr.msf.second;
96 cdtoc[tochdr.cdth_trk1].frame = tocentry.cdte_addr.msf.frame;
97 cdtoc[tochdr.cdth_trk1].frame += cdtoc[tochdr.cdth_trk1].min * 60 * 75;
98 cdtoc[tochdr.cdth_trk1].frame += cdtoc[tochdr.cdth_trk1].sec * 75;
100 return tochdr.cdth_trk1;
109 ret = ret + (n % 10);
117 get_cddb_id(int ntrks)
124 n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
128 t = ((cdtoc[ntrks].min * 60) + cdtoc[ntrks].sec) -
129 ((cdtoc[0].min * 60) + cdtoc[0].sec);
131 return ((n % 0xff) << 24 | t << 8 | ntrks);
135 main(int argc, char **argv)
144 fprintf(stderr, "Disc is not an audio CD\n");
152 printf("%08x %d", get_cddb_id(ntrks), ntrks);
153 for (i = 0; i < ntrks; i++) {
154 printf(" %d", cdtoc[i].frame);
156 printf(" %d\n", (cdtoc[ntrks].frame) / 75);
160 /* zdiscid.c ends here */