losetup: use LOOP_foo defines
[oweals/busybox.git] / util-linux / lspci.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * lspci implementation for busybox
4  *
5  * Copyright (C) 2009  Malek Degachi <malek-degachi@laposte.net>
6  *
7  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8  */
9 #include "libbb.h"
10
11 enum {
12         OPT_m = (1 << 0),
13         OPT_k = (1 << 1),
14 };
15
16 /*
17  * PCI_SLOT_NAME PCI_CLASS: PCI_VID:PCI_DID [PCI_SUBSYS_VID:PCI_SUBSYS_DID] [DRIVER]
18  */
19 static int FAST_FUNC fileAction(
20                 const char *fileName,
21                 struct stat *statbuf UNUSED_PARAM,
22                 void *userData UNUSED_PARAM,
23                 int depth UNUSED_PARAM)
24 {
25         parser_t *parser;
26         char *tokens[3];
27         char *pci_slot_name = NULL, *driver = NULL;
28         int pci_class = 0, pci_vid = 0, pci_did = 0;
29         int pci_subsys_vid = 0, pci_subsys_did = 0;
30
31         char *uevent_filename = concat_path_file(fileName, "/uevent");
32         parser = config_open2(uevent_filename, fopen_for_read);
33         free(uevent_filename);
34
35         while (config_read(parser, tokens, 3, 2, "\0:=", PARSE_NORMAL)) {
36                 if (strcmp(tokens[0], "DRIVER") == 0) {
37                         driver = xstrdup(tokens[1]);
38                         continue;
39                 }
40
41                 if (strcmp(tokens[0], "PCI_CLASS") == 0) {
42                         pci_class = xstrtou(tokens[1], 16)>>8;
43                         continue;
44                 }
45
46                 if (strcmp(tokens[0], "PCI_ID") == 0) {
47                         pci_vid = xstrtou(tokens[1], 16);
48                         pci_did = xstrtou(tokens[2], 16);
49                         continue;
50                 }
51
52                 if (strcmp(tokens[0], "PCI_SUBSYS_ID") == 0) {
53                         pci_subsys_vid = xstrtou(tokens[1], 16);
54                         pci_subsys_did = xstrtou(tokens[2], 16);
55                         continue;
56                 }
57
58                 if (strcmp(tokens[0], "PCI_SLOT_NAME") == 0) {
59                         pci_slot_name = xstrdup(tokens[2]);
60                         continue;
61                 }
62         }
63         config_close(parser);
64
65
66         if (option_mask32 & OPT_m) {
67                 printf("%s \"Class %04x\" \"%04x\" \"%04x\" \"%04x\" \"%04x\"",
68                        pci_slot_name, pci_class, pci_vid, pci_did,
69                        pci_subsys_vid, pci_subsys_did);
70         } else {
71                 printf("%s Class %04x: %04x:%04x",
72                        pci_slot_name, pci_class, pci_vid, pci_did);
73         }
74
75         if ((option_mask32 & OPT_k) && driver) {
76                 if (option_mask32 & OPT_m) {
77                         printf(" \"%s\"", driver);
78                 } else {
79                         printf(" %s", driver);
80                 }
81         }
82         bb_putchar('\n');
83
84         free(driver);
85         free(pci_slot_name);
86
87         return TRUE;
88 }
89
90 int lspci_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
91 int lspci_main(int argc UNUSED_PARAM, char **argv)
92 {
93         getopt32(argv, "m" /*non-compat:*/ "k" /*ignored:*/ "nv");
94
95         recursive_action("/sys/bus/pci/devices",
96                         ACTION_RECURSE,
97                         fileAction,
98                         NULL, /* dirAction */
99                         NULL, /* userData */
100                         0 /* depth */);
101
102         return EXIT_SUCCESS;
103 }