C++实现Linux下弹出U盘的方法
来源:互联网 发布:龙猫头像知乎 编辑:程序博客网 时间:2024/06/05 02:41
转载http://www.jb51.net/article/69806.htm
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <linux/fd.h>
#include <sys/mount.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <scsi/scsi_ioctl.h>
int main(int argc, char *argv[])
{
int fd = -1;
char *device;
if (argc != 2)
{
printf("usage: usb-s /dev/sde1");
return -1;
}
device = strdup(argv[1]);
if ((fd = open(device, O_RDONLY|O_NONBLOCK)) < 0)
{
printf("open device %s failed!\n", device);
free(device);
return -1;
}
int status, k;
sg_io_hdr_t io_hdr;
unsigned char allowRmBlk[6] = {ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0};
unsigned char startStop1Blk[6] = {START_STOP, 0, 0, 0, 1, 0};
unsigned char startStop2Blk[6] = {START_STOP, 0, 0, 0, 2, 0};
unsigned char inqBuff[2];
unsigned char sense_buffer[32];
if ((ioctl(fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
printf("not an sg device, or old sg driver\n");
goto out;
}
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = 6;
io_hdr.mx_sb_len = sizeof(sense_buffer);
io_hdr.dxfer_direction = SG_DXFER_NONE;
io_hdr.dxfer_len = 0;
io_hdr.dxferp = inqBuff;
io_hdr.sbp = sense_buffer;
io_hdr.timeout = 10000;
io_hdr.cmdp = allowRmBlk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
{
goto out;
}
io_hdr.cmdp = startStop1Blk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
{
goto out;
}
io_hdr.cmdp = startStop2Blk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
{
goto out;
}
/* force kernel to reread partition table when new disc inserted */
status = ioctl(fd, BLKRRPART);
out:
close(fd);
free(device);
return 0;
}
运行
sudo usb-s /dev/sdb1
- C++实现Linux下弹出U盘的方法
- Linux下弹出U盘的代码
- Linux下弹出U盘的代码
- linux下制作u盘启动盘的方法
- linux下制作u盘启动盘的方法
- linux下制作u盘启动盘的方法
- linux下制作u盘启动盘的方法
- 图文详解Linux下使用U盘的方法
- windows下 安装Kali Linux到 U盘的方法
- 嵌入式linux下u盘自动挂载的实现
- 嵌入式linux下u盘自动挂载的实现
- 嵌入式linux下u盘自动挂载的实现
- linux下格式化U盘方法
- Linux下实现U盘自动检测
- linux下c语言有一些神奇的gotoxy()实现方法
- Linux下c实现域名转IP的方法封装
- J.U.C下的并发容器的并发实现
- LINUX 下实现U盘的自己挂载和卸载,实现即插即用的功能
- 创建Java多线程的多种方法ouchuquan
- idea破解网址
- Selenium2 入门[2] —— WebDrive 简单的小例子,访问百度搜索内容
- python装饰器
- 一年了---胡思乱想
- C++实现Linux下弹出U盘的方法
- 深入浅出Java多线程(1)-方法 join
- 2016码农谷全国大学生程序设计邀请赛(测试赛)
- 编译opencv3.1.0时报错unable to find a javc compiler
- 自定义ImageView实现拖动、旋转、缩放功能
- SVN版本控制与分支设置
- 92.UITableView键盘收起,点击UITableView空白处收起键盘
- 如何停止java线程
- 动态规划——物品无限的背包问题