View the number of usb in linux

Source: Internet
Author: User
Tags strtok
In linux, view the number of usb-Linux Enterprise Application-Linux server application information. For more information, see. Hanging out in the library at noon, I saw a dictionary of linux command syntax. I flipped through it, and my eyes fell onto lsusb, listing all the usb devices. Then I suddenly shouted out. So I came back and tried:

[Lbxwz @ localhost ~] $ Lsusb
Bus 005 Device 001: ID :0000
Bus 004 Device 001: ID :0000
Bus 003 Device 001: ID :0000
Bus 002 Device 002: ID 046d: c018 Logitech, Inc.
Bus 002 Device 001: ID :0000
Bus 001 Device 001: ID :0000

It's hard to read/proc/bus/usb/device files.

It turns out that the world is so big.

Haha

I have attached a statistical program about the number of usb, which is used to read the/proc/bus/usb/device file.

# 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}; // used to store the USB name
Int UsbNum = 0; // count the number of USB
Int FileNum = 0; // count the number of characters in the Read File
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) // if the number of characters overflows, the space is allocated separately.
{
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) // filter out the usb information on the non-master> board in the file Array
{
UsbNum ++;
HostUsbFlag = strstr (HostUsbFlag, ArrFound); // use HostUsbFlag to indicate the first address to access USB> information.
I = 0;
While (* HostUsbFlag! = '\ N ')
{
UsbName [I ++] = * (HostUsbFlag ++ );
}
UsbName = '\ 0 ';
UsbNameFlag = strtok (UsbName, "= ");
UsbNameFlag = strtok (NULL, "= ");
ArrFlag = HostUsbFlag;
Strncpy (* (USBInterfacesName-1 + UsbNum), UsbNameFlag, strlen (UsbNameFlag) + 1 );
}
Free (FileArr );
Return (UsbNum); // return the number of USB Interfaces
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.