cd-utils 1.0.0

Small interface to cd-rom library
Documentation
#include <stdio.h>

#include <fcntl.h>
#include <linux/cdrom.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

#include "cd-utils.h"


bool cdrom_has_cd(const char *device) {
    int fd;

    // printf("cdrom_has_cd(\"%s\")\n", device);

	if ((fd = open(device, O_RDONLY | O_NONBLOCK)) <  0) {
		perror("open");
		return 0;
	}

	int ret = ioctl(fd,CDROM_DRIVE_STATUS, 0);

    // ioctl(fd, CDROM_LOCKDOOR, 0);
	close(fd);
	
	return (ret == CDS_DISC_OK);
}

int cdrom_media_type(const char *device) {
    int fd;

    // printf("cdrom_media_type(\"%s\")\n", device);

	if ((fd = open(device, O_RDONLY | O_NONBLOCK)) <  0) {
		perror("open");
		return -1;
	}

	int retval = 0;
	int ret = ioctl(fd,CDROM_DRIVE_STATUS, 0);
	if (ret == CDS_DISC_OK) {
		retval = 0;
		int fd2 = ioctl(fd,CDROM_DISC_STATUS,0);
		switch(fd2) {
			case CDS_AUDIO:
				retval = 1;
				break;
			case CDS_MIXED:
				retval = 2;
				break;
			case CDS_DATA_1:
				retval = 3;
				break;
			case CDS_XA_2_2:
			case CDS_XA_2_1:
				retval = 4;
				break;
			case CDS_NO_INFO:
			default:
				break;
        }
	}
	else {
		retval = -1;
	}

    // ioctl(fd, CDROM_LOCKDOOR, 0);
	close(fd);

    return retval;
}

bool cdrom_eject(const char *device) {
	int fd;

	// printf("cdrom_eject(\"%s\")\n", device);

	if ((fd = open(device, O_RDONLY | O_NONBLOCK)) <  0) {
		perror("open");
		return 0;
	}

    ioctl(fd, CDROM_LOCKDOOR, 0);
	int error_code = ioctl(fd, CDROMEJECT, 0);
    close(fd);
    return (error_code == 0);
}