[Midnightbsd-cvs] src: usr.bin/batt: Replace the batt command with a new implementation
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Sat Dec 6 00:08:39 EST 2008
Log Message:
-----------
Replace the batt command with a new implementation written in C. This version uses the sysctlbyname interface to collect data and runs much more quickly. Still requires ACPI battery information.
Add l, t and u flags to show the battery life as a percentage, time remaining, and number of batteries (units). The default is l and t.
Modified Files:
--------------
src/usr.bin/batt:
Makefile (r1.1 -> r1.2)
batt.1 (r1.1 -> r1.2)
Added Files:
-----------
src/usr.bin/batt:
batt.c (r1.1)
Removed Files:
-------------
src/usr.bin/batt:
batt.sh
-------------- next part --------------
--- /dev/null
+++ usr.bin/batt/batt.c
@@ -0,0 +1,95 @@
+/* $MidnightBSD: src/usr.bin/batt/batt.c,v 1.1 2008/12/06 05:08:38 laffer1 Exp $ */
+/*-
+ Copyright (c) 2008 Lucas Holt
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+*/
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+
+#include <stdio.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[]) {
+ int life, time, units;
+ size_t len;
+ int ch, lflag, tflag, uflag;
+
+ lflag = tflag = uflag = 0;
+ while ((ch = getopt(argc, argv, "ltu")) != -1) {
+ switch (ch) {
+ case 'l':
+ lflag = 1;
+ break;
+ case 't':
+ tflag = 1;
+ break;
+ case 'u':
+ uflag = 1;
+ break;
+ case '?': /* FALLS THROUGH */
+ default:
+ fprintf(stderr, "usage: %s [-ltf]\n", argv[0]);
+ exit(1);
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ /* if no flags are set, use the most likely */
+ if (lflag == tflag == uflag == 0)
+ lflag = tflag = 1;
+
+ if (lflag) {
+ len = sizeof(life);
+ if (sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0) < 0)
+ err(1, "ACPI not loaded or no battery found.");
+ printf("%d%% capacity\n", life);
+ }
+
+ if (tflag) {
+ len = sizeof(time);
+ if (sysctlbyname("hw.acpi.battery.time", &time, &len, NULL, 0) < 0)
+ err(1, "ACPI not loaded or no battery found.");
+ if (time < 1)
+ printf("Battery charging or drained.\n");
+ else if (time == 1)
+ printf("1 minute remaining\n");
+ else
+ printf("%d minutes remaining\n", time);
+ }
+
+ if (uflag) {
+ len = sizeof(units);
+ if (sysctlbyname("hw.acpi.battery.units", &units, &len, NULL, 0) < 0)
+ err(1, "ACPI not loaded or no battery found.");
+ if (units == 1)
+ printf("1 battery\n");
+ else
+ printf("%d batteries\n", units);
+ }
+
+ return 0;
+}
--- usr.bin/batt/batt.sh
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/bin/sh
-sysctl -a | grep hw.acpi.batt | cut -c 9-30
Index: batt.1
===================================================================
RCS file: /home/cvs/src/usr.bin/batt/batt.1,v
retrieving revision 1.1
retrieving revision 1.2
diff -L usr.bin/batt/batt.1 -L usr.bin/batt/batt.1 -u -r1.1 -r1.2
--- usr.bin/batt/batt.1
+++ usr.bin/batt/batt.1
@@ -24,18 +24,29 @@
.\"
.\" $MidnightBSD$
.\"
-.Dd August 16, 2008
+.Dd December 6, 2008
.Dt BATT 1
.Os
.Sh NAME
.Nm batt
.Nd "battery status and life"
+.Sh SYNOPSIS
+.Nm
+.Op Fl ltu
.Sh DESCRIPTION
The
.Nm
command prints the current status of batteries connected on
ACPI capable systems. This does not include CMOS batteries.
.Pp
+The following options are available:
+.Bl -tag -width indent
+.It Fl l
+List the battery life remaining as a percentage.
+.It Fl t
+Show the time remaining in minutes.
+.It Fl u
+Display the number of battery units.
.Sh HISTORY
The
.Nm
Index: Makefile
===================================================================
RCS file: /home/cvs/src/usr.bin/batt/Makefile,v
retrieving revision 1.1
retrieving revision 1.2
diff -L usr.bin/batt/Makefile -L usr.bin/batt/Makefile -u -r1.1 -r1.2
--- usr.bin/batt/Makefile
+++ usr.bin/batt/Makefile
@@ -1,6 +1,7 @@
# $MidnightBSD$
-SCRIPTS=batt.sh
+PROG= batt
+#SCRIPTS=batt.sh
MAN= batt.1
.include <bsd.prog.mk>
More information about the Midnightbsd-cvs
mailing list