summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ed56e29)
raw | patch | inline | side by side (parent: ed56e29)
author | octo <octo> | |
Sat, 10 Jun 2006 13:09:25 +0000 (13:09 +0000) | ||
committer | octo <octo> | |
Sat, 10 Jun 2006 13:09:25 +0000 (13:09 +0000) |
`sread' is the secure-read function provided by `common.c'. This removed
duplicate code..
duplicate code..
src/apcups.c | patch | blob | history |
diff --git a/src/apcups.c b/src/apcups.c
index f5a0deac941f21b745e1bdd7fd7409072889fb3e..e6f6cae9fa758350637eae66f01d3403881f2aa3 100644 (file)
--- a/src/apcups.c
+++ b/src/apcups.c
#define BIG_BUF 4096
-/*
- * Read nbytes from the network.
- * It is possible that the total bytes require in several
- * read requests
- */
-static int read_nbytes (int *fd, char *ptr, int nbytes)
-{
- int nleft;
- int nread;
-
- nleft = nbytes;
- nread = -1;
-
- assert (*fd >= 0);
-
- while ((nleft > 0) && (nread != 0))
- {
- nread = read (*fd, ptr, nleft);
-
- if ((nread < 0) && (errno == EINTR || errno == EAGAIN))
- continue;
-
- if (nread < 0)
- {
- *fd = -1;
- DBG ("Reading from socket failed failed: %s; *fd = -1;", strerror (errno));
- syslog (LOG_ERR, "apcups plugin: Reading from socket failed failed: %s", strerror (errno));
- return (-1);
- }
-
- if (nread == 0)
- {
- DBG ("Received EOF. Closing socket %i.", *fd);
- close (*fd);
- *fd = -1;
- return (nbytes - nleft);
- }
-
- nleft -= nread;
- ptr += nread;
- }
-
- return (nbytes - nleft);
-}
-
/*
* Write nbytes to the network.
* It may require several writes.
short pktsiz;
/* get data size -- in short */
- if ((nbytes = read_nbytes (sockfd, (char *) &pktsiz, sizeof (short))) <= 0)
+ if ((nbytes = sread (*sockfd, (void *) &pktsiz, sizeof (short))) <= 0)
+ {
+ *sockfd = -1;
return (-1);
+ }
if (nbytes != sizeof (short))
return (-2);
return (0);
/* now read the actual data */
- if ((nbytes = read_nbytes (sockfd, buf, pktsiz)) <= 0)
- return (-2);
+ if ((nbytes = sread (*sockfd, (void *) buf, pktsiz)) <= 0)
+ {
+ *sockfd = -1;
+ return (-1);
+ }
if (nbytes != pktsiz)
return (-2);
return (nbytes);
-} /* static int net_recv (int sockfd, char *buf, int buflen) */
+} /* static int net_recv (int *sockfd, char *buf, int buflen) */
/*
* Send a message over the network. The send consists of