Skip to content

Commit

Permalink
Use strtonum(3) for -t seconds, improve errors on invalid number input
Browse files Browse the repository at this point in the history
Base 10 suffices, negative numbers should be invalid (not converted) and
zero not treated specially.

This also unifies error messages and removes unnecessary EINVAL from them
since strtonum()'s errstr is explicit enough already.

Feedback and OK martjin, tb
  • Loading branch information
klemensn committed Dec 1, 2018
1 parent cedc02c commit 550f9df
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions usr.sbin/apmd/apmd.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: apmd.c,v 1.82 2018/11/30 18:05:31 tedu Exp $ */
/* $OpenBSD: apmd.c,v 1.83 2018/12/01 23:35:59 kn Exp $ */

/*
* Copyright (c) 1995, 1996 John T. Kohl
Expand Down Expand Up @@ -392,9 +392,10 @@ main(int argc, char *argv[])
sockname = optarg;
break;
case 't':
ts.tv_sec = strtoul(optarg, NULL, 0);
if (ts.tv_sec == 0)
usage();
ts.tv_sec = strtonum(optarg, 1, LLONG_MAX, &errstr);
if (errstr != NULL)
errx(1, "number of seconds is %s: %s", errstr,
optarg);
break;
case 's': /* status only */
statonly = 1;
Expand Down Expand Up @@ -422,14 +423,14 @@ main(int argc, char *argv[])
autoaction = AUTO_HIBERNATE;
autolimit = strtonum(optarg, 1, 100, &errstr);
if (errstr != NULL)
errc(1, EINVAL, "%s percentage: %s", errstr,
errx(1, "battery percentage is %s: %s", errstr,
optarg);
break;
case 'z':
autoaction = AUTO_SUSPEND;
autolimit = strtonum(optarg, 1, 100, &errstr);
if (errstr != NULL)
errc(1, EINVAL, "%s percentage: %s", errstr,
errx(1, "battery percentage is %s: %s", errstr,
optarg);
break;
case '?':
Expand Down

0 comments on commit 550f9df

Please sign in to comment.