Initial commit

This commit is contained in:
Veit Heller
2014-09-15 17:57:04 +02:00
parent 36c0d500a2
commit 3dc5b6f5e0
5 changed files with 195 additions and 0 deletions

33
Makefile Normal file
View File

@@ -0,0 +1,33 @@
override CFLAGS+=-Werror -Wall -g -fPIC -O2 -DNDEBUG -std=c99 -ftrapv -Wfloat-equal -Wundef -Wwrite-strings -Wconversion -Wuninitialized
PREFIX=/usr/bin/cd_check/
BUILDDIR=bin/
SRCDIR=src/
#Makes everything
all:
mkdir $(BUILDDIR) 2> /dev/null || true
cc $(CFLAGS) $(SRCDIR)cd_check_eject.c -o $(BUILDDIR)cd_check_eject
cc $(CFLAGS) $(SRCDIR)cd_check_capabilities.c -o $(BUILDDIR)cd_check_capabilities
cc $(CFLAGS) $(SRCDIR)cd_check_read_toc.c -o $(BUILDDIR)cd_check_read_toc
cc $(CFLAGS) $(SRCDIR)cd_check_read.c -o $(BUILDDIR)cd_check_read
#Uses picky extensions and makes everything(Extensions may break compiling)
dev:
make all CFLAGS+=-Wshadow -Wunreachable-code -Wswitch-enum -Wswitch-default -Wcast-align -Winit-self -Wpointer-arith -Weffc++
#Cleans directory(no uninstall!)
clean:
rm -rf $(BUILDDIR)
#Installs into specified(or default) directory
install:
install -d $(PREFIX)
install $(BUILDDIR)cd_check_eject $(PREFIX)cd_check_eject
install $(BUILDDIR)cd_check_capabilities $(PREFIX)cd_check_capabilities
install $(BUILDDIR)cd_check_read_toc $(PREFIX)cd_check_read_toc
install $(BUILDDIR)cd_check_read $(PREFIX)cd_check_read
#Uninstalls from specified(or default)directory
uninstall:
rm -rf $(PREFIX)

View File

@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/cdrom.h>
static inline void die(int code, const char* message){
fprintf(stderr, "%s\n", message);
exit(code);
}
int main(){
register int fd, caps;
if((fd = open("/dev/cdrom", O_RDONLY | O_NONBLOCK)) == -1)
die(127, "cd_check_capabilities: Cannot open /dev/cdrom");
if((caps = ioctl(fd, CDROM_GET_CAPABILITY)) == -1)
die(127, "cd_check_capabilities: 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",
(caps & CDC_CD_R) ? "yes" : "no",
(caps & CDC_CD_RW) ? "yes" : "no",
(caps & CDC_DVD) ? "yes" : "no",
(caps & CDC_DVD_R) ? "yes" : "no");
printf("It can open the tray: %s\n\t\tclose the tray: %s\n\t\t"
"lock the tray: %s\n\t\tplay audio: %s\n\t\t"
"select discs:%s\n",
(caps & CDC_OPEN_TRAY) ? "yes" : "no",
(caps & CDC_CLOSE_TRAY) ? "yes" : "no",
(caps & CDC_LOCK) ? "yes" : "no",
(caps & CDC_PLAY_AUDIO) ? "yes" : "no",
(caps & CDC_SELECT_DISK) ? "yes" : "no");
close(fd);
return 0;
}

25
src/cd_check_eject.c Normal file
View File

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

52
src/cd_check_read.c Normal file
View File

@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/cdrom.h>
static inline void die(int code, const char* message){
fprintf(stderr, "%s\n", message);
exit(code);
}
int main(){
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");
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");
start_stop.cdmsf_min0 = toc_entry.cdte_addr.msf.minute;
start_stop.cdmsf_sec0 = toc_entry.cdte_addr.msf.second;
start_stop.cdmsf_frame0 = toc_entry.cdte_addr.msf.frame;
toc_entry.cdte_track = CDROM_LEADOUT;
toc_entry.cdte_format = CDROM_MSF;
if(ioctl(fd, CDROMREADTOCENTRY, &toc_entry) == -1)
die(127, "cd_check_read: 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");
printf("Press anything to stop playing.\n");
getchar();
if(ioctl(fd, CDROMSTOP) == -1)
die(127, "cd_check_read: ioctl() failed");
close(fd);
return 0;
}

45
src/cd_check_read_toc.c Normal file
View File

@@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/cdrom.h>
static inline void die(int code, const char* message){
fprintf(stderr, "%s\n", message);
exit(code);
}
int main(){
register int fd, i;
struct cdrom_tochdr toc_hdr;
struct cdrom_tocentry toc_entry;
if((fd = open("/dev/cdrom", O_RDONLY)) == -1)
if(errno == ENOMEDIUM)
die(127, "cd_check_read_toc: No CD in drive");
else
die(127, "cd_check_read_toc: Cannot open /dev/cdrom");
if(ioctl(fd, CDROMREADTOCHDR, &toc_hdr) == -1)
die(127, "cd_check_read_toc: Cannot get header");
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, "cd_check_read_toc: 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,
(toc_entry.cdte_addr.msf.frame*100+27)/75);
}
close(fd);
return 0;
}