Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to eject the CD Drive on Linux using C?

Tags:

c

linux

I was reading through this Advanced Linux Programming tutorial when I encountered a problem. I was trying to eject the CD-ROM drive using this code:

int fd = open(path_to_cdrom, O_RDONLY);

// Eject the CD-ROM drive
ioctl(fd, CDROMEJECT);

close(fd);

Then I try to compile this code and get the following output:

In file included from /usr/include/linux/cdrom.h:14,
                 from new.c:2:
/usr/include/asm/byteorder.h: In function ‘___arch__swab32’:
/usr/include/asm/byteorder.h:19: error: expected ‘)’ before ‘:’ token
/usr/include/asm/byteorder.h: In function ‘___arch__swab64’:
/usr/include/asm/byteorder.h:43: error: expected ‘)’ before ‘:’ token

So what am I doing wrong?

like image 933
Kredns Avatar asked Dec 09 '22 19:12

Kredns


2 Answers

The error message you're seeing looks like something is wrong in your #include lines, not with the code you posted. I tried compiling http://www.advancedlinuxprogramming.com/listings/chapter-6/cdrom-eject.c and it compiles just fine.

like image 161
Laurence Gonsalves Avatar answered Dec 15 '22 01:12

Laurence Gonsalves


According to this, you need to specify O_NONBLOCK when opening the device, otherwise it won't work.

From that page:

cdrom = open(CDDEVICE,O_RDONLY | O_NONBLOCK)

like image 25
mrduclaw Avatar answered Dec 14 '22 23:12

mrduclaw