Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / busybox / util-linux / lspci.c
blob514678afd31631cd729dbff06b50343601a6e63f
1 /* vi: set sw=4 ts=4: */
2 /*
3 * lspci implementation for busybox
5 * Copyright (C) 2009 Malek Degachi <malek-degachi@laposte.net>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
10 //usage:#define lspci_trivial_usage
11 //usage: "[-mk]"
12 //usage:#define lspci_full_usage "\n\n"
13 //usage: "List all PCI devices"
14 //usage: "\n"
15 //usage: "\n -m Parsable output"
16 //usage: "\n -k Show driver"
18 #include "libbb.h"
20 enum {
21 OPT_m = (1 << 0),
22 OPT_k = (1 << 1),
26 * PCI_SLOT_NAME PCI_CLASS: PCI_VID:PCI_DID [PCI_SUBSYS_VID:PCI_SUBSYS_DID] [DRIVER]
28 static int FAST_FUNC fileAction(
29 const char *fileName,
30 struct stat *statbuf UNUSED_PARAM,
31 void *userData UNUSED_PARAM,
32 int depth UNUSED_PARAM)
34 parser_t *parser;
35 char *tokens[3];
36 char *pci_slot_name = NULL, *driver = NULL;
37 int pci_class = 0, pci_vid = 0, pci_did = 0;
38 int pci_subsys_vid = 0, pci_subsys_did = 0;
40 char *uevent_filename = concat_path_file(fileName, "/uevent");
41 parser = config_open2(uevent_filename, fopen_for_read);
42 free(uevent_filename);
44 while (config_read(parser, tokens, 3, 2, "\0:=", PARSE_NORMAL)) {
45 if (strcmp(tokens[0], "DRIVER") == 0) {
46 driver = xstrdup(tokens[1]);
47 continue;
50 if (strcmp(tokens[0], "PCI_CLASS") == 0) {
51 pci_class = xstrtou(tokens[1], 16)>>8;
52 continue;
55 if (strcmp(tokens[0], "PCI_ID") == 0) {
56 pci_vid = xstrtou(tokens[1], 16);
57 pci_did = xstrtou(tokens[2], 16);
58 continue;
61 if (strcmp(tokens[0], "PCI_SUBSYS_ID") == 0) {
62 pci_subsys_vid = xstrtou(tokens[1], 16);
63 pci_subsys_did = xstrtou(tokens[2], 16);
64 continue;
67 if (strcmp(tokens[0], "PCI_SLOT_NAME") == 0) {
68 pci_slot_name = xstrdup(tokens[2]);
69 continue;
72 config_close(parser);
75 if (option_mask32 & OPT_m) {
76 printf("%s \"Class %04x\" \"%04x\" \"%04x\" \"%04x\" \"%04x\"",
77 pci_slot_name, pci_class, pci_vid, pci_did,
78 pci_subsys_vid, pci_subsys_did);
79 } else {
80 printf("%s Class %04x: %04x:%04x",
81 pci_slot_name, pci_class, pci_vid, pci_did);
84 if ((option_mask32 & OPT_k) && driver) {
85 if (option_mask32 & OPT_m) {
86 printf(" \"%s\"", driver);
87 } else {
88 printf(" %s", driver);
91 bb_putchar('\n');
93 free(driver);
94 free(pci_slot_name);
96 return TRUE;
99 int lspci_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
100 int lspci_main(int argc UNUSED_PARAM, char **argv)
102 getopt32(argv, "m" /*non-compat:*/ "k" /*ignored:*/ "nv");
104 recursive_action("/sys/bus/pci/devices",
105 ACTION_RECURSE,
106 fileAction,
107 NULL, /* dirAction */
108 NULL, /* userData */
109 0 /* depth */);
111 return EXIT_SUCCESS;