From ae0a9eb524e73de4b6d8948c2ff5324ad0174d5f Mon Sep 17 00:00:00 2001 From: Alexander Kobel Date: Wed, 15 Apr 2020 12:49:10 +0200 Subject: Make optarg parsing more robust Allow -n MIN as equivalent to -nMIN and --min-value=MIN. --- brightnessctl.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'brightnessctl.c') diff --git a/brightnessctl.c b/brightnessctl.c index 6045ec3..a042fc8 100644 --- a/brightnessctl.c +++ b/brightnessctl.c @@ -121,9 +121,7 @@ int main(int argc, char **argv) { if (strcmp(name.sysname, "Linux")) fail("This program only supports Linux.\n"); p.exponent = 1; - while (1) { - if ((c = getopt_long(argc, argv, "lqpmn::e::srhVc:d:", options, NULL)) < 0) - break; + while ((c = getopt_long(argc, argv, "lqpmn::e::srhVc:d:", options, NULL)) >= 0) { switch (c) { case 'l': p.list = true; @@ -146,12 +144,16 @@ int main(int argc, char **argv) { case 'n': if (optarg) p.min = atol(optarg); + else if (NULL != argv[optind] && '-' != argv[optind][0]) + p.min = atol(argv[optind++]); else p.min = 1; break; case 'e': if (optarg) p.exponent = atof(optarg); + else if (NULL != argv[optind] && atof(argv[optind]) > 0.0) + p.exponent = atol(argv[optind++]); else p.exponent = 4; break; @@ -643,8 +645,8 @@ Options:\n\ -q, --quiet\t\t\tsuppress output.\n\ -p, --pretend\t\t\tdo not perform write operations.\n\ -m, --machine-readable\tproduce machine-readable output.\n\ - -n, --min-value\t\tset minimum brightness, defaults to 1.\n\ - -e, --exponent[=K]\t\tchanges percentage curve to exponential.\n\ + -n, --min-value[=MIN]\t\tset minimum brightness (to 1 if MIN is omitted).\n\ + -e, --exponent[=K]\t\tchanges percentage curve to exponential (to 4 if K is omitted).\n\ -s, --save\t\t\tsave previous state in a temporary file.\n\ -r, --restore\t\t\trestore previous saved state.\n\ -h, --help\t\t\tprint this help.\n\ @@ -665,4 +667,3 @@ Valid values:\n\ percentage delta\t\tExample: 50%%- or +10%%\n\ \n"); } - -- cgit v1.2.3