-
obtaining CD info
hey guys, I need to get the number of tracks on a CD, my solution is below, but I am not sure that it is the nicest way to do it, any comments?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <linux/cdrom.h>
struct cdrom_tochdr header; /* struct for the header */
int main( int argc, char *argv[]){
int fh; //CDROM file handle
int firstTrack, lastTrack;
int numberOfTracks;
char *device = "/dev/cdrom"; //default device
/* check for argument in command line */
if( argc > 1 ) //if argument, use it as the device
device = argv[1];
fh = open(device, O_RDONLY); //open device
/* error checking */
if ( fh == -1 ) {
fprintf( stderr,"%s: ", device );
exit(1);
}
/* read the header information */
if(ioctl(fh, CDROMREADTOCHDR, &header) != 0) {
fprintf(stderr, "Error in calling ioctl\n"); /* error handling */
exit(0);
}
/* get number of tracks */
firstTrack = header.cdth_trk0;
lastTrack = header.cdth_trk1;
numberOfTracks = lastTrack; //last track equals total number of tracks
/* print the information */
printf("There are %d tracks on this CD\n", numberOfTracks);
return 0;
}
-
oh, and if anyone wants to compile this here is the Makefile:
Code:
CC=gcc
numtrk: numtrk.o
$(CC) -o numtrk numtrk.o
numtrk.o: numtrk.c
$(CC) -c numtrk.c
clean: rm -f *.o *~ core* numtrk
-
Looks fine to me, just reading it though. Why do you question whether it is good or not? It uses standard interfaces (ioctl), it should be fine.
-
well I'm asking because I was wondering if there is some other way of obtaining the tracks. Maybe CDs have the number of tracks set somewhere in their data - I'm just getting the last track and setting totaltracks equal to it.
-
After flipping through the cdrom.h header, I found this function. You might want to look at it.
Code:
extern void cdrom_count_tracks(struct cdrom_device_info *cdi,tracktype* tracks);
-