view toys/other/lsusb.c @ 1178:f8432a8872e0 draft

More work on the cleanup page, partway through describing ifconfig.
author Rob Landley <rob@landley.net>
date Wed, 01 Jan 2014 15:00:44 -0600
parents 36993c59a3d3
children
line wrap: on
line source

/* lsusb.c - list available USB devices
 *
 * Copyright 2013 Andre Renaud <andre@bluewatersys.com>

USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USR|TOYFLAG_BIN))

config LSUSB
  bool "lsusb"
  default y
  help
    usage: lsusb

    List USB hosts/devices.
*/

#include "toys.h"

static int list_device(struct dirtree *new)
{
  FILE *file;
  char *name;
  int busnum = 0, devnum = 0, pid = 0, vid = 0;

  if (!new->parent) return DIRTREE_RECURSE;
  if (new->name[0] == '.') return 0;
  name = dirtree_path(new, 0);
  snprintf(toybuf, sizeof(toybuf), "%s/%s", name, "/uevent");
  file = fopen(toybuf, "r");
  if (file) {
    int count = 0;

    while (fgets(toybuf, sizeof(toybuf), file))
      if (sscanf(toybuf, "BUSNUM=%u\n", &busnum)
          || sscanf(toybuf, "DEVNUM=%u\n", &devnum)
          || sscanf(toybuf, "PRODUCT=%x/%x/", &pid, &vid)) count++;

    if (count == 3)
      printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid);
    fclose(file);
  }
  free(name);

  return 0;
}

void lsusb_main(void)
{
  dirtree_read("/sys/bus/usb/devices/", list_device);
}