Updated for better error messages

This commit is contained in:
Veit Heller
2014-12-17 13:55:06 +01:00
parent ca5fa0bc0e
commit 383e922d22
4 changed files with 43 additions and 22 deletions

View File

@@ -5,19 +5,23 @@
#include <fcntl.h>
#include <linux/cdrom.h>
const char* program_name;
static inline void die(int code, const char* message){
fprintf(stderr, "%s\n", message);
fprintf(stderr, "%s: %s\n", program_name, message);
exit(code);
}
int main(){
int main(int argc, char** argv){
register int fd, caps;
program_name = argv[0];
if((fd = open("/dev/cdrom", O_RDONLY | O_NONBLOCK)) == -1)
die(127, "cd_check_capabilities: Cannot open /dev/cdrom");
die(127, "Cannot open /dev/cdrom");
if((caps = ioctl(fd, CDROM_GET_CAPABILITY)) == -1)
die(127, "cd_check_capabilities: ioctl failed");
die(127, "ioctl failed");
printf("This drive is capable of playing CD-R: %s\n\t\t\t"
"CD-RW: %s\n\t\t\tDVD: %s\n\t\t\tDVD-R: %s\n",

View File

@@ -5,19 +5,23 @@
#include <fcntl.h>
#include <linux/cdrom.h>
const char* program_name;
static inline void die(int code, const char* message){
fprintf(stderr, "%s\n", message);
fprintf(stderr, "%s: %s\n", program_name, message);
exit(code);
}
int main(){
int main(int argc, char** argv){
register int fd;
program_name = argv[0];
if((fd = open("/dev/cdrom", O_RDONLY | O_NONBLOCK)) == -1)
die(127, "cd_check_eject: Cannot open /dev/cdrom");
die(127, "Cannot open /dev/cdrom");
if(ioctl(fd, CDROMEJECT) == -1)
die(127, "cd_check_eject: ioctl() failed");
die(127, "ioctl() failed");
close(fd);

View File

@@ -1,28 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/cdrom.h>
const char* program_name;
static inline void die(int code, const char* message){
fprintf(stderr, "%s\n", message);
fprintf(stderr, "%s: %s\n", program_name, message);
exit(code);
}
int main(){
int main(int argc, char** argv){
register int fd;
struct cdrom_tocentry toc_entry;
struct cdrom_msf start_stop;
if((fd = open("/dev/cdrom", O_RDONLY)) == -1)
die(127, "cd_check_read: Cannot open /dev/cdrom");
program_name = argv[0];
if((fd = open("/dev/cdrom", O_RDONLY)) == -1){
if(errno == ENOMEDIUM)
die(127, "No CD in drive");
else
die(127, "Cannot open /dev/cdrom");
}
toc_entry.cdte_track = 1;
toc_entry.cdte_format = CDROM_MSF;
if(ioctl(fd, CDROMREADTOCENTRY, &toc_entry) == -1)
die(127, "cd_check_read: ioctl() failed");
die(127, "ioctl() failed");
start_stop.cdmsf_min0 = toc_entry.cdte_addr.msf.minute;
start_stop.cdmsf_sec0 = toc_entry.cdte_addr.msf.second;
@@ -32,20 +41,20 @@ int main(){
toc_entry.cdte_format = CDROM_MSF;
if(ioctl(fd, CDROMREADTOCENTRY, &toc_entry) == -1)
die(127, "cd_check_read: ioctl() failed");
die(127, "ioctl() failed");
start_stop.cdmsf_min1 = toc_entry.cdte_addr.msf.minute;
start_stop.cdmsf_sec1 = toc_entry.cdte_addr.msf.second;
start_stop.cdmsf_frame1 = toc_entry.cdte_addr.msf.frame;
if(ioctl(fd, CDROMPLAYMSF, &start_stop) == -1)
die(127, "cd_check_read: ioctl() failed");
die(127, "ioctl() failed");
printf("Press anything to stop playing.\n");
getchar();
if(ioctl(fd, CDROMSTOP) == -1)
die(127, "cd_check_read: ioctl() failed");
die(127, "ioctl() failed");
close(fd);
return 0;

View File

@@ -6,26 +6,30 @@
#include <fcntl.h>
#include <linux/cdrom.h>
const char* program_name;
static inline void die(int code, const char* message){
fprintf(stderr, "%s\n", message);
fprintf(stderr, "%s: %s\n", program_name, message);
exit(code);
}
int main(){
int main(int argc, char** argv){
register int fd;
register unsigned char i;
struct cdrom_tochdr toc_hdr;
struct cdrom_tocentry toc_entry;
program_name = argv[0];
if((fd = open("/dev/cdrom", O_RDONLY)) == -1){
if(errno == ENOMEDIUM)
die(127, "cd_check_read_toc: No CD in drive");
die(127, "No CD in drive");
else
die(127, "cd_check_read_toc: Cannot open /dev/cdrom");
die(127, "Cannot open /dev/cdrom");
}
if(ioctl(fd, CDROMREADTOCHDR, &toc_hdr) == -1)
die(127, "cd_check_read_toc: Cannot get header");
die(127, "Cannot get header");
printf("First track: %d\nLast track: %d\n\n trackno) length\n",
toc_hdr.cdth_trk0, toc_hdr.cdth_trk1);
@@ -35,7 +39,7 @@ int main(){
toc_entry.cdte_format = CDROM_MSF;
if(ioctl(fd, CDROMREADTOCENTRY, &toc_entry) == -1)
die(127, "cd_check_read_toc: Cannot get table of contents");
die(127, "Cannot get table of contents");
printf(" %2d)\t%02d:%02d.%02d\n", i, toc_entry.cdte_addr.msf.minute,
toc_entry.cdte_addr.msf.second,