您的位置:首页 > 运维架构 > Linux

嵌入式linux自动挂载卸载u盘

2012-09-04 18:12 996 查看
#include <stdio.h>

#include <stdlib.h>

#include <error.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <string.h>

#include <sys/mount.h>

#include <unistd.h>

#include <pthread.h>

int

main()

{

pid_t pc;

int i,fd;

char flag=0;

setvbuf(stdout, NULL, _IONBF, 0);

while (1)

{

int fd;

char buf[1024];

int len;

char *seek=NULL;

fd = open( "/proc/partitions",O_RDONLY);

if (fd>0)

{

/*add by cyx at 20120904 必须先清空否则会出错*/

memset(buf, 0 ,sizeof(buf));

len = read(fd, buf, sizeof(buf));

close(fd);

if (len>0)

{

seek = buf;

seek = strstr(seek, "mmcblk0p1");

if((flag&0x1)==0)

if(seek)

{

chdir("/");

mkdir("mnt/mmc_sd_card", 0777);

system("mount -t vfat /dev/mmc/blk0/part1 /mnt/mmc_sd_card");

flag=flag|0x1;

}

if(seek==NULL)

if(flag&0x1)

{

flag=flag&0xfe;

}

seek = buf;

seek = strstr(seek, "sda1");

if((flag&0x2)==0)

if(seek)

{

printf("mount /mnt/usb_disc1......\n");

chdir("/");

mkdir("mnt/usb_disc1",0777);

system("mount -t vfat /dev/sda1 /mnt/usb_disc1");

flag=flag|0x2;

}

if(seek==NULL)

if(flag&0x2)

{

printf("umount /mnt/usb_disc1......\n");

system("umount /mnt/usb_disc1;rm -rf /mnt/usb_disc1");

system("rm -rf /mnt/usb_disc1");

flag=flag&0xfd;

}

seek = buf;

seek = strstr(seek, "sda2");

if((flag&0x4)==0)

if(seek)

{

chdir("/")

mkdir("mnt/usb_disc2",0777);

system("mount -t vfat /dev/sda2 /mnt/usb_disc2");

flag=flag|0x4;

}

if(seek==NULL)

if(flag&0x4)

{

flag=flag&0xfb;

}

seek = buf;

seek = strstr(seek, "sda3");

if((flag&0x8)==0)

if(seek)

{

chdir("/")

mkdir("mnt/usb_disc3",0777);

system("mount -t vfat /dev/sda3 /mnt/usb_disc3");

flag=flag|0x8;

}

if(seek==NULL)

if(flag&0x8)

{

flag=flag&0xf7;

}

seek = buf;

seek = strstr(seek, "sda4");

if((flag&0x10)==0)

if(seek)

{

chdir("/")

mkdir("mnt/usb_disc4",0777);

system("mount -t vfat /dev/sda4 /mnt/usb_disc4");

flag=flag|0x10;

}

if(seek==NULL)

if(flag&0x10)

{

flag=flag&0xef;

}

}

}

sleep(1);//1秒钟检测一次

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: