summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorMichael Shalayeff <mickey@cvs.openbsd.org>2001-07-06 21:08:01 +0000
committerMichael Shalayeff <mickey@cvs.openbsd.org>2001-07-06 21:08:01 +0000
commitfb7fb0318c5c819776d4de436a65c53a4d0eae0f (patch)
treed518dfe8e21821b9e9601cea41080c418ced62a8 /usr.sbin
parent646c2d4980933167019a486c06d75c1e2122ffcd (diff)
cleanup and knf
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/apm/apm.c379
1 files changed, 185 insertions, 194 deletions
diff --git a/usr.sbin/apm/apm.c b/usr.sbin/apm/apm.c
index d1a94f0a279..9421bb79371 100644
--- a/usr.sbin/apm/apm.c
+++ b/usr.sbin/apm/apm.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: apm.c,v 1.4 2001/07/05 20:10:32 mickey Exp $ */
+/* $OpenBSD: apm.c,v 1.5 2001/07/06 21:08:00 mickey Exp $ */
/*
* Copyright (c) 1996 John T. Kohl
@@ -30,14 +30,13 @@
*/
#include <stdio.h>
-#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
+#include <errno.h>
#include <err.h>
#include <string.h>
#include <sys/types.h>
-#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
@@ -49,15 +48,12 @@
#define TRUE 1
extern char *__progname;
-extern char *optarg;
void usage(void);
void zzusage(void);
-int do_zzz(const char *pn, enum apm_action action);
+int do_zzz(int, enum apm_action action);
int open_socket(const char *pn);
-int send_command(int fd,
- struct apm_command *cmd,
- struct apm_reply *reply);
+int send_command(int fd, struct apm_command *cmd, struct apm_reply *reply);
void
usage(void)
@@ -80,215 +76,210 @@ send_command(int fd,
struct apm_command *cmd,
struct apm_reply *reply)
{
- /* send a command to the apm daemon */
- cmd->vno = APMD_VNO;
+ /* send a command to the apm daemon */
+ cmd->vno = APMD_VNO;
- if (send(fd, cmd, sizeof(*cmd), 0) == sizeof(*cmd)) {
- if (recv(fd, reply, sizeof(*reply), 0) != sizeof(*reply)) {
- warn("invalid reply from APM daemon");
- return 1;
+ if (send(fd, cmd, sizeof(*cmd), 0) == sizeof(*cmd)) {
+ if (recv(fd, reply, sizeof(*reply), 0) != sizeof(*reply)) {
+ warn("invalid reply from APM daemon");
+ return 1;
+ }
+ } else {
+ warn("invalid send to APM daemon");
+ return 1;
}
- } else {
- warn("invalid send to APM daemon");
- return 1;
- }
- return 0;
+
+ return 0;
}
int
-do_zzz(const char *pn, enum apm_action action)
+do_zzz(int fd, enum apm_action action)
{
- struct apm_command command;
- struct apm_reply reply;
- int fd;
+ struct apm_command command;
+ struct apm_reply reply;
- switch (action) {
- case NONE:
- case SUSPEND:
- command.action = SUSPEND;
- break;
- case STANDBY:
- command.action = STANDBY;
- break;
- default:
- zzusage();
- }
- fd = open_socket(pn);
+ switch (action) {
+ case NONE:
+ case SUSPEND:
+ command.action = SUSPEND;
+ break;
+ case STANDBY:
+ command.action = STANDBY;
+ break;
+ default:
+ zzusage();
+ }
- if (fd == -1)
- err(1, "cannot open connection to APM daemon");
- printf("Suspending system...\n");
- exit(send_command(fd, &command, &reply));
+ printf("Suspending system...\n");
+ exit(send_command(fd, &command, &reply));
}
int
open_socket(const char *sockname)
{
- int sock, errr;
- struct sockaddr_un s_un;
+ int sock, errr;
+ struct sockaddr_un s_un;
- sock = socket(AF_UNIX, SOCK_STREAM, 0);
- if (sock == -1)
- err(1, "cannot create local socket");
+ sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (sock == -1)
+ err(1, "cannot create local socket");
+
+ s_un.sun_family = AF_UNIX;
+ strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path));
+ s_un.sun_len = SUN_LEN(&s_un);
+ if (connect(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1) {
+ errr = errno;
+ close(sock);
+ errno = errr;
+ err(1, "cannot open connection to APM daemon");
+ }
- s_un.sun_family = AF_UNIX;
- strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path));
- s_un.sun_len = SUN_LEN(&s_un);
- if (connect(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1) {
- errr = errno;
- close(sock);
- errno = errr;
- return -1;
- }
- return sock;
+ return sock;
}
int
main(int argc, char *argv[])
{
- char *sockname = _PATH_APM_SOCKET;
- int ch;
- int dostatus = FALSE;
- int doac = FALSE;
- int dopct = FALSE;
- int dobstate = FALSE;
- int domin = FALSE;
- int fd;
- int rval;
- int verbose = FALSE;
- enum apm_action action = NONE;
- struct apm_command command;
- struct apm_reply reply;
+ const char *sockname = _PATH_APM_SOCKET;
+ int dostatus = FALSE;
+ int doac = FALSE;
+ int dopct = FALSE;
+ int dobstate = FALSE;
+ int domin = FALSE;
+ int verbose = FALSE;
+ int ch, fd, rval;
+ enum apm_action action = NONE;
+ struct apm_command command;
+ struct apm_reply reply;
- while ((ch = getopt(argc, argv, "lmbvasSzf:")) != -1)
- switch(ch) {
- case 'v':
- verbose = TRUE;
- break;
- case 'f':
- sockname = optarg;
- break;
- case 'z':
- if (action != NONE)
- usage();
- action = SUSPEND;
- break;
- case 'S':
- if (action != NONE)
- usage();
- action = STANDBY;
- break;
- case 's':
- if (action != NONE && action != GETSTATUS)
- usage();
- dostatus = TRUE;
- action = GETSTATUS;
- break;
- case 'b':
- if (action != NONE && action != GETSTATUS)
- usage();
- dobstate = TRUE;
- action = GETSTATUS;
- break;
- case 'l':
- if (action != NONE && action != GETSTATUS)
- usage();
- dopct = TRUE;
- action = GETSTATUS;
- break;
- case 'm':
- if (action != NONE && action != GETSTATUS)
- usage();
- domin = TRUE;
- action = GETSTATUS;
- break;
- case 'a':
- if (action != NONE && action != GETSTATUS)
- usage();
- doac = TRUE;
- action = GETSTATUS;
- break;
- case '?':
- default:
- usage();
- }
+ while ((ch = getopt(argc, argv, "lmbvasSzf:")) != -1)
+ switch(ch) {
+ case 'v':
+ verbose = TRUE;
+ break;
+ case 'f':
+ sockname = optarg;
+ break;
+ case 'z':
+ if (action != NONE)
+ usage();
+ action = SUSPEND;
+ break;
+ case 'S':
+ if (action != NONE)
+ usage();
+ action = STANDBY;
+ break;
+ case 's':
+ if (action != NONE && action != GETSTATUS)
+ usage();
+ dostatus = TRUE;
+ action = GETSTATUS;
+ break;
+ case 'b':
+ if (action != NONE && action != GETSTATUS)
+ usage();
+ dobstate = TRUE;
+ action = GETSTATUS;
+ break;
+ case 'l':
+ if (action != NONE && action != GETSTATUS)
+ usage();
+ dopct = TRUE;
+ action = GETSTATUS;
+ break;
+ case 'm':
+ if (action != NONE && action != GETSTATUS)
+ usage();
+ domin = TRUE;
+ action = GETSTATUS;
+ break;
+ case 'a':
+ if (action != NONE && action != GETSTATUS)
+ usage();
+ doac = TRUE;
+ action = GETSTATUS;
+ break;
+ case '?':
+ default:
+ usage();
+ }
- if (!strcmp(__progname, "zzz")) {
- return (do_zzz(sockname, action));
- }
+ fd = open_socket(sockname);
- fd = open_socket(sockname);
+ if (!strcmp(__progname, "zzz"))
+ return (do_zzz(fd, action));
- switch (action) {
- case NONE:
- verbose = doac = dopct = dobstate = dostatus = domin = TRUE;
- action = GETSTATUS;
- /* fallthrough */
- case GETSTATUS:
- if (fd == -1) {
- /* open the device directly and get status */
- fd = open(_PATH_APM_NORMAL, O_RDONLY);
- if (fd == -1) {
- err(1, "cannot contact APM daemon and cannot open " _PATH_APM_NORMAL);
- }
- if (ioctl(fd, APM_IOC_GETPOWER, &reply.batterystate) == 0)
- goto printval;
- }
- case SUSPEND:
- case STANDBY:
- command.action = action;
- break;
- default:
- usage();
- }
-
- if ((rval = send_command(fd, &command, &reply)) == 0) {
- switch (action) {
- case GETSTATUS:
- printval:
- if (verbose) {
- if (dobstate)
- printf("Battery state: %s\n",
- battstate(reply.batterystate.battery_state));
- if (dopct)
- printf("Battery remaining: %d percent\n",
- reply.batterystate.battery_life);
- if (domin)
- printf("Battery life estimate: %d minutes\n",
- reply.batterystate.minutes_left);
- if (doac)
- printf("A/C adapter state: %s\n",
- ac_state(reply.batterystate.ac_state));
- if (dostatus)
- printf("Power management enabled\n");
- } else {
- if (dobstate)
- printf("%d\n", reply.batterystate.battery_state);
- if (dopct)
- printf("%d\n", reply.batterystate.battery_life);
- if (domin)
- printf("%d\n", reply.batterystate.minutes_left);
- if (doac)
- printf("%d\n", reply.batterystate.ac_state);
- if (dostatus)
- printf("1\n");
- }
- break;
- default:
- break;
- }
- switch (reply.newstate) {
- case SUSPEND:
- printf("System will enter suspend mode momentarily.\n");
- break;
- case STANDBY:
- printf("System will enter standby mode momentarily.\n");
- break;
- default:
- break;
- }
- } else
- errx(rval, "cannot get reply from APM daemon");
+ switch (action) {
+ case NONE:
+ action = GETSTATUS;
+ verbose = doac = dopct = dobstate = dostatus = domin =
+ TRUE;
+ /* fallthrough */
+ case GETSTATUS:
+ if (fd == -1) {
+ /* open the device directly and get status */
+ fd = open(_PATH_APM_NORMAL, O_RDONLY);
+ if (ioctl(fd, APM_IOC_GETPOWER,
+ &reply.batterystate) == 0)
+ goto printval;
+ }
+ case SUSPEND:
+ case STANDBY:
+ command.action = action;
+ break;
+ default:
+ usage();
+ }
- return (0);
+ if ((rval = send_command(fd, &command, &reply)) == 0) {
+ switch (action) {
+ case GETSTATUS:
+ printval:
+ if (verbose) {
+ if (dobstate)
+ printf("Battery state: %s\n",
+ battstate(reply.batterystate.battery_state));
+ if (dopct)
+ printf("Battery remaining: %d percent\n",
+ reply.batterystate.battery_life);
+ if (domin)
+ printf("Battery life estimate: %d minutes\n",
+ reply.batterystate.minutes_left);
+ if (doac)
+ printf("A/C adapter state: %s\n",
+ ac_state(reply.batterystate.ac_state));
+ if (dostatus)
+ printf("Power management enabled\n");
+ } else {
+ if (dobstate)
+ printf("%d\n",
+ reply.batterystate.battery_state);
+ if (dopct)
+ printf("%d\n",
+ reply.batterystate.battery_life);
+ if (domin)
+ printf("%d\n",
+ reply.batterystate.minutes_left);
+ if (doac)
+ printf("%d\n",
+ reply.batterystate.ac_state);
+ if (dostatus)
+ printf("1\n");
+ }
+ break;
+ default:
+ }
+ switch (reply.newstate) {
+ case SUSPEND:
+ printf("System will enter suspend mode momentarily.\n");
+ break;
+ case STANDBY:
+ printf("System will enter standby mode momentarily.\n");
+ break;
+ default:
+ }
+ } else
+ errx(rval, "cannot get reply from APM daemon");
+ return (0);
}