rendered paste bodystatic char rcsid[] = "$Id$";/* * Use the pciutils library to lookup a vendor id/device * * $Log$ */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <stdarg.h>#include <sys/types.h>#include <sys/stat.h>#include <pci/pci.h> // From pciutils/lib#include <argp.h>#include <syslog.h>int verbose = 0;/* Communication with libpci */static struct pci_access *pacc;static struct pci_filter filter;const char *argp_program_version = rcsid;const char *argp_program_bug_address = "<wynn@forums.gentoo.org>";/* Program documentation. */static char doc[] ="Use the pciutils library to lookup a vendor id/device";/* A description of the arguments we accept. */static char args_doc[] = "PCITABLE"; /* The options we understand. */static struct argp_option options[] = { /* name, key, arg, flags, doc, group */ {"verbose", 'v', 0, 0, "Produce verbose output", 0 }, {"pciids", 'i', "PCIIDS", 0, "pci.ids file (default /usr/share/misc/pci.ids)", 0 }, { 0, 0, 0, 0, 0, 0 }}; /* Used by `main' to communicate with `parse_opt'. */struct arguments { char *args[1]; /* PCITABLE */ char *pci_ids; int verbose;}; /* Parse a single option. */static error_tparse_opt (int key, char *arg, struct argp_state *state){ /* * Get the INPUT argument from `argp_parse', which we know is a * pointer to our arguments structure. */ struct arguments *arguments = state->input; switch (key) { case 'v': arguments->verbose = 1; break; case 'i': arguments->pci_ids = arg; printf("Setting pci_ids to %s\n",arg); break; case ARGP_KEY_ARG: if (state->arg_num >= 1) /* Too many arguments. */ argp_usage(state); arguments->args[state->arg_num] = arg; printf("Setting pci table to %s\n",arg); break; case ARGP_KEY_END: if (state->arg_num < 1) { arguments->args[state->arg_num] = NULL; fprintf(stderr, "The pci table must be given.\n"); argp_usage(state); } break; default: return ARGP_ERR_UNKNOWN; } return 0;}/* Our argp parser. */static struct argp argp = { /* options, parser, args_doc, doc, children, help_filter, argp_domain */ options, parse_opt, args_doc, doc, 0, 0, 0 };void die(char *msg, ...){ va_list args; va_start(args, msg); fputs("lspci: ", stderr); vfprintf(stderr, msg, args); fputc('\n', stderr); exit(1);}static voidshow_info(u_short vendor_id, u_short device_id){ u_char classbuf[128], devbuf[128]; printf("%s\n", pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, vendor_id, device_id));}static voidread_table(char *filename){ struct stat buf; FILE *fp; int begin = 0; char *line = NULL; size_t len = 0; u_long id; char *tail; u_short vendor_id, device_id; if (stat(filename, &buf)) { printf("Can't stat %s: %m", filename); return; } if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "Can't open %s: %m", filename); return; } while (1) { ssize_t n = getline(&line, &len, fp); if (n <= 0) /* End of file or error. */ break; line[n - 1] = '\0'; if (strncmp(line, "static ", 7) == 0) break; } while (1) { ssize_t n = getline(&line, &len, fp); char *p, *q; if (n <= 0) /* End of file or error. */ break; line[n - 1] = '\0'; if (strstr(line, "{ 0 }") != NULL) break; // printf("read_table: line '%s'\n", line); if ((p=strstr(line, "0x")) != NULL) { id = strtoul(p, &tail, 0); vendor_id = (u_short) id; // printf("read_table: id %04lx, p '%s', tail '%s'\n", id, p, tail); } else { fprintf(stderr, "Can't find vendor id in '%s'\n", line); } if ((q=strchr(p, ',')) != NULL) { id = strtoul((q+1), &tail, 0); device_id = (u_short) id; // printf("read_table: id %04lx, q '%s', tail '%s'\n", id, q, tail); } else { fprintf(stderr, "Can't find device id in '%s'\n", p); } show_info(vendor_id, device_id); } fclose(fp);}intmain (int argc, char **argv){ struct arguments arguments; u_short vendor_id = 0x10b7, device_id = 0x1700; /* Default values. */ arguments.verbose = 0; arguments.pci_ids = "/usr/share/misc/pci.ids"; /* * Parse our arguments; every option seen by `parse_opt' will be * reflected in `arguments'. */ argp_parse (&argp, argc, argv, 0, 0, &arguments); printf("PCITABLE = %s\nVERBOSE = %s\nPCI IDS = %s\n", arguments.args[0]?arguments.args[0]:"NONE", arguments.verbose ? "yes" : "no", arguments.pci_ids?arguments.pci_ids:"NONE" ); if (arguments.args[0] == NULL) return(1); pacc = pci_alloc(); pacc->error = die; pci_filter_init(pacc, &filter); pci_init(pacc); read_table(arguments.args[0]); pci_cleanup(pacc); return(0);}/*Local Variables:compile-command: "gcc -O2 -g pci-lookup.c -o pci-lookup -lpci"End:*/