Skip to content

Commit

Permalink
Add error checking to the exponent arg
Browse files Browse the repository at this point in the history
Fixes: #84
  • Loading branch information
Hummer12007 committed Feb 4, 2024
1 parent 93c8ed1 commit 912498a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions brightnessctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum operation;

static void fail(char *, ...);
static void usage(void);
static float parse_exponent(const char *);
#define cat_with(...) _cat_with(__VA_ARGS__, NULL)
static char *_cat_with(char, ...);
static char *dir_child(char *, char*);
Expand Down Expand Up @@ -174,9 +175,9 @@ int main(int argc, char **argv) {
break;
case 'e':
if (optarg)
p.exponent = atof(optarg);
else if (NULL != argv[optind] && atof(argv[optind]) > 0.0)
p.exponent = atof(argv[optind++]);
p.exponent = parse_exponent(optarg);
else if (NULL != argv[optind] && parse_exponent(argv[optind]) > 0.0)
p.exponent = parse_exponent(argv[optind++]);
else
p.exponent = 4;
break;
Expand Down Expand Up @@ -673,6 +674,14 @@ char *class_path(char *class) {
return dir_child(path, class);
}

static float parse_exponent(const char *str) {
char *endptr = NULL;
double ret;
if ((ret = strtod(str, &endptr)) == 0)
fail("Invalid exponent provided: %s\n", str);
return ret;
}

void fail(char *err_msg, ...) {
va_list va;
va_start(va, err_msg);
Expand Down

0 comments on commit 912498a

Please sign in to comment.