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;
}