Mercurial > hg > toybox
annotate toys/other/lsusb.c @ 1183:0752b2d58909 draft
Rename xmsprintf() to just xmprintf().
Partly because there's no supplied target string ala sprintf, and partly
because I can never remember what order the m and s go in.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 16 Jan 2014 09:26:50 -0600 |
parents | 36993c59a3d3 |
children |
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 } |