diff options
author | Luiz Fernando Capitulino <lcapitulino@mandriva.com> | 2009-02-05 14:41:07 +0000 |
---|---|---|
committer | Luiz Fernando Capitulino <lcapitulino@mandriva.com> | 2009-02-05 14:41:07 +0000 |
commit | be551918c972ed63fb7a018356212aea271cdbac (patch) | |
tree | 6685f12ead6f456e3618e3404d91ed80e6c7a4e1 /lspcidrake.c | |
parent | 5f65fad1a9a17fdc588ab9eadb791b15ac4452e3 (diff) | |
download | ldetect-be551918c972ed63fb7a018356212aea271cdbac.tar ldetect-be551918c972ed63fb7a018356212aea271cdbac.tar.gz ldetect-be551918c972ed63fb7a018356212aea271cdbac.tar.bz2 ldetect-be551918c972ed63fb7a018356212aea271cdbac.tar.xz ldetect-be551918c972ed63fb7a018356212aea271cdbac.zip |
- lspcidrake: use getopt to parse command-line instead of playing
with pointers
Diffstat (limited to 'lspcidrake.c')
-rw-r--r-- | lspcidrake.c | 61 |
1 files changed, 37 insertions, 24 deletions
diff --git a/lspcidrake.c b/lspcidrake.c index 2e32f51..6883463 100644 --- a/lspcidrake.c +++ b/lspcidrake.c @@ -1,6 +1,7 @@ #include <stdio.h> #include <string.h> #include <unistd.h> +#include <getopt.h> #include "libldetect.h" static int verboze = 0; @@ -50,33 +51,45 @@ static void print_dmi_entries(struct dmi_entries entries) { printf("%-16s: %s\n", entries.entries[i].module, entries.entries[i].constraints); } +static void usage(void) +{ + printf( + "usage: lspcidrake [options]\n" + "\t-p, --pci-file <file>\tPCI devices source [/proc/bus/pci/devices by default]\n" + "\t-u, --usb-file <file>\tUSB devices source [/proc/bus/usb/devices by default]\n" + "\t-v, --verbose\t\tVerbose mode [print ids and sub-ids], implies full probe\n" + "\t-d, --dmidecode <file>\tTo use this dmidecode output instead of calling demicode\n"); +} + int main(int argc, char **argv) { - char ** ptr = argv; - int fake = 0; + int opt, fake = 0; + struct option options[] = { { "verbose", 0, NULL, 'v' }, + { "pci-file", 1, NULL, 'p' }, + { "usb-file", 1, NULL, 'u' }, + { "dmidecode", 0, NULL, 'd' }, + { NULL, 0, NULL, 0 } }; - while (ptr && *ptr) { - if (!strcmp(*ptr, "-h") || !strcmp(*ptr, "--help")) { - printf("usage: lspcidrake [-v|-u]\n" - "-p <file>: pci devices source [/proc/bus/pci/devices by default]\n" - "-u <file>: usb devices source [/proc/bus/usb/devices by default]\n" - "-v : verbose mode [print ids and sub-ids], implies full probe\n" - "--dmidecode <file>: to use this dmidecode output instead of calling dmidecode\n" - ); - return 0; - } - if (!strcmp(*ptr, "-v")) - verboze = 1; - else if (!strcmp(*ptr, "-u")) { - proc_usb_path = *++ptr; - fake = 1; - } else if (!strcmp(*ptr, "-p")) { - proc_pci_path = *++ptr; - fake = 1; - } else if (!strcmp(*ptr, "--dmidecode")) { - dmidecode_file = *++ptr; - fake = 1; + while ((opt = getopt_long(argc, argv, "vp:u:d", options, NULL)) != -1) { + switch (opt) { + case 'v': + verboze = 1; + break; + case 'p': + proc_pci_path = optarg; + fake = 1; + break; + case 'u': + proc_usb_path = optarg; + fake = 1; + break; + case 'd': + dmidecode_file = optarg; + fake = 1; + break; + default: + usage(); + return 1; } - ptr++; } if (!fake || proc_pci_path) printit(pci_probe(), print_pci_class); |