Torbjxrn Kristoffersen wrote:

> I'm trying to make a program that simply ejects the CD-ROM. The reason
> why is that I'd like to learn more about the ioctl() call.
> 
> Maybe I've misunderstood everything, but here's my code which doesn't
> work:
> 
>     FILE *fd = fopen("/dev/cdrom", "w");

The first argument to ioctl() must be a descriptor (an integer), as
returned from open(), not an ANSI stream (`FILE *').

Also, trying to open /dev/cdrom for writing is likely to fail.

>     int x;
> 
>     ioctl (fd, CDROMEJECT, &x);
> 
>      (I'm not really sure why I'm using 'x', but I saw it being used in
> another example and I saw that
>        ioctl() took a third argument also)

The CDROMEJECT ioctl() doesn't use the third argument, so you can just
pass zero.

The following program works fine for me.

-- 
Glynn Clements <[EMAIL PROTECTED]>

#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>
#include <stdio.h>

int main(void)
{
        int fd = open("/dev/cdrom", O_RDONLY);

        if (fd < 0)
        {
                perror("open(\"/dev/cdrom\") failed");
                return 1;
        }

        if (ioctl(fd, CDROMEJECT, 0) < 0)
        {
                perror("ioctl(CDROMEJECT) failed");
                return 1;
        }

        return 0;
}

Reply via email to