打開光驅,5秒後再自動關閉光驅的程序: #include <fcntl.h> #include <linux/cdrom.h> #include <stdio.h> #include <sys/ioctl.h> #include <unistd.h>
int main(void) { int fd = open("/dev/cdrom", O_RDONLY | O_NONBLOCK); if (fd == -1) { printf("Failed opening CD-ROM.\n"); return -1; } if (!ioctl(fd, CDROMEJECT, NULL)) printf("Ejected CD-ROM successfully.\n"); else printf("Failed ejecting CD-ROM.\n");
sleep(5); if (!ioctl(fd, CDROMCLOSETRAY, NULL)) printf("Closed CD-ROM successfully.\n"); else printf("Failed closing CD-ROM.\n");
close(fd); return 0; }
|