[clug] Is a CD in a drive..

Ishwor nepbabu.cx at gmail.com
Wed Mar 25 07:59:37 GMT 2009


Hi,

Jeremy Kerr wrote:

[...]

> bash ioctl bindings, anyone? :)

And here's hackish C version cooked & souped up just a while ago :p

[isg at tuxbuntu:workspace/C]$ cat -n check_cd.c

     1	#include <linux/cdrom.h>
     2	#include <stdio.h>
     3	#include <sys/types.h>
     4	#include <sys/stat.h>
     5	#include <sys/ioctl.h>
     6	#include <fcntl.h>
     7	#include <errno.h>
     8	
     9	// from <linux/cdrom.h>
    10	//#define CDS_NO_INFO			0
    11	//#define CDS_NO_DISC             	1
    12	//#define CDS_TRAY_OPEN           	2
    13	//#define CDS_DRIVE_NOT_READY     	3
    14	//#define CDS_DISC_OK             	4
    15	
    16	//return drive status
    17	char*
    18	status(int r)
    19	{
    20		switch(r)
    21		{
    22			case 0:
    23				return "Not implemented";
    24			case 1:
    25				return "No disc loaded";
    26			case 2:
    27				return "Tray is open";
    28			case 3:
    29				return "Drive not ready";
    30			case 4:
    31				return "Disc OK!";
    32			default:
    33				return "Houston we have a problem here";
    34		}
    35		
    36	}
    37	
    38	
    39	int
    40	main()
    41	{
    42		int fd = open("/dev/scd0", O_RDWR|O_EXCL|O_NONBLOCK);
    43		int res = ioctl(fd, CDROM_DRIVE_STATUS,0);
    44		printf("Status: %s\n", status(res));
    45		if (errno)
    46			perror("Error occured");
    47	
    48	return 0;
    49	
    50	}

Regards,
Ishwor


More information about the linux mailing list