From owner-svn-src-head@freebsd.org Sat Dec 9 15:59:11 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 73BE9E8F888; Sat, 9 Dec 2017 15:59:11 +0000 (UTC) (envelope-from zeising@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4D7246A5FF; Sat, 9 Dec 2017 15:59:11 +0000 (UTC) (envelope-from zeising@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id vB9FxA1N001256; Sat, 9 Dec 2017 15:59:10 GMT (envelope-from zeising@FreeBSD.org) Received: (from zeising@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id vB9FxAv9001255; Sat, 9 Dec 2017 15:59:10 GMT (envelope-from zeising@FreeBSD.org) Message-Id: <201712091559.vB9FxAv9001255@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: zeising set sender to zeising@FreeBSD.org using -f From: Niclas Zeising Date: Sat, 9 Dec 2017 15:59:10 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r326733 - head/usr.sbin/acpi/acpiconf X-SVN-Group: head X-SVN-Commit-Author: zeising X-SVN-Commit-Paths: head/usr.sbin/acpi/acpiconf X-SVN-Commit-Revision: 326733 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Dec 2017 15:59:11 -0000 Author: zeising (doc,ports committer) Date: Sat Dec 9 15:59:10 2017 New Revision: 326733 URL: https://svnweb.freebsd.org/changeset/base/326733 Log: 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 Modified: head/usr.sbin/acpi/acpiconf/acpiconf.c Modified: head/usr.sbin/acpi/acpiconf/acpiconf.c ============================================================================== --- head/usr.sbin/acpi/acpiconf/acpiconf.c Sat Dec 9 15:47:26 2017 (r326732) +++ head/usr.sbin/acpi/acpiconf/acpiconf.c Sat Dec 9 15:59:10 2017 (r326733) @@ -92,7 +92,7 @@ acpi_battinfo(int num) uint32_t volt; if (num < 0 || num > 64) - err(EX_USAGE, "invalid battery %d", num); + errx(EX_USAGE, "invalid battery %d", num); /* Print battery design information. */ battio.unit = num; @@ -205,8 +205,9 @@ usage(const char* prog) int main(int argc, char *argv[]) { - char *prog; - int c, sleep_type; + char *prog, *end; + int c, sleep_type, battery, ack; + int iflag = 0, kflag = 0, sflag = 0; prog = argv[0]; if (argc < 2) @@ -218,16 +219,24 @@ main(int argc, char *argv[]) while ((c = getopt(argc, argv, "hi:k:s:")) != -1) { switch (c) { 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; 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; case 's': + sflag = 1; if (optarg[0] == 'S') - sleep_type = optarg[1] - '0'; - else - sleep_type = optarg[0] - '0'; + optarg++; + sleep_type = strtol(optarg, &end, 10); + if ((size_t)(end - optarg) != strlen(optarg)) + errx(EX_USAGE, "invalid sleep type"); if (sleep_type < 1 || sleep_type > 4) errx(EX_USAGE, "invalid sleep type (%d)", sleep_type); @@ -241,7 +250,25 @@ main(int argc, char *argv[]) argc -= 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); close(acpifd);