2007/01/17

cdrom tray status: 偵測光碟機狀態



/*
* cdstatus.c <device>
*
* This loads a CDROM from a specified slot in a changer, and displays
* information about the changer status. The drive should be unmounted before
* using this program.
*
* Based on code originally from Gerhard Zuber .
* Changer status information, and rewrite for the new Uniform CDROM driver
* interface by Erik Andersen <andersee@debian.org>.
*/

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

int
main (int argc, char **argv)
{
char *program;
char *device;
int fd; /* file descriptor for CD-ROM device */
int status; /* return status for system calls */

if (argc != 2) {
fprintf (stderr, "usage: %s \n", program);
exit (1);
}

program = argv[0];
device = argv[1];

/* open device */
fd = open(device, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
fprintf (stderr, "%s: open failed for `%s': %s\n",
program, device, strerror (errno));
exit (1);
}

status = ioctl (fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
if (status<0) {
perror(" CDROM_DRIVE_STATUS");
} else switch(status) {
case CDS_NO_INFO: printf ("NO_INFO\n"); break;
case CDS_NO_DISC: printf ("NO_DISC\n"); break;
case CDS_TRAY_OPEN: printf ("TRAY_OPEN\n"); break;
case CDS_DRIVE_NOT_READY: printf ("NOT_READY\n"); break;
case CDS_DISC_OK: printf ("DISC_OK\n"); break;
default: printf ("ERROR: %d\n", status); break;
}

/* close device */
close (fd);
exit (0);
}

0 意見: