#include #include #include #include #include #include #include const char* program_name; static inline void die(int code, const char* message, int fd){ if(fd != -1) close(fd); fprintf(stderr, "%s: %s\n", program_name, message); exit(code); } static inline void print_usage(){ printf("Usage: %s [-h]\n\t-h\tdisplays this help message.\n\t\ Calling the program with no arguments will read the \ table of contents of the CD lying in the tray.", program_name); die(127); } int main(int argc, char** argv){ register int fd; register unsigned char i; struct cdrom_tochdr toc_hdr; struct cdrom_tocentry toc_entry; if(!argv) die(127, "How did you do that? No argv?", -1); program_name = argv[0]; if(argc > 1) print_usage(); if((fd = open("/dev/cdrom", O_RDONLY)) == -1){ if(errno == ENOMEDIUM) die(127, "No CD in drive", fd); else die(127, "Cannot open /dev/cdrom", fd); } if(ioctl(fd, CDROMREADTOCHDR, &toc_hdr) == -1) die(127, "Cannot get header", fd); printf("First track: %d\nLast track: %d\n\n trackno) length\n", toc_hdr.cdth_trk0, toc_hdr.cdth_trk1); for(i = toc_hdr.cdth_trk0; i <= toc_hdr.cdth_trk1; i++){ toc_entry.cdte_track = i; toc_entry.cdte_format = CDROM_MSF; if(ioctl(fd, CDROMREADTOCENTRY, &toc_entry) == -1) die(127, "Cannot get table of contents", fd); printf(" %2d)\t%02d:%02d.%02d\n", i, toc_entry.cdte_addr.msf.minute, toc_entry.cdte_addr.msf.second, (toc_entry.cdte_addr.msf.frame*100+27)/75); } close(fd); return 0; }