summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: bfec4e6)
raw | patch | inline | side by side (parent: bfec4e6)
author | octo <octo> | |
Fri, 19 May 2006 07:23:38 +0000 (07:23 +0000) | ||
committer | octo <octo> | |
Fri, 19 May 2006 07:23:38 +0000 (07:23 +0000) |
- Fixed more indentation.
- Made internal-only functions static.
- Made internal-only functions static.
src/apcups.c | patch | blob | history |
diff --git a/src/apcups.c b/src/apcups.c
index f481d0eb1ea36fcec5542702f10e15f1c5a9cdef..b99b2e70442c576715b6a675a0e644b0b501f2e1 100644 (file)
--- a/src/apcups.c
+++ b/src/apcups.c
}
/* Close the network connection */
-void net_close (int sockfd)
+static void net_close (int sockfd)
{
short pktsiz = 0;
* Returns -1 on error
* Returns socket file descriptor otherwise
*/
-int net_open(char *host, char *service, int port)
+static int net_open(char *host, char *service, int port)
{
int sockfd;
struct hostent *hp;
* Returns -1 on hard end of file (i.e. network connection close)
* Returns -2 on error
*/
-int net_recv(int sockfd, char *buff, int maxlen)
-{
- int nbytes;
- short pktsiz;
-
- /* get data size -- in short */
- if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
- /* probably pipe broken because client died */
- return -1; /* assume hard EOF received */
- }
- if (nbytes != sizeof(short))
- return -2;
-
- pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
- if (pktsiz > maxlen) {
- net_errmsg = "net_recv: record length too large\n";
- return -2;
- }
- if (pktsiz == 0)
- return 0; /* soft EOF */
-
- /* now read the actual data */
- if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
- net_errmsg = "net_recv: read_nbytes error\n";
- return -2;
- }
- if (nbytes != pktsiz) {
- net_errmsg = "net_recv: error in read_nbytes\n";
- return -2;
- }
-
- return (nbytes); /* return actual length of message */
+static int net_recv(int sockfd, char *buff, int maxlen)
+{
+ int nbytes;
+ short pktsiz;
+
+ /* get data size -- in short */
+ if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
+ /* probably pipe broken because client died */
+ return -1; /* assume hard EOF received */
+ }
+ if (nbytes != sizeof(short))
+ return -2;
+
+ pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
+ if (pktsiz > maxlen) {
+ net_errmsg = "net_recv: record length too large\n";
+ return -2;
+ }
+ if (pktsiz == 0)
+ return 0; /* soft EOF */
+
+ /* now read the actual data */
+ if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
+ net_errmsg = "net_recv: read_nbytes error\n";
+ return -2;
+ }
+ if (nbytes != pktsiz) {
+ net_errmsg = "net_recv: error in read_nbytes\n";
+ return -2;
+ }
+
+ return (nbytes); /* return actual length of message */
}
/*
* Returns number of bytes sent
* Returns -1 on error
*/
-int net_send(int sockfd, char *buff, int len)
-{
- int rc;
- short pktsiz;
-
- /* send short containing size of data packet */
- pktsiz = htons((short)len);
- rc = write_nbytes(sockfd, (char *)&pktsiz, sizeof(short));
- if (rc != sizeof(short)) {
- net_errmsg = "net_send: write_nbytes error of length prefix\n";
- return -1;
- }
-
- /* send data packet */
- rc = write_nbytes(sockfd, buff, len);
- if (rc != len) {
- net_errmsg = "net_send: write_nbytes error\n";
- return -1;
- }
-
- return rc;
+static int net_send(int sockfd, char *buff, int len)
+{
+ int rc;
+ short pktsiz;
+
+ /* send short containing size of data packet */
+ pktsiz = htons((short)len);
+ rc = write_nbytes(sockfd, (char *)&pktsiz, sizeof(short));
+ if (rc != sizeof(short)) {
+ net_errmsg = "net_send: write_nbytes error of length prefix\n";
+ return -1;
+ }
+
+ /* send data packet */
+ rc = write_nbytes(sockfd, buff, len);
+ if (rc != len) {
+ net_errmsg = "net_send: write_nbytes error\n";
+ return -1;
+ }
+
+ return rc;
}
/* Get and print status from apcupsd NIS server */
static void do_pthreads_status(char *host, int port, struct apc_detail_s *apcups_detail)
{
- int sockfd, n;
- char recvline[MAXSTRING + 1];
- char *tokptr;
+ int sockfd, n;
+ char recvline[MAXSTRING + 1];
+ char *tokptr;
- if ((sockfd = net_open(host, NULL, port)) < 0)
- Error_abort0(net_errmsg);
-
- net_send(sockfd, "status", 6);
-
- while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
- recvline[n] = 0;
+ if ((sockfd = net_open(host, NULL, port)) < 0)
+ Error_abort0(net_errmsg);
+
+ net_send(sockfd, "status", 6);
+
+ while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
+ recvline[n] = 0;
#ifdef APCMAIN
- fputs(recvline, stdout);
- int printit = 1;
+ fputs(recvline, stdout);
+ int printit = 1;
#else
- int printit = 0;
+ int printit = 0;
#endif /* ifdef APCMAIN */
- tokptr = strtok(recvline,":");
- while(tokptr != NULL) {
- /* Look for Limit_Add */
- if(strncmp("LINEV",tokptr,5) == 0) {
- if(printit) fprintf(stdout," Found LINEV.\n");
- tokptr = strtok(NULL," \t");
- if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
- apcups_detail->linev = atof (tokptr);
- }else if(strncmp("BATTV",tokptr,5) == 0) {
- if(printit) fprintf(stdout," Found BATTV.\n");
- tokptr = strtok(NULL," \t");
- if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
- apcups_detail->battv = atof (tokptr);
- }else if(strncmp("ITEMP",tokptr,5) == 0) {
- if(printit) fprintf(stdout," Found ITEMP.\n");
- tokptr = strtok(NULL," \t");
- if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
- apcups_detail->itemp = atof (tokptr);
- }else if(strncmp("LOADPCT",tokptr,7) == 0) {
- if(printit) fprintf(stdout," Found LOADPCT.\n");
- tokptr = strtok(NULL," \t");
- if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
- apcups_detail->loadpct = atof (tokptr);
- }else if(strncmp("BCHARGE",tokptr,7) == 0) {
- if(printit) fprintf(stdout," Found BCHARGE.\n");
- tokptr = strtok(NULL," \t");
- if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
- apcups_detail->bcharge = atof (tokptr);
- }else if(strncmp("OUTPUTV",tokptr,7) == 0) {
- if(printit) fprintf(stdout," Found OUTPUTV.\n");
- tokptr = strtok(NULL," \t");
- if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
- apcups_detail->outputv = atof (tokptr);
- }else if(strncmp("LINEFREQ",tokptr,8) == 0) {
- if(printit) fprintf(stdout," Found LINEFREQ.\n");
- tokptr = strtok(NULL," \t");
- if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
- apcups_detail->linefreq = atof (tokptr);
- }else if(strncmp("TIMELEFT",tokptr,8) == 0) {
- if(printit) fprintf(stdout," Found TIMELEFT.\n");
- tokptr = strtok(NULL," \t");
- if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
- apcups_detail->timeleft = atof (tokptr);
- } /* */
- tokptr = strtok(NULL,":");
- }
- }
+ tokptr = strtok(recvline,":");
+ while(tokptr != NULL) {
+ /* Look for Limit_Add */
+ if(strncmp("LINEV",tokptr,5) == 0) {
+ if(printit) fprintf(stdout," Found LINEV.\n");
+ tokptr = strtok(NULL," \t");
+ if(tokptr == NULL) continue;
+ if(printit) fprintf(stdout," Value %s.\n",tokptr);
+ apcups_detail->linev = atof (tokptr);
+ }else if(strncmp("BATTV",tokptr,5) == 0) {
+ if(printit) fprintf(stdout," Found BATTV.\n");
+ tokptr = strtok(NULL," \t");
+ if(tokptr == NULL) continue;
+ if(printit) fprintf(stdout," Value %s.\n",tokptr);
+ apcups_detail->battv = atof (tokptr);
+ }else if(strncmp("ITEMP",tokptr,5) == 0) {
+ if(printit) fprintf(stdout," Found ITEMP.\n");
+ tokptr = strtok(NULL," \t");
+ if(tokptr == NULL) continue;
+ if(printit) fprintf(stdout," Value %s.\n",tokptr);
+ apcups_detail->itemp = atof (tokptr);
+ }else if(strncmp("LOADPCT",tokptr,7) == 0) {
+ if(printit) fprintf(stdout," Found LOADPCT.\n");
+ tokptr = strtok(NULL," \t");
+ if(tokptr == NULL) continue;
+ if(printit) fprintf(stdout," Value %s.\n",tokptr);
+ apcups_detail->loadpct = atof (tokptr);
+ }else if(strncmp("BCHARGE",tokptr,7) == 0) {
+ if(printit) fprintf(stdout," Found BCHARGE.\n");
+ tokptr = strtok(NULL," \t");
+ if(tokptr == NULL) continue;
+ if(printit) fprintf(stdout," Value %s.\n",tokptr);
+ apcups_detail->bcharge = atof (tokptr);
+ }else if(strncmp("OUTPUTV",tokptr,7) == 0) {
+ if(printit) fprintf(stdout," Found OUTPUTV.\n");
+ tokptr = strtok(NULL," \t");
+ if(tokptr == NULL) continue;
+ if(printit) fprintf(stdout," Value %s.\n",tokptr);
+ apcups_detail->outputv = atof (tokptr);
+ }else if(strncmp("LINEFREQ",tokptr,8) == 0) {
+ if(printit) fprintf(stdout," Found LINEFREQ.\n");
+ tokptr = strtok(NULL," \t");
+ if(tokptr == NULL) continue;
+ if(printit) fprintf(stdout," Value %s.\n",tokptr);
+ apcups_detail->linefreq = atof (tokptr);
+ }else if(strncmp("TIMELEFT",tokptr,8) == 0) {
+ if(printit) fprintf(stdout," Found TIMELEFT.\n");
+ tokptr = strtok(NULL," \t");
+ if(tokptr == NULL) continue;
+ if(printit) fprintf(stdout," Value %s.\n",tokptr);
+ apcups_detail->timeleft = atof (tokptr);
+ } /* */
+ tokptr = strtok(NULL,":");
+ }
+ }
- if (n < 0)
- Error_abort0(net_errmsg);
-
- net_close(sockfd);
+ if (n < 0)
+ Error_abort0(net_errmsg);
+
+ net_close(sockfd);
}
#ifdef APCMAIN
int main(int argc, char **argv)
{
- /* we are not really going to use this */
- struct apc_detail_s apcups_detail;
+ /* we are not really going to use this */
+ struct apc_detail_s apcups_detail;
- if (!*host || strcmp(host, "0.0.0.0") == 0)
- host = "localhost";
-
- do_pthreads_status(host, port, &apcups_detail);
-
- return 0;
+ if (!*host || strcmp(host, "0.0.0.0") == 0)
+ host = "localhost";
+
+ do_pthreads_status(host, port, &apcups_detail);
+
+ return 0;
}
#else
static void apcups_init (void)