From cb3fafd0938b6b2b101704b201577122de34fb1a Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 11 Feb 2013 11:07:56 -0500 Subject: [PATCH] safe-strto-example.c: Add a pared-down argv-parsing example --- Makefile | 6 ++- safe-strto-example.c | 93 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 safe-strto-example.c diff --git a/Makefile b/Makefile index af2d15b..3a4d753 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ VERSION = 1 RELEASE = $(COURSE)-$(PACKAGE)-$(VERSION) RUN_PROGRAM = hello_world SCRIPTS = -C_PROGRAMS = hello_world +C_PROGRAMS = hello_world safe-strto-example CXX_PROGRAMS = goodbye_world PROGRAMS = $(C_PROGRAMS) $(CXX_PROGRAMS) @@ -64,6 +64,10 @@ TAR = tar # http://www.gnu.org/software/make/manual/html_node/Rules.html all: $(PROGRAMS) +# Dependency declarations +safe-strto-example_OBJECTS = safe-strto-example.o safe-strto.o +safe-strto-example.o safe-strto.o: config.h safe-strto.h + # target: help - display callable targets # Use `grep` to search this file for target comments help: diff --git a/safe-strto-example.c b/safe-strto-example.c new file mode 100644 index 0000000..0c1d4e9 --- /dev/null +++ b/safe-strto-example.c @@ -0,0 +1,93 @@ +/* +Demonstrate argument parsing with safe-strto.h + +Copyright (C) 2013 W. Trevor King + +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 +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include /* for exit(), EXIT_* */ +#include /* for stderr, *printf() */ +#include /* for getopt_long() */ +#include /* for the complex type qualifier */ +#include "config.h" /* enabling safe_strtoc() */ +#include "safe-strto.h" /* for safe_strtol() and safe_strtoc() */ + +static struct option long_options[] = { + {"my-long", required_argument, 0, 'l' }, + {"my-double", required_argument, 0, 'd' }, +#ifdef HAVE_COMPLEX + {"my-complex", required_argument, 0, 'c' }, +#endif /* HAVE_COMPLEX */ + {} +}; + +#ifdef HAVE_COMPLEX +static char *short_options = "l:d:c:"; +#else +static char *short_options = "l:d:"; +#endif /* HAVE_COMPLEX */ + +#ifdef HAVE_COMPLEX +void parse_args(int argc, char **argv, long *my_long, double *my_double, + double complex *my_complex) +#else +void parse_args(int argc, char **argv, long *my_long, double *my_double) +#endif /* HAVE_COMPLEX */ +{ + int ch; + + while (1) { + ch = getopt_long(argc, argv, short_options, long_options, NULL); + if (ch == -1) + break; + switch(ch) { + case 'l': + *my_long = safe_strtol(optarg, 10, "my-int"); + break; + case 'd': + *my_double = safe_strtod(optarg, "my-double"); + break; +#ifdef HAVE_COMPLEX + case 'c': + *my_complex = safe_strtoc(optarg, "my-complex"); + break; +#endif /* HAVE_COMPLEX */ + case '?': + exit(EXIT_FAILURE); + default: + printf("?? getopt returned character code 0%o ??\n", ch); + } + } + return; +} + +int main(int argc, char **argv) { + long int my_long = 0; + double my_double = 0; +#ifdef HAVE_COMPLEX + double complex my_complex = 0; + + parse_args(argc, argv, &my_long, &my_double, &my_complex); +#else + parse_args(argc, argv, &my_long, &my_double); +#endif /* HAVE_COMPLEX */ + printf("my_long: %ld\n", my_long); + printf("my_double: %g\n", my_double); +#ifdef HAVE_COMPLEX + printf("my_complex: %g + %gi\n", creal(my_complex), cimag(my_complex)); +#endif /* HAVE_COMPLEX */ + + return EXIT_SUCCESS; +} -- 2.26.2