Aircraft
Aircraft

Reputation: 295

how to write a program to scan for the connected USB and show them in terminal

I was just wondering that if this possible to write a program in c in linux to scan for the usb connected to the system and show them in terminal.

I have a good hand in shell scripting but dont know how to this in C program. In shell scripting, we can use echo command to do many functions but what is the replacement of echo in C language.

Any guide or a sample code will help, thanks.!

Upvotes: 2

Views: 6803

Answers (2)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

You can use libusb which is a more robust way to do what you want, you need root privileges or at least read access to all the usb devices, you can create a udev rule for that, this is the code

#include <libusb-1.0/libusb.h>
#include <assert.h>
#include <stdio.h>

int main()
{
    libusb_context *context;
    libusb_device **list;
    ssize_t         count;
    size_t          index;

    if (libusb_init(&context) != 0)
     {
        fprintf(stderr, "error: intializing `libusb'");
        return -1;
     }

    count = libusb_get_device_list(context, &list);
    for (index = 0; index < count; ++index)
    {
        struct libusb_device           *device;
        struct libusb_device_descriptor descriptor;
        char                            buffer[256];
        struct libusb_device_handle    *handle;
        int                             result;

        device = list[index];
        if ((result = libusb_get_device_descriptor(device, &descriptor)) != 0)
         {
            fprintf(stderr, "error(%d): reading descriptor\n", result);
            continue;
         }

        if ((result = libusb_open(device, &handle)) != 0)
         {
            fprintf(stderr, "error(%d): openning device 0x%04X:0x%04X\n", 
                result, descriptor.idVendor, descriptor.idProduct);
            continue;
         }
        fprintf(stdout, "\ndevice #: %zu 0x%04X:0x%04X\n", 
            index, descriptor.idVendor, descriptor.idProduct);

        result = libusb_get_string_descriptor_ascii(
            handle,
            descriptor.iProduct,
            (unsigned char *)buffer,
            sizeof(buffer)
        );
        if (result != 0)
            fprintf(stdout, "\tproduct     : %s\n", buffer);
        result = libusb_get_string_descriptor_ascii(
            handle,
            descriptor.iManufacturer,
            (unsigned char *)buffer,
            sizeof(buffer)
        );
        if (result != 0)
            fprintf(stdout, "\tmanufacturer: %s\n", buffer);
        libusb_close(handle);
    }
    return 0;
}

remember to pass -lusb-1.0 to the linker command, or if you use a Makefile add it to LDFLAGS.

A simple udev rule to achieve that would be

SUBSYSTEMS=="usb",MODE="0660",GROUP="usb"

adding your user to the usb group.

You could also write a simple dbus program that gives you access to this information, and share it with unprivileged users.

Upvotes: 2

Sir Jo Black
Sir Jo Black

Reputation: 2096

This should be funny! :) As you indicate in the question, I send you a way to get echo in a C program! :)

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <malloc.h>

#define BUF_SIZE 1024

int main(void)
{
    FILE *f;
    char * buf;

    f=popen("lsusb", "r");
    if (f==NULL) {
        perror("1 - Error");
        return errno;
    }

    buf=malloc(BUF_SIZE);
    if (buf==NULL) {
        perror("2 - Error");
        pclose(f);
        return errno;
    }

    while(fgets(buf,BUF_SIZE,f)!=NULL) {
        printf("%s",buf);
    }
    puts("");

    pclose(f);
    free(buf);

    return 0;
}

Upvotes: 3

Related Questions