The author has 4 posts.
Font size: Small - 100% (Default)  Content converter: No conversion
 
Clicks Replies
1402 3
[有趣的程序]Linux系統下彈出光驅的C程序(已在Fedora22下測試通過)
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 1 Posted at: 7/15/15 22:08
#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");
    close(fd);
    return 0;
}
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 2 Posted at: 7/15/15 22:10
由于open光驱时,可能光驱中没有光盘,因此需要使用O_NONBLOCK选项,否则open会失败(系統默認為我們打開這個光驅的目的是查看光盤上的文件)
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 3 Posted at: 7/15/15 22:24
打開光驅,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;
}
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 4 Posted at: 7/15/15 22:24
[octopus@pc3 cdrom]$ ./cdrom
Ejected CD-ROM successfully.
Closed CD-ROM successfully.
[octopus@pc3 cdrom]$

Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
(Shortcut key: Ctrl+Enter)
Post Information
Clicks: 1402 Replies: 3
Author: 巨大八爪鱼
Last reply: 巨大八爪鱼
Last reply time: 7/15/15 22:24
Announcements