You can ask the cd writer (using c code) if it has a cd-rom in it using the ioctls as described in cdrom.h. For me that file is in /usr/include/linux/cdrom.h.
For example, I wrote this program which returns 0 if there's a cd in the drive, or nonzero if there isn't a cd in the drive ready to go. It appears to work for me. I suspect there are also ioctls for checking if the cd-rom is full.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <linux/cdrom.h>
#include <string.h>
// Return 0 if a cd is in the drive
// Return nonzero if no cd is in the drive
#define DEFAULT_DRIVE "/dev/cdrom"
int main(int argc, char* argv[])
{
char* drive = DEFAULT_DRIVE;
int i;
// Get the drive location from command line.
for (i = 1; i < argc; i++)
{
if (strcmp("-h", argv[i]) == 0)
{
printf("cdindrive [-h] [drive]\n");
printf("\t returns 0 if cdindrive, else nonzero.\n");
printf("\t -h prints this help.\n");
printf("\t drive is the cd drive to check (ex: /dev/cdrom0)\n");
}
else
{
drive = argv[i];
}
}
// open a descriptor to the drive
int fd;
fd = open(drive, O_RDONLY);
if (fd < 0)
{
if (errno == ENOMEDIUM)
{
// This happened when no cd was in the drive.
return 1;
}
perror("open");
return 1;
}
// See if it's ready
switch (ioctl(fd, CDROM_DRIVE_STATUS))
{
case -1:
perror("ioctl");
return 1;
case CDS_NO_INFO:
case CDS_NO_DISC:
case CDS_TRAY_OPEN:
case CDS_DRIVE_NOT_READY:
return 1;
case CDS_DISC_OK:
return 0;
}
return 1;
}
|