Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10502127
  • 博文数量: 2905
  • 博客积分: 20098
  • 博客等级: 上将
  • 技术积分: 36298
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-23 05:00
文章存档

2012年(1)

2011年(3)

2009年(2901)

分类: LINUX

2009-03-23 11:29:21

中午在图书馆闲逛,看到一本《linux指令语法辞典》,随手翻了翻,眼睛落在lsusb     列出所有usb设备那一行,顿时狂喊晕。。。于是回来试了下:
[lbxwz@localhost ~]$ lsusb
Bus 005 Device 001: ID 0000:0000 
Bus 004 Device 001: ID 0000:0000 
Bus 003 Device 001: ID 0000:0000 
Bus 002 Device 002: ID 046d:c018 Logitech, Inc.
Bus 002 Device 001: ID 0000:0000 
Bus 001 Device 001: ID 0000:0000 
想我们原来还煞费苦心的读取/proc/bus/usb/device文件 真是太苦了
原来,世界是那么的大。。
呵呵
现附上我同学写的一个关于usb个数的统计程序 ,用的就是读/proc/bus/usb/device文件 ,只能说是麻烦了些。

#include "include.h"

#ifdef USBPATH 
#define USBPATH "/proc/bus/usb/devices" 
#else
#define USBPATH "/proc/bus/usb/devices" 
#endif

#define BUFSIZE 512 
#define EXTENDSIZE 256 

int IfExistUSBInterface(char **USBInterfacesName)
{
    FILE *FileStream;
    char *FileArr;
    char *ArrFlag;
    char *HostUsbFlag;
    char *UsbNameFlag;
    char ArrFound[] = "Product";
    char UsbName[32] = {0};  //用来存储USB的名字 
    int UsbNum = 0;          //统计USB的数量 
    int FileNum = 0;         //统计读取文件中的字符个数 
    int BufSize;
    int i;

    BufSize = BUFSIZE;
    FileArr = (char *)malloc(BufSize*sizeof(char));
    ArrFlag = FileArr;

    if((FileStream = fopen(USBPATH,"r")) == NULL)
    {
        printf("File open error!\n");
        exit(0);
    }

    while(!feof(FileStream))
    {
        *ArrFlag++ = fgetc(FileStream);
        FileNum ++;
        if(FileNum > BufSize) // 如果字符个数溢出则进行在另行分配空间 
        {
            BufSize += EXTENDSIZE;
            FileArr = (char *)realloc(FileArr,BufSize*sizeof(char));
            if(FileArr == NULL)
            {
                printf("alloc error!\n");
                exit(0);
            }
            ArrFlag = FileArr + FileNum;
        }
    }
    *(ArrFlag-1) = '\0';
    fclose(FileStream);

    ArrFlag = FileArr;
    while((HostUsbFlag = strstr(ArrFlag,"B:")) != NULL)//过滤掉文件数组中的非主>板上的usb信息 
    {
        UsbNum ++;
        HostUsbFlag = strstr(HostUsbFlag,ArrFound);//用HostUsbFlag来表示存取USB>信息的首地址 
        i = 0;
        while(*HostUsbFlag != '\n')
        {
            UsbName[i++] = *(HostUsbFlag++);
        }
        UsbName[i] = '\0';
        UsbNameFlag = strtok(UsbName,"=");
        UsbNameFlag = strtok(NULL,"=");
        ArrFlag = HostUsbFlag;
        strncpy(*(USBInterfacesName-1+UsbNum),UsbNameFlag,strlen(UsbNameFlag)+1);
    }
    free(FileArr);
    return(UsbNum);//返回USB接口的数量 
}

阅读(1471) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~