annotate toys/other/lsusb.c @ 1539:3e85af1f7e22 draft

First batch of sed tests. Only good for TEST_HOST=1 at the moment because the test infrastructure itself depends on sed, so if an unfinished sed is in the $PATH it goes boing. But hey, corner cases! I have... more.
author Rob Landley <rob@landley.net>
date Wed, 29 Oct 2014 18:44:33 -0500
parents 36993c59a3d3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
900
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
1 /* lsusb.c - list available USB devices
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
2 *
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
3 * Copyright 2013 Andre Renaud <andre@bluewatersys.com>
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
4
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
5 USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USR|TOYFLAG_BIN))
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
6
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
7 config LSUSB
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
8 bool "lsusb"
902
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
9 default y
900
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
10 help
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
11 usage: lsusb
902
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
12
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
13 List USB hosts/devices.
900
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
14 */
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
15
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
16 #include "toys.h"
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
17
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
18 static int list_device(struct dirtree *new)
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
19 {
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
20 FILE *file;
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
21 char *name;
902
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
22 int busnum = 0, devnum = 0, pid = 0, vid = 0;
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
23
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
24 if (!new->parent) return DIRTREE_RECURSE;
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
25 if (new->name[0] == '.') return 0;
900
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
26 name = dirtree_path(new, 0);
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
27 snprintf(toybuf, sizeof(toybuf), "%s/%s", name, "/uevent");
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
28 file = fopen(toybuf, "r");
902
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
29 if (file) {
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
30 int count = 0;
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
31
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
32 while (fgets(toybuf, sizeof(toybuf), file))
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
33 if (sscanf(toybuf, "BUSNUM=%u\n", &busnum)
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
34 || sscanf(toybuf, "DEVNUM=%u\n", &devnum)
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
35 || sscanf(toybuf, "PRODUCT=%x/%x/", &pid, &vid)) count++;
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
36
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
37 if (count == 3)
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
38 printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid);
900
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
39 fclose(file);
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
40 }
902
36993c59a3d3 Tighten up lsusb, default to "y".
Rob Landley <rob@landley.net>
parents: 900
diff changeset
41 free(name);
900
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
42
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
43 return 0;
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
44 }
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
45
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
46 void lsusb_main(void)
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
47 {
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
48 dirtree_read("/sys/bus/usb/devices/", list_device);
edd8e6fd418d Attached is a pretty simple implementation of lsub. It doesn't implement any of the flags/options, but does the raw output.
Andre Renaud <andre@bluewatersys.com>
parents:
diff changeset
49 }