Userland: Implement simple lspci command

This commit is contained in:
Conrad Pankoff 2019-08-14 01:32:22 +10:00 committed by Andreas Kling
parent 6fe18a0417
commit 99ed4ce30c
Notes: sideshowbarker 2024-07-19 12:41:40 +09:00
2 changed files with 39 additions and 1 deletions

View File

@ -268,7 +268,7 @@ Optional<KBuffer> procfs$pci(InodeIdentifier)
obj.set("class", PCI::get_class(address));
obj.set("subsystem_id", PCI::get_subsystem_id(address));
obj.set("subsystem_vendor_id", PCI::get_subsystem_vendor_id(address));
json.append(obj);
json.append(move(obj));
});
return json.serialized<KBufferBuilder>();
}

38
Userland/lspci.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <AK/AKString.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <LibCore/CFile.h>
#include <stdio.h>
int main(int argc, char** argv)
{
UNUSED_PARAM(argc);
UNUSED_PARAM(argv);
CFile file("/proc/pci");
if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error: %s\n", file.error_string());
return 1;
}
auto file_contents = file.read_all();
auto json = JsonValue::from_string(file_contents).as_array();
json.for_each([](auto& value) {
auto dev = value.as_object();
auto bus = dev.get("bus").to_u32();
auto slot = dev.get("slot").to_u32();
auto function = dev.get("function").to_u32();
auto vendor_id = dev.get("vendor_id").to_u32();
auto device_id = dev.get("device_id").to_u32();
auto revision_id = dev.get("revision_id").to_u32();
auto class_id = dev.get("class").to_u32();
printf("%02x:%02x.%d %04x: %04x:%04x (rev %02x)\n",
bus, slot, function,
class_id, vendor_id, device_id, revision_id
);
});
return 0;
}