Improve options and error handling.
Improve options handling and error out if multiple mutually exclusive options are passed to acpiconf. Switch from using atoi() to strtol() for argument parsing, and add error checking and handling, instead of blindly trusting that the integer conversion is OK. Cange err() to errx() in once case, the errno value was garbage there. Reviewed by: emaste Approved by: emaste Differential Revision: D13430
This commit is contained in:
parent
82e2d06a27
commit
fba6127915
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=326733
@ -92,7 +92,7 @@ acpi_battinfo(int num)
|
|||||||
uint32_t volt;
|
uint32_t volt;
|
||||||
|
|
||||||
if (num < 0 || num > 64)
|
if (num < 0 || num > 64)
|
||||||
err(EX_USAGE, "invalid battery %d", num);
|
errx(EX_USAGE, "invalid battery %d", num);
|
||||||
|
|
||||||
/* Print battery design information. */
|
/* Print battery design information. */
|
||||||
battio.unit = num;
|
battio.unit = num;
|
||||||
@ -205,8 +205,9 @@ usage(const char* prog)
|
|||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char *prog;
|
char *prog, *end;
|
||||||
int c, sleep_type;
|
int c, sleep_type, battery, ack;
|
||||||
|
int iflag = 0, kflag = 0, sflag = 0;
|
||||||
|
|
||||||
prog = argv[0];
|
prog = argv[0];
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
@ -218,16 +219,24 @@ main(int argc, char *argv[])
|
|||||||
while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
|
while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'i':
|
case 'i':
|
||||||
acpi_battinfo(atoi(optarg));
|
iflag = 1;
|
||||||
|
battery = strtol(optarg, &end, 10);
|
||||||
|
if ((size_t)(end - optarg) != strlen(optarg))
|
||||||
|
errx(EX_USAGE, "invalid battery");
|
||||||
break;
|
break;
|
||||||
case 'k':
|
case 'k':
|
||||||
acpi_sleep_ack(atoi(optarg));
|
kflag = 1;
|
||||||
|
ack = strtol(optarg, &end, 10);
|
||||||
|
if ((size_t)(end - optarg) != strlen(optarg))
|
||||||
|
errx(EX_USAGE, "invalid ack argument");
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
|
sflag = 1;
|
||||||
if (optarg[0] == 'S')
|
if (optarg[0] == 'S')
|
||||||
sleep_type = optarg[1] - '0';
|
optarg++;
|
||||||
else
|
sleep_type = strtol(optarg, &end, 10);
|
||||||
sleep_type = optarg[0] - '0';
|
if ((size_t)(end - optarg) != strlen(optarg))
|
||||||
|
errx(EX_USAGE, "invalid sleep type");
|
||||||
if (sleep_type < 1 || sleep_type > 4)
|
if (sleep_type < 1 || sleep_type > 4)
|
||||||
errx(EX_USAGE, "invalid sleep type (%d)",
|
errx(EX_USAGE, "invalid sleep type (%d)",
|
||||||
sleep_type);
|
sleep_type);
|
||||||
@ -241,7 +250,25 @@ main(int argc, char *argv[])
|
|||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
if (sleep_type != -1)
|
if (iflag != 0 && kflag != 0 && sflag != 0)
|
||||||
|
errx(EX_USAGE, "-i, -k and -s are mutually exclusive");
|
||||||
|
|
||||||
|
if (iflag != 0) {
|
||||||
|
if (kflag != 0)
|
||||||
|
errx(EX_USAGE, "-i and -k are mutually exclusive");
|
||||||
|
if (sflag != 0)
|
||||||
|
errx(EX_USAGE, "-i and -s are mutually exclusive");
|
||||||
|
acpi_battinfo(battery);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (kflag != 0) {
|
||||||
|
if (sflag != 0)
|
||||||
|
errx(EX_USAGE, "-k and -s are mutually exclusive");
|
||||||
|
acpi_sleep_ack(ack);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (sflag != 0)
|
||||||
acpi_sleep(sleep_type);
|
acpi_sleep(sleep_type);
|
||||||
|
|
||||||
close(acpifd);
|
close(acpifd);
|
||||||
|
Loading…
Reference in New Issue
Block a user