LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Program to check whether a disk is in a drive? (https://www.linuxquestions.org/questions/linux-software-2/program-to-check-whether-a-disk-is-in-a-drive-218995/)

foo_bar_foo 08-27-2004 09:31 PM

the eject command simple won't work if there is no disk in the drive
the call to get the file descriptor will fail
you can get some info using
int test = (ioctl(fdcdrom, CDROM_DRIVE_STATUS));
but it doesn't do any good unless you want to close it if it's open or something
no matter how you implement it or in what language it won't open with no disk in it so you are covered
Code:

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

int main () {
  int fd;
  if ((fd = open("dev/cdrom",O_RDONLY ))==-1)
    return 0;
  ioctl (fd, CDROMEJECT);
  close (fd);
  return 0;
}


g4c9z 08-28-2004 09:11 AM

Yes, actually that's a good idea. Actually, while that may be true for the eject ioctl call, it isn't true for the eject command. When I type "eject" at a shell and there's no CD in my drive, the tray pops out. I think the difference is that you haven't passed the O_NONBLOCK flag to the open command; the "eject" program does, as does my program. But your program is probably useful and is simpler than mine too.


All times are GMT -5. The time now is 04:17 PM.