- //弹出光驱
- #include <unistd.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <linux/cdrom.h>
- #define DEVICE "/dev/cdrom"
- int fd = open(DEVICE, O_RDONLY|O_NONBLOCK);
- if (fd < 0) {
- perror("unable to open "DEVICE);
- exit(1);
- }
- if (ioctl(fd, CDROMEJECT) != 0) {
- perror("CDROMEJECT ioctl failed");
- exit(1);
- }
- if (close(fd) != 0) {
- perror("unable to close "DEVICE);
- exit(1);
- }
- //收回光驱
- #include <unistd.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <linux/cdrom.h>
- #define DEVICE "/dev/cdrom"
- int fd = open(DEVICE, O_RDONLY|O_NONBLOCK);
- if (fd < 0) {
- perror("unable to open "DEVICE);
- exit(1);
- }
- if (ioctl(fd, CDROMCLOSETRAY,0) != 0) {
- perror("CDROMCLOSETRAY ioctl failed");
- exit(1);
- }
- if (close(fd) != 0) {
- perror("unable to close "DEVICE);
- exit(1);
- }
- //该片段来自于http://www.codesnippet.cn/detail/020820134944.html
来源: http://www.codesnippet.cn/detail/020820134944.html