📄 usb_auto_mount.c
字号:
#include <string.h>
#include <stdio.h>
#include <sys/mount.h>
//char sysFileName;
char g_hdPath[10][30];//zhb for hdd missed 20061205
int part_in_use[10] = {0};
#define MAX_READ_BUFFER_SIZE 1024
#define MAX_PARTITION_NAME_LENGTH 5
char sdimformations[10][6];
int x=0,y=0;
int main(void)
{
FILE *pf;
char stream[MAX_READ_BUFFER_SIZE+1];
char partition[MAX_PARTITION_NAME_LENGTH];
int major, minor, size;
while(1)
{
system("cat /proc/partitions > /tmp/usb");
if (!(pf = fopen("/proc/partitions", "r")))
{
perror("open /proc/partitions");
return 0 ;
}
x=0;
while (fgets(stream, MAX_READ_BUFFER_SIZE, pf))
{
sscanf(stream, "\t%d\t%d\t%d\t%s", &major, &minor,& size, partition);
if ((strncmp(partition, "sd", 2) == 0)&&strlen(partition)==3 )
x++;
}
printf("x:%d",x);
if(x!=y)
{
UpdateDevices();
y=x;
}
fclose(pf);
}
return 0 ;
}
int UpdateDevices()
{
HD_UnMount(2);
memset(sdimformations,0,sizeof(sdimformations));
int Host0Count = USB_Host_Mount();
printf("host0count = %d \n",Host0Count);
return 1;
}
int USB_Host_Mount()
{
int i = 0 ;
int j = 0;
int z = 1;
char path[60];
char hdd[30];
memset(g_hdPath,0,sizeof(g_hdPath));
//memset(g_mountPath,0,sizeof(g_mountPath));
FILE *pf;
char stream[MAX_READ_BUFFER_SIZE+1];
char partition[MAX_PARTITION_NAME_LENGTH];
int major, minor, size;
int g=0,e=0,k=0,m=0;
memset(sdimformations,0,sizeof(sdimformations));
if (!(pf = fopen("/tmp/usb", "r")))
{
perror("open /proc/partitions");
return 0 ;
}
g=0;
while (fgets(stream, MAX_READ_BUFFER_SIZE, pf))
{
memset(partition,0,sizeof(partition));
sscanf(stream, "\t%d\t%d\t%d\t%s", &major, &minor,& size, partition);
printf("major==%d\n",major);
if ((strncmp(partition, "sd", 2) == 0)&&strlen(partition)>3 )
{
for(m=0;m<10;m++)
{
if ((strncmp(partition, sdimformations[m], 5) == 0))
printf("m=%d",m);
break;
}
strcpy(sdimformations[g],partition);
printf("partition%d:[%s]\n",g,partition);
printf("m:%d\n",m);
g++;
printf("g:%d\n",g);
}
}
for(e=0;e<5;e++)
{
for(k=0;k<5;k++)
{
printf("sdimformations[%d][%d]%c\n",e,k,sdimformations[e][k]);
}
}
printf("\rcome into findpart\n");
for(i=0;i < 10;i++)
{
sprintf(path,"/dev/%s",sdimformations[i]);
printf("path==%s\n",path);
while(part_in_use[z-1]!= 0 && z <=10)
z++;
if(z>10) {
HD_UnMount(2);
z=1;
while(part_in_use[z-1]!= 0 && z <=10)
z++;
}
sprintf(hdd,"/mnt/USB%d",z);
printf("hdd=%s\n",hdd);
if(!(mount (path, hdd, "vfat", MS_RDONLY |MS_NOSUID | MS_NODEV, "iocharset=utf8")))
{
part_in_use[z-1] = 1;
sprintf(g_hdPath[j]," USB%d",z);
printf("vfat mount to %s seeceful!\n",hdd);
j++;
}
else if(!(mount (path, hdd, "ntfs",MS_RDONLY|MS_NOSUID | MS_NODEV, "nls=utf8")))
{
part_in_use[z-1] = 1;
sprintf(g_hdPath[j]," USB%d",z);
printf("ntfs mount to %s seeceful!\n",hdd);
j++;
}
else continue;
}
fclose(pf);
return j;
}
int HD_UnMount(int n)
{
int i=0;
char hdd[20];
if(n== 1) //HD
{
for(i=1;i<= 10;i++)
{
sprintf(hdd,"umount /mnt/HD%d ",i);
printf("%s\n",hdd);
system(hdd);
}
}
if(n==2) //USB HD
{
for(i=1;i <= 10;i++)
{
part_in_use[i-1]= 0;
sprintf(hdd,"umount /mnt/USB%d ",i);
printf("%s\n",hdd);
if((umount(hdd)) !=0 )
printf("umount fail\n");
else
printf("umount successer \n");
system(hdd);
}
}
if(n==3) //NFS
{
if((umount("/mnt/nfs1/")) !=0 )
printf("umount NFS fail\n");
else
printf("umount NFS successer \n");
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -