Check for >= 255 since sign extension from byte to u_int sometimes makes
the value for "unknown" 0xffffffff. The underlying kernel drivers should be updated to only return 255 but the ABI is used by too many userland utilities. Also, make this WARNS 6 compatible.
This commit is contained in:
parent
e8b4d56eb5
commit
a607c5e4cb
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=129784
@ -4,5 +4,6 @@ PROG= apm
|
|||||||
MAN= apm.8
|
MAN= apm.8
|
||||||
MLINKS= apm.8 apmconf.8
|
MLINKS= apm.8 apmconf.8
|
||||||
MANSUBDIR= /i386
|
MANSUBDIR= /i386
|
||||||
|
WARNS?= 6
|
||||||
|
|
||||||
.include <bsd.prog.mk>
|
.include <bsd.prog.mk>
|
||||||
|
@ -40,8 +40,8 @@ __FBSDID("$FreeBSD$");
|
|||||||
|
|
||||||
int cmos_wall = 0; /* True when wall time is in cmos clock, else UTC */
|
int cmos_wall = 0; /* True when wall time is in cmos clock, else UTC */
|
||||||
|
|
||||||
void
|
static void
|
||||||
usage()
|
usage(void)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s\n%s\n",
|
fprintf(stderr, "%s\n%s\n",
|
||||||
"usage: apm [-ablstzZ] [-d enable ] [ -e enable ] "
|
"usage: apm [-ablstzZ] [-d enable ] [ -e enable ] "
|
||||||
@ -54,7 +54,7 @@ usage()
|
|||||||
* Return 1 for boolean true, and 0 for false, according to the
|
* Return 1 for boolean true, and 0 for false, according to the
|
||||||
* interpretation of the string argument given.
|
* interpretation of the string argument given.
|
||||||
*/
|
*/
|
||||||
int
|
static int
|
||||||
is_true(const char *boolean) {
|
is_true(const char *boolean) {
|
||||||
char *endp;
|
char *endp;
|
||||||
long val;
|
long val;
|
||||||
@ -74,9 +74,11 @@ is_true(const char *boolean) {
|
|||||||
warnx("invalid boolean argument \"%s\"", boolean);
|
warnx("invalid boolean argument \"%s\"", boolean);
|
||||||
usage();
|
usage();
|
||||||
/* NOTREACHED */
|
/* NOTREACHED */
|
||||||
|
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
static int
|
||||||
int2bcd(int i)
|
int2bcd(int i)
|
||||||
{
|
{
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
@ -93,7 +95,7 @@ int2bcd(int i)
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
static int
|
||||||
bcd2int(int bcd)
|
bcd2int(int bcd)
|
||||||
{
|
{
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
@ -110,28 +112,28 @@ bcd2int(int bcd)
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
apm_suspend(int fd)
|
apm_suspend(int fd)
|
||||||
{
|
{
|
||||||
if (ioctl(fd, APMIO_SUSPEND, NULL) == -1)
|
if (ioctl(fd, APMIO_SUSPEND, NULL) == -1)
|
||||||
err(1, "ioctl(APMIO_SUSPEND)");
|
err(1, "ioctl(APMIO_SUSPEND)");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
apm_standby(int fd)
|
apm_standby(int fd)
|
||||||
{
|
{
|
||||||
if (ioctl(fd, APMIO_STANDBY, NULL) == -1)
|
if (ioctl(fd, APMIO_STANDBY, NULL) == -1)
|
||||||
err(1, "ioctl(APMIO_STANDBY)");
|
err(1, "ioctl(APMIO_STANDBY)");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
apm_getinfo(int fd, apm_info_t aip)
|
apm_getinfo(int fd, apm_info_t aip)
|
||||||
{
|
{
|
||||||
if (ioctl(fd, APMIO_GETINFO, aip) == -1)
|
if (ioctl(fd, APMIO_GETINFO, aip) == -1)
|
||||||
err(1, "ioctl(APMIO_GETINFO)");
|
err(1, "ioctl(APMIO_GETINFO)");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
apm_enable(int fd, int enable) {
|
apm_enable(int fd, int enable) {
|
||||||
|
|
||||||
if (enable) {
|
if (enable) {
|
||||||
@ -143,36 +145,34 @@ apm_enable(int fd, int enable) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
print_all_info(int fd, apm_info_t aip, int bioscall_available)
|
print_all_info(int fd, apm_info_t aip, int bioscall_available)
|
||||||
{
|
{
|
||||||
struct apm_bios_arg args;
|
struct apm_bios_arg args;
|
||||||
int apmerr;
|
int apmerr;
|
||||||
|
const char *batt_msg[] = { "high", "low", "critical", "charging" };
|
||||||
|
const char *line_msg[] = { "off-line", "on-line" };
|
||||||
|
|
||||||
printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
|
printf("APM version: %d.%d\n", aip->ai_major, aip->ai_minor);
|
||||||
printf("APM Management: %s\n", (aip->ai_status ? "Enabled" : "Disabled"));
|
printf("APM Management: %s\n", aip->ai_status ? "Enabled" : "Disabled");
|
||||||
printf("AC Line status: ");
|
printf("AC Line status: ");
|
||||||
if (aip->ai_acline == 255)
|
if (aip->ai_acline >= 255)
|
||||||
printf("unknown");
|
printf("unknown");
|
||||||
else if (aip->ai_acline > 1)
|
else if (aip->ai_acline > 1)
|
||||||
printf("invalid value (0x%x)", aip->ai_acline);
|
printf("invalid value (0x%x)", aip->ai_acline);
|
||||||
else {
|
else
|
||||||
char *messages[] = { "off-line", "on-line" };
|
printf("%s", line_msg[aip->ai_acline]);
|
||||||
printf("%s", messages[aip->ai_acline]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("Battery status: ");
|
printf("Battery status: ");
|
||||||
if (aip->ai_batt_stat == 255)
|
if (aip->ai_batt_stat >= 255)
|
||||||
printf("unknown");
|
printf("unknown");
|
||||||
else if (aip->ai_batt_stat > 3)
|
else if (aip->ai_batt_stat > 3)
|
||||||
printf("invalid value (0x%x)", aip->ai_batt_stat);
|
printf("invalid value (0x%x)", aip->ai_batt_stat);
|
||||||
else {
|
else
|
||||||
char *messages[] = { "high", "low", "critical", "charging" };
|
printf("%s", batt_msg[aip->ai_batt_stat]);
|
||||||
printf("%s", messages[aip->ai_batt_stat]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("Remaining battery life: ");
|
printf("Remaining battery life: ");
|
||||||
if (aip->ai_batt_life == 255)
|
if (aip->ai_batt_life >= 255)
|
||||||
printf("unknown\n");
|
printf("unknown\n");
|
||||||
else if (aip->ai_batt_life <= 100)
|
else if (aip->ai_batt_life <= 100)
|
||||||
printf("%d%%\n", aip->ai_batt_life);
|
printf("%d%%\n", aip->ai_batt_life);
|
||||||
@ -194,10 +194,10 @@ print_all_info(int fd, apm_info_t aip, int bioscall_available)
|
|||||||
}
|
}
|
||||||
if (aip->ai_infoversion >= 1) {
|
if (aip->ai_infoversion >= 1) {
|
||||||
printf("Number of batteries: ");
|
printf("Number of batteries: ");
|
||||||
if (aip->ai_batteries == (u_int) -1)
|
if (aip->ai_batteries >= 255)
|
||||||
printf("unknown\n");
|
printf("unknown\n");
|
||||||
else {
|
else {
|
||||||
int i;
|
u_int i;
|
||||||
struct apm_pwstatus aps;
|
struct apm_pwstatus aps;
|
||||||
|
|
||||||
printf("%d\n", aip->ai_batteries);
|
printf("%d\n", aip->ai_batteries);
|
||||||
@ -208,26 +208,21 @@ print_all_info(int fd, apm_info_t aip, int bioscall_available)
|
|||||||
continue;
|
continue;
|
||||||
printf("Battery %d:\n", i);
|
printf("Battery %d:\n", i);
|
||||||
printf("\tBattery status: ");
|
printf("\tBattery status: ");
|
||||||
if (aps.ap_batt_flag != 255 &&
|
if (aps.ap_batt_flag <= 255 &&
|
||||||
(aps.ap_batt_flag & APM_BATT_NOT_PRESENT)) {
|
(aps.ap_batt_flag & APM_BATT_NOT_PRESENT)) {
|
||||||
printf("not present\n");
|
printf("not present\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (aps.ap_batt_stat == 255)
|
if (aps.ap_batt_stat >= 255)
|
||||||
printf("unknown\n");
|
printf("unknown\n");
|
||||||
else if (aps.ap_batt_stat > 3)
|
else if (aps.ap_batt_stat > 3)
|
||||||
printf("invalid value (0x%x)\n",
|
printf("invalid value (0x%x)\n",
|
||||||
aps.ap_batt_stat);
|
aps.ap_batt_stat);
|
||||||
else {
|
else
|
||||||
char *messages[] = { "high",
|
|
||||||
"low",
|
|
||||||
"critical",
|
|
||||||
"charging" };
|
|
||||||
printf("%s\n",
|
printf("%s\n",
|
||||||
messages[aps.ap_batt_stat]);
|
batt_msg[aps.ap_batt_stat]);
|
||||||
}
|
|
||||||
printf("\tRemaining battery life: ");
|
printf("\tRemaining battery life: ");
|
||||||
if (aps.ap_batt_life == 255)
|
if (aps.ap_batt_life >= 255)
|
||||||
printf("unknown\n");
|
printf("unknown\n");
|
||||||
else if (aps.ap_batt_life <= 100)
|
else if (aps.ap_batt_life <= 100)
|
||||||
printf("%d%%\n", aps.ap_batt_life);
|
printf("%d%%\n", aps.ap_batt_life);
|
||||||
@ -343,14 +338,14 @@ print_all_info(int fd, apm_info_t aip, int bioscall_available)
|
|||||||
* currently, it can turn off the display, but the display never comes
|
* currently, it can turn off the display, but the display never comes
|
||||||
* back until the machine suspend/resumes :-).
|
* back until the machine suspend/resumes :-).
|
||||||
*/
|
*/
|
||||||
void
|
static void
|
||||||
apm_display(int fd, int newstate)
|
apm_display(int fd, int newstate)
|
||||||
{
|
{
|
||||||
if (ioctl(fd, APMIO_DISPLAY, &newstate) == -1)
|
if (ioctl(fd, APMIO_DISPLAY, &newstate) == -1)
|
||||||
err(1, "ioctl(APMIO_DISPLAY)");
|
err(1, "ioctl(APMIO_DISPLAY)");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
apm_haltcpu(int fd, int enable) {
|
apm_haltcpu(int fd, int enable) {
|
||||||
|
|
||||||
if (enable) {
|
if (enable) {
|
||||||
@ -362,7 +357,7 @@ apm_haltcpu(int fd, int enable) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
apm_set_timer(int fd, int delta)
|
apm_set_timer(int fd, int delta)
|
||||||
{
|
{
|
||||||
time_t tmr;
|
time_t tmr;
|
||||||
@ -394,7 +389,7 @@ int
|
|||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int c, fd;
|
int c, fd;
|
||||||
int sleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
|
int dosleep = 0, all_info = 1, apm_status = 0, batt_status = 0;
|
||||||
int display = -1, batt_life = 0, ac_status = 0, standby = 0;
|
int display = -1, batt_life = 0, ac_status = 0, standby = 0;
|
||||||
int batt_time = 0, delta = 0, enable = -1, haltcpu = -1;
|
int batt_time = 0, delta = 0, enable = -1, haltcpu = -1;
|
||||||
char *cmdname;
|
char *cmdname;
|
||||||
@ -410,7 +405,7 @@ main(int argc, char *argv[])
|
|||||||
cmdname = argv[0];
|
cmdname = argv[0];
|
||||||
|
|
||||||
if (strcmp(cmdname, "zzz") == 0) {
|
if (strcmp(cmdname, "zzz") == 0) {
|
||||||
sleep = 1;
|
dosleep = 1;
|
||||||
all_info = 0;
|
all_info = 0;
|
||||||
goto finish_option;
|
goto finish_option;
|
||||||
}
|
}
|
||||||
@ -455,7 +450,7 @@ main(int argc, char *argv[])
|
|||||||
all_info = 0;
|
all_info = 0;
|
||||||
break;
|
break;
|
||||||
case 'z':
|
case 'z':
|
||||||
sleep = 1;
|
dosleep = 1;
|
||||||
all_info = 0;
|
all_info = 0;
|
||||||
break;
|
break;
|
||||||
case 'Z':
|
case 'Z':
|
||||||
@ -470,7 +465,8 @@ main(int argc, char *argv[])
|
|||||||
argv += optind;
|
argv += optind;
|
||||||
}
|
}
|
||||||
finish_option:
|
finish_option:
|
||||||
if (haltcpu != -1 || enable != -1 || display != -1 || delta || sleep || standby) {
|
if (haltcpu != -1 || enable != -1 || display != -1 || delta || dosleep
|
||||||
|
|| standby) {
|
||||||
fd = open(APMDEV, O_RDWR);
|
fd = open(APMDEV, O_RDWR);
|
||||||
bioscall_available = 1;
|
bioscall_available = 1;
|
||||||
} else if ((fd = open(APMDEV, O_RDWR)) >= 0)
|
} else if ((fd = open(APMDEV, O_RDWR)) >= 0)
|
||||||
@ -485,7 +481,7 @@ main(int argc, char *argv[])
|
|||||||
apm_haltcpu(fd, haltcpu);
|
apm_haltcpu(fd, haltcpu);
|
||||||
if (delta)
|
if (delta)
|
||||||
apm_set_timer(fd, delta);
|
apm_set_timer(fd, delta);
|
||||||
if (sleep)
|
if (dosleep)
|
||||||
apm_suspend(fd);
|
apm_suspend(fd);
|
||||||
else if (standby)
|
else if (standby)
|
||||||
apm_standby(fd);
|
apm_standby(fd);
|
||||||
|
Loading…
Reference in New Issue
Block a user