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

2019-07-13 00:44发布

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

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...... ");
                             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...... ");
                           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秒钟检测一次

       }
}