[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

ejecting CD in a program....



Anyone know the reason for this mysterious behaviour?:

I am writing a little application which has to wait for a CD to be
loaded into the drive, obtain some data from the CD, and then eject
it ready for the next one....

What I have found is that if there is initially no CD in the drive,
the program waits successfully, but fails to eject. If there is an
initial CD in the drive it does not need to wait, and the eject
succeeds..

With a little trial and error, I have found that closing and re-opening
the device before the eject can solve the problem, but ONLY with a
sleep between close and open.

If I leave the device open, then no amount of sleeping before the
eject will help...

Here is the piece of test code that is doing this:

	int main(void)
	{
	        int             tracks, i;
	        int             cdfd;
	
	        if((cdfd = open(drivename, O_RDONLY | O_NONBLOCK)) < 0)
	        {
	                perror(drivename);
	                exit(1);
	        }
	        printf("waiting for CD\n");
	        while(ioctl(cdfd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)
	                sleep(2);
	        tracks = read_toc(cdfd);
	/* why ?? */
 	        close(cdfd);
	        sleep(1);
	        if((cdfd = open(drivename, O_RDONLY | O_NONBLOCK)) < 0)
	        {
	                printf("%s: cannot open\n", drivename);
	                exit(1);
	        }
	/* end why */
	        if(ioctl(cdfd, CDROMEJECT) < 0)
	                perror("CDROMEJECT");
	        close(cdfd);
	        exit(0);
	}

This code does what I want, but the unexplained need for the close/sleep/open
looks ugly.

On my laptop the turnaround time from drive closure to eject is just under
20 seconds.

Regards,
DigbyT
-- 
Digby R. S. Tarvin                                          digbyt(at)digbyt.com
http://www.digbyt.com



Reply to: