annotate cddb-id.c @ 6:f5daa3cdeb58

- Clear up old variable usage. - Grok Artist/Title in TTITLE fields. - Add the ability to append to track title fields.
author darius
date Sat, 12 Aug 2000 07:12:12 +0000
parents 74031379d3cb
children ad83a38c3f5a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
74031379d3cb Initial revision
darius
parents:
diff changeset
1 #include <stdio.h>
74031379d3cb Initial revision
darius
parents:
diff changeset
2 #include <stdlib.h>
74031379d3cb Initial revision
darius
parents:
diff changeset
3 #include <unistd.h>
74031379d3cb Initial revision
darius
parents:
diff changeset
4 #include <sysexits.h>
74031379d3cb Initial revision
darius
parents:
diff changeset
5 #include <fcntl.h>
74031379d3cb Initial revision
darius
parents:
diff changeset
6 #include <sys/errno.h>
74031379d3cb Initial revision
darius
parents:
diff changeset
7 #include <sys/ioctl.h>
74031379d3cb Initial revision
darius
parents:
diff changeset
8 #include <sys/cdio.h>
74031379d3cb Initial revision
darius
parents:
diff changeset
9
74031379d3cb Initial revision
darius
parents:
diff changeset
10 int cddb_sum(int n);
74031379d3cb Initial revision
darius
parents:
diff changeset
11 unsigned long cddb_discid(int tot_trks, struct cd_toc_entry *cdtoc);
74031379d3cb Initial revision
darius
parents:
diff changeset
12 void usage(void);
74031379d3cb Initial revision
darius
parents:
diff changeset
13
74031379d3cb Initial revision
darius
parents:
diff changeset
14 int
74031379d3cb Initial revision
darius
parents:
diff changeset
15 main(int argc, char **argv)
74031379d3cb Initial revision
darius
parents:
diff changeset
16 {
74031379d3cb Initial revision
darius
parents:
diff changeset
17 int cd_fd, tot_tracks, i, machine, disc_length, tracknums;
74031379d3cb Initial revision
darius
parents:
diff changeset
18 unsigned long disc_id;
74031379d3cb Initial revision
darius
parents:
diff changeset
19 struct ioc_toc_header toc_head;
74031379d3cb Initial revision
darius
parents:
diff changeset
20 struct ioc_read_toc_entry toc_entries_head;
74031379d3cb Initial revision
darius
parents:
diff changeset
21 struct cd_toc_entry *toc_entries;
74031379d3cb Initial revision
darius
parents:
diff changeset
22 char cd_path[256], ch;
74031379d3cb Initial revision
darius
parents:
diff changeset
23
74031379d3cb Initial revision
darius
parents:
diff changeset
24 /* Default CD path */
74031379d3cb Initial revision
darius
parents:
diff changeset
25 strcpy(cd_path, "/dev/cd0c");
74031379d3cb Initial revision
darius
parents:
diff changeset
26
74031379d3cb Initial revision
darius
parents:
diff changeset
27 /* Default to human readable */
74031379d3cb Initial revision
darius
parents:
diff changeset
28 machine = 0;
74031379d3cb Initial revision
darius
parents:
diff changeset
29
74031379d3cb Initial revision
darius
parents:
diff changeset
30 /* Default to no track number stuff */
74031379d3cb Initial revision
darius
parents:
diff changeset
31 tracknums = 0;
74031379d3cb Initial revision
darius
parents:
diff changeset
32
74031379d3cb Initial revision
darius
parents:
diff changeset
33 while ((ch = getopt(argc, argv, "f:mhn")) != -1) {
74031379d3cb Initial revision
darius
parents:
diff changeset
34 switch(ch) {
74031379d3cb Initial revision
darius
parents:
diff changeset
35 case 'f':
74031379d3cb Initial revision
darius
parents:
diff changeset
36 if (strlen(optarg) > 255) {
74031379d3cb Initial revision
darius
parents:
diff changeset
37 warnx("Filename too long");
74031379d3cb Initial revision
darius
parents:
diff changeset
38 exit(EX_USAGE);
74031379d3cb Initial revision
darius
parents:
diff changeset
39 }
74031379d3cb Initial revision
darius
parents:
diff changeset
40
74031379d3cb Initial revision
darius
parents:
diff changeset
41 strcpy(cd_path, optarg);
74031379d3cb Initial revision
darius
parents:
diff changeset
42 break;
74031379d3cb Initial revision
darius
parents:
diff changeset
43
74031379d3cb Initial revision
darius
parents:
diff changeset
44 case 'm':
74031379d3cb Initial revision
darius
parents:
diff changeset
45 machine = 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
46 break;
74031379d3cb Initial revision
darius
parents:
diff changeset
47
74031379d3cb Initial revision
darius
parents:
diff changeset
48 case 'n':
74031379d3cb Initial revision
darius
parents:
diff changeset
49 tracknums = 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
50 break;
74031379d3cb Initial revision
darius
parents:
diff changeset
51
74031379d3cb Initial revision
darius
parents:
diff changeset
52 case '?':
74031379d3cb Initial revision
darius
parents:
diff changeset
53 case 'h':
74031379d3cb Initial revision
darius
parents:
diff changeset
54 default:
74031379d3cb Initial revision
darius
parents:
diff changeset
55 usage();
74031379d3cb Initial revision
darius
parents:
diff changeset
56 break;
74031379d3cb Initial revision
darius
parents:
diff changeset
57 }
74031379d3cb Initial revision
darius
parents:
diff changeset
58 }
74031379d3cb Initial revision
darius
parents:
diff changeset
59
74031379d3cb Initial revision
darius
parents:
diff changeset
60 if ((cd_fd = open(cd_path, O_RDWR)) == -1) {
74031379d3cb Initial revision
darius
parents:
diff changeset
61 warnx("Failed to open %s, reason: %s", cd_path, strerror(errno));
74031379d3cb Initial revision
darius
parents:
diff changeset
62 exit(EX_IOERR);
74031379d3cb Initial revision
darius
parents:
diff changeset
63 }
74031379d3cb Initial revision
darius
parents:
diff changeset
64
74031379d3cb Initial revision
darius
parents:
diff changeset
65 if (ioctl(cd_fd, CDIOREADTOCHEADER, &toc_head) == -1) {
74031379d3cb Initial revision
darius
parents:
diff changeset
66 warnx("Failed to get TOC header, reason: %s", strerror(errno));
74031379d3cb Initial revision
darius
parents:
diff changeset
67 exit(EX_UNAVAILABLE);
74031379d3cb Initial revision
darius
parents:
diff changeset
68 }
74031379d3cb Initial revision
darius
parents:
diff changeset
69
74031379d3cb Initial revision
darius
parents:
diff changeset
70 tot_tracks = toc_head.ending_track - toc_head.starting_track + 1;
74031379d3cb Initial revision
darius
parents:
diff changeset
71
74031379d3cb Initial revision
darius
parents:
diff changeset
72 toc_entries = (struct cd_toc_entry *)malloc(sizeof(struct cd_toc_entry) * (tot_tracks + 1));
74031379d3cb Initial revision
darius
parents:
diff changeset
73 if (toc_entries == NULL) {
74031379d3cb Initial revision
darius
parents:
diff changeset
74 warnx("Couldn't allocate memeory for TOC entries");
74031379d3cb Initial revision
darius
parents:
diff changeset
75 exit(EX_UNAVAILABLE);
74031379d3cb Initial revision
darius
parents:
diff changeset
76 }
74031379d3cb Initial revision
darius
parents:
diff changeset
77
74031379d3cb Initial revision
darius
parents:
diff changeset
78 toc_entries_head.data = toc_entries;
74031379d3cb Initial revision
darius
parents:
diff changeset
79 toc_entries_head.data_len = sizeof(struct cd_toc_entry) * (tot_tracks + 1);
74031379d3cb Initial revision
darius
parents:
diff changeset
80 toc_entries_head.starting_track = 0;
74031379d3cb Initial revision
darius
parents:
diff changeset
81 toc_entries_head.address_format = CD_MSF_FORMAT;
74031379d3cb Initial revision
darius
parents:
diff changeset
82
74031379d3cb Initial revision
darius
parents:
diff changeset
83 if (ioctl(cd_fd, CDIOREADTOCENTRYS, &toc_entries_head) == -1) {
74031379d3cb Initial revision
darius
parents:
diff changeset
84 warnx("Failed to get TOC entries, reason: %s\n", strerror(errno));
74031379d3cb Initial revision
darius
parents:
diff changeset
85 exit(EX_UNAVAILABLE);
74031379d3cb Initial revision
darius
parents:
diff changeset
86 }
74031379d3cb Initial revision
darius
parents:
diff changeset
87
74031379d3cb Initial revision
darius
parents:
diff changeset
88 if (tracknums) {
74031379d3cb Initial revision
darius
parents:
diff changeset
89 for (i = 1; i <= tot_tracks; i++)
74031379d3cb Initial revision
darius
parents:
diff changeset
90 if (!(toc_entries[i].control & 4))
74031379d3cb Initial revision
darius
parents:
diff changeset
91 printf("%d ", i);
74031379d3cb Initial revision
darius
parents:
diff changeset
92
74031379d3cb Initial revision
darius
parents:
diff changeset
93 printf("\n");
74031379d3cb Initial revision
darius
parents:
diff changeset
94
74031379d3cb Initial revision
darius
parents:
diff changeset
95 exit(0);
74031379d3cb Initial revision
darius
parents:
diff changeset
96 }
74031379d3cb Initial revision
darius
parents:
diff changeset
97
74031379d3cb Initial revision
darius
parents:
diff changeset
98 disc_id = cddb_discid(tot_tracks, toc_entries);
74031379d3cb Initial revision
darius
parents:
diff changeset
99 disc_length = (toc_entries[tot_tracks].addr.msf.minute * 60) +
74031379d3cb Initial revision
darius
parents:
diff changeset
100 (toc_entries[tot_tracks].addr.msf.second);
74031379d3cb Initial revision
darius
parents:
diff changeset
101
74031379d3cb Initial revision
darius
parents:
diff changeset
102 if (machine == 0) {
74031379d3cb Initial revision
darius
parents:
diff changeset
103 printf("Start track = %d, end track = %d, length = %d\n", toc_head.starting_track,
74031379d3cb Initial revision
darius
parents:
diff changeset
104 toc_head.ending_track, toc_head.len);
74031379d3cb Initial revision
darius
parents:
diff changeset
105 printf("Disc ID is %08x\n", disc_id);
74031379d3cb Initial revision
darius
parents:
diff changeset
106 printf("Length is %d seconds\n", disc_length);
74031379d3cb Initial revision
darius
parents:
diff changeset
107 } else {
74031379d3cb Initial revision
darius
parents:
diff changeset
108 printf("%08x", disc_id);
74031379d3cb Initial revision
darius
parents:
diff changeset
109 }
74031379d3cb Initial revision
darius
parents:
diff changeset
110
74031379d3cb Initial revision
darius
parents:
diff changeset
111 for (i = 0; i < tot_tracks; i++) {
74031379d3cb Initial revision
darius
parents:
diff changeset
112 if (machine == 0) {
74031379d3cb Initial revision
darius
parents:
diff changeset
113 printf("Track %d, Minute = %d, Second = %d, Frame = %d, Type = %s, Offset = %d\n",
74031379d3cb Initial revision
darius
parents:
diff changeset
114 toc_entries[i].track, toc_entries[i].addr.msf.minute,
74031379d3cb Initial revision
darius
parents:
diff changeset
115 toc_entries[i].addr.msf.second, toc_entries[i].addr.msf.frame,
74031379d3cb Initial revision
darius
parents:
diff changeset
116 (toc_entries[i].control & 4) ? "data" : "audio",
74031379d3cb Initial revision
darius
parents:
diff changeset
117 (toc_entries[i].addr.msf.minute * 60 * 75) +
74031379d3cb Initial revision
darius
parents:
diff changeset
118 (toc_entries[i].addr.msf.second * 75) +
74031379d3cb Initial revision
darius
parents:
diff changeset
119 toc_entries[i].addr.msf.frame);
74031379d3cb Initial revision
darius
parents:
diff changeset
120 } else {
74031379d3cb Initial revision
darius
parents:
diff changeset
121 printf(" %d", (toc_entries[i].addr.msf.minute * 60 * 75) +
74031379d3cb Initial revision
darius
parents:
diff changeset
122 (toc_entries[i].addr.msf.second * 75) +
74031379d3cb Initial revision
darius
parents:
diff changeset
123 toc_entries[i].addr.msf.frame);
74031379d3cb Initial revision
darius
parents:
diff changeset
124 }
74031379d3cb Initial revision
darius
parents:
diff changeset
125
74031379d3cb Initial revision
darius
parents:
diff changeset
126 }
74031379d3cb Initial revision
darius
parents:
diff changeset
127
74031379d3cb Initial revision
darius
parents:
diff changeset
128 if (machine == 1) {
74031379d3cb Initial revision
darius
parents:
diff changeset
129 printf(" %d\n", disc_length);
74031379d3cb Initial revision
darius
parents:
diff changeset
130 }
74031379d3cb Initial revision
darius
parents:
diff changeset
131 }
74031379d3cb Initial revision
darius
parents:
diff changeset
132
74031379d3cb Initial revision
darius
parents:
diff changeset
133 int
74031379d3cb Initial revision
darius
parents:
diff changeset
134 cddb_sum(int n)
74031379d3cb Initial revision
darius
parents:
diff changeset
135 {
74031379d3cb Initial revision
darius
parents:
diff changeset
136 int ret;
74031379d3cb Initial revision
darius
parents:
diff changeset
137
74031379d3cb Initial revision
darius
parents:
diff changeset
138 /* For backward compatibility this algorithm must not change */
74031379d3cb Initial revision
darius
parents:
diff changeset
139
74031379d3cb Initial revision
darius
parents:
diff changeset
140 ret = 0;
74031379d3cb Initial revision
darius
parents:
diff changeset
141
74031379d3cb Initial revision
darius
parents:
diff changeset
142 while (n > 0) {
74031379d3cb Initial revision
darius
parents:
diff changeset
143 ret = ret + (n % 10);
74031379d3cb Initial revision
darius
parents:
diff changeset
144 n = n / 10;
74031379d3cb Initial revision
darius
parents:
diff changeset
145 }
74031379d3cb Initial revision
darius
parents:
diff changeset
146
74031379d3cb Initial revision
darius
parents:
diff changeset
147 return (ret);
74031379d3cb Initial revision
darius
parents:
diff changeset
148 }
74031379d3cb Initial revision
darius
parents:
diff changeset
149
74031379d3cb Initial revision
darius
parents:
diff changeset
150 unsigned long
74031379d3cb Initial revision
darius
parents:
diff changeset
151 cddb_discid(int tot_trks, struct cd_toc_entry *cdtoc)
74031379d3cb Initial revision
darius
parents:
diff changeset
152 {
74031379d3cb Initial revision
darius
parents:
diff changeset
153 int i,
74031379d3cb Initial revision
darius
parents:
diff changeset
154 t = 0,
74031379d3cb Initial revision
darius
parents:
diff changeset
155 n = 0;
74031379d3cb Initial revision
darius
parents:
diff changeset
156
74031379d3cb Initial revision
darius
parents:
diff changeset
157 /* For backward compatibility this algorithm must not change */
74031379d3cb Initial revision
darius
parents:
diff changeset
158
74031379d3cb Initial revision
darius
parents:
diff changeset
159 i = 0;
74031379d3cb Initial revision
darius
parents:
diff changeset
160
74031379d3cb Initial revision
darius
parents:
diff changeset
161 while (i < tot_trks) {
74031379d3cb Initial revision
darius
parents:
diff changeset
162 n = n + cddb_sum((cdtoc[i].addr.msf.minute * 60)
74031379d3cb Initial revision
darius
parents:
diff changeset
163 + cdtoc[i].addr.msf.second);
74031379d3cb Initial revision
darius
parents:
diff changeset
164 i++;
74031379d3cb Initial revision
darius
parents:
diff changeset
165 }
74031379d3cb Initial revision
darius
parents:
diff changeset
166 t = ((cdtoc[tot_trks].addr.msf.minute * 60) +
74031379d3cb Initial revision
darius
parents:
diff changeset
167 cdtoc[tot_trks].addr.msf.second) -
74031379d3cb Initial revision
darius
parents:
diff changeset
168 ((cdtoc[0].addr.msf.minute * 60) +
74031379d3cb Initial revision
darius
parents:
diff changeset
169 cdtoc[0].addr.msf.second);
74031379d3cb Initial revision
darius
parents:
diff changeset
170
74031379d3cb Initial revision
darius
parents:
diff changeset
171 return ((n % 0xff) << 24 | t << 8 | tot_trks);
74031379d3cb Initial revision
darius
parents:
diff changeset
172 }
74031379d3cb Initial revision
darius
parents:
diff changeset
173
74031379d3cb Initial revision
darius
parents:
diff changeset
174 void
74031379d3cb Initial revision
darius
parents:
diff changeset
175 usage(void)
74031379d3cb Initial revision
darius
parents:
diff changeset
176 {
74031379d3cb Initial revision
darius
parents:
diff changeset
177 printf("Usage:\n");
74031379d3cb Initial revision
darius
parents:
diff changeset
178 printf("cddb-id [-f <cd-dev>] [-m]\n");
74031379d3cb Initial revision
darius
parents:
diff changeset
179 printf("<cd-dev> is the cd device to use (Default: /dev/cd0c)\n");
74031379d3cb Initial revision
darius
parents:
diff changeset
180 printf("-m causes machine readable output\n");
74031379d3cb Initial revision
darius
parents:
diff changeset
181 exit(EX_USAGE);
74031379d3cb Initial revision
darius
parents:
diff changeset
182 }