From c5509750f2f6ceb6b0744ad9ddcfbc989e815932 Mon Sep 17 00:00:00 2001 From: jsing <> Date: Wed, 27 Aug 2014 15:51:41 +0000 Subject: Add an option type that handles argument to integer conversion. --- src/usr.bin/openssl/apps.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/usr.bin/openssl/apps.c') diff --git a/src/usr.bin/openssl/apps.c b/src/usr.bin/openssl/apps.c index e5eda3f53b..fc5e2d073a 100644 --- a/src/usr.bin/openssl/apps.c +++ b/src/usr.bin/openssl/apps.c @@ -1,4 +1,4 @@ -/* $OpenBSD: apps.c,v 1.2 2014/08/27 14:59:44 jsing Exp $ */ +/* $OpenBSD: apps.c,v 1.3 2014/08/27 15:51:41 jsing Exp $ */ /* * Copyright (c) 2014 Joel Sing * @@ -2253,7 +2253,9 @@ options_usage(struct option *opts) int options_parse(int argc, char **argv, struct option *opts, char **unnamed) { + const char *errstr; struct option *opt; + long long val; char *arg, *p; int i, j; @@ -2274,17 +2276,32 @@ options_parse(int argc, char **argv, struct option *opts, char **unnamed) if (strcmp(p, opt->name) != 0) continue; - switch (opt->type) { - case OPTION_ARG: + if (opt->type == OPTION_ARG || + opt->type == OPTION_ARG_INT) { if (++i >= argc) { fprintf(stderr, "missing %s argument for -%s\n", opt->argname, opt->name); return (1); } + } + + switch (opt->type) { + case OPTION_ARG: *opt->opt.arg = argv[i]; break; + case OPTION_ARG_INT: + val = strtonum(argv[i], 0, INT_MAX, &errstr); + if (errstr != NULL) { + fprintf(stderr, + "%s %s argument for -%s\n", + errstr, opt->argname, opt->name); + return (1); + } + *opt->opt.value = (int)val; + break; + case OPTION_FLAG: *opt->opt.flag = 1; break; -- cgit v1.2.3-55-g6feb