diff options
author | Jean Delvare <jdelvare@suse.de> | 2005-02-10 20:57:13 +0000 |
---|---|---|
committer | Jean Delvare <jdelvare@suse.de> | 2005-02-10 20:57:13 +0000 |
commit | 2377410549c638dccd7a689f52c353798fe922e7 (patch) | |
tree | a79e313e9ad5f36059133fe54e970b8e7a108a61 /ownership.c | |
parent | 204ebe77b4dbde5a3e8dd3917c2f7ba6f2597c11 (diff) | |
download | dmidecode-git-2377410549c638dccd7a689f52c353798fe922e7.tar.gz |
Copy command line-handling from dmidecode.c.
Diffstat (limited to 'ownership.c')
-rw-r--r-- | ownership.c | 91 |
1 files changed, 82 insertions, 9 deletions
diff --git a/ownership.c b/ownership.c index 9aeab1c..faa02b0 100644 --- a/ownership.c +++ b/ownership.c @@ -1,7 +1,7 @@ /* * Compaq Ownership Tag * - * (C) 2003-2004 Jean Delvare <khali@linux-fr.org> + * (C) 2003-2005 Jean Delvare <khali@linux-fr.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,11 +32,25 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> +#include <getopt.h> +#include "version.h" #include "config.h" #include "types.h" #include "util.h" +/* Options are global */ +struct opt +{ + const char* devmem; + unsigned int flags; +}; +static struct opt opt; + +#define FLAG_VERSION (1<<0) +#define FLAG_HELP (1<<1) + static void ownership(u32 base, const char *pname, const char *devmem) { u8 *buf; @@ -94,23 +108,82 @@ static u32 decode(const u8 *p) return 0; } -int main(int argc, const char *argv[]) +/* Return -1 on error, 0 on success */ +static int parse_command_line(int argc, char * const argv[]) +{ + int option; + const char *optstring = "d:hV"; + struct option longopts[]={ + { "dev-mem", required_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { 0, 0, 0, 0 } + }; + + while((option=getopt_long(argc, argv, optstring, longopts, NULL))!=-1) + switch(option) + { + case 'd': + opt.devmem=optarg; + break; + case 'h': + opt.flags|=FLAG_HELP; + break; + case 'V': + opt.flags|=FLAG_VERSION; + break; + case ':': + case '?': + return -1; + } + + return 0; +} + +static void print_help(void) +{ + static const char *help= + "Usage: ownership [OPTIONS]\n" + "Options are:\n" + " -d, --dev-mem FILE Read memory from device FILE (default: " DEFAULT_MEM_DEV ")\n" + " -h, --help Display this help text and exit\n" + " -V, --version Display the version and exit\n"; + + printf("%s", help); +} + +int main(int argc, char * const argv[]) { u8 *buf; off_t fp; - const char *devmem=DEFAULT_MEM_DEV; int ok=0; if(sizeof(u8)!=1 || sizeof(u32)!=4) { - fprintf(stderr,"%s: compiler incompatibility\n", argv[0]); + fprintf(stderr, "%s: compiler incompatibility\n", argv[0]); exit(255); } - - if(argc>=2) - devmem=argv[1]; - if((buf=mem_chunk(0xE0000, 0x20000, devmem))==NULL) + /* Set default option values */ + opt.devmem=DEFAULT_MEM_DEV; + opt.flags=0; + + if(parse_command_line(argc, argv)<0) + exit(2); + + if(opt.flags & FLAG_HELP) + { + print_help(); + return 0; + } + + if(opt.flags & FLAG_VERSION) + { + printf("%s\n", VERSION); + return 0; + } + + if((buf=mem_chunk(0xE0000, 0x20000, opt.devmem))==NULL) exit(1); for(fp=0; !ok && fp<=0x1FFF0; fp+=16) @@ -128,7 +201,7 @@ int main(int argc, const char *argv[]) if((base=decode(p))) { ok=1; - ownership(base, argv[0], devmem); + ownership(base, argv[0], opt.devmem); } } } |