Package: apcupsd
Version: 3.10.17-2
Severity: normal
Tags: patch
Hi,
Example client.c cannot be compiled as supplied in the examples dir.
I downloaded the source and added the code necessary to compile it.
BTW thanks for maintaining apcupsd: it's the reason I picked an APC product.
Regards,
Hugo
===
--- client.c2003-11-26 09:54:46.0 -0600
+++ do_apcups_client.cpp2005-12-25 07:33:12.0 -0600
@@ -17,15 +17,34 @@
* For additional examples of code, see cgi/upsfetch.c
*/
-#include "apc.h"
+// 12/25/2005 - Changed by Hugo Vanwoerkom to compile + run on Debian
Sarge.
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
-#ifdef HAVE_NISLIB
/* Default values, can be changed on command line */
#define SERV_TCP_PORT 3551
#define SERV_HOST_ADDR "127.0.0.1"
-void handle_client();
+//Only working parms:
+//127.0.0.1:3551 status
+//127.0.0.1:3551 events
+
+
+void handle_client(FILE *fp, int sockfd, char *cmd);
+int net_open(char *host, char *service, int port);
+int net_recv(int sockfd, char *buff, int maxlen);
+int net_send(int sockfd, char *buff, int len);
+void net_close(int sockfd);
+int asnprintf(char *str, size_t size, const char *fmt, ...);
+void (*error_out)(const char *file, int line, const char *fmt,...);
extern int net_errno;
@@ -66,6 +85,7 @@
error_abort(msg);
}
+//printf("%d %s\n",__LINE__,__FILE__);
handle_client(stdin, sockfd, cmd); /* do it all */
net_close(sockfd);
exit(0);
@@ -83,39 +103,265 @@
void handle_client(FILE *fp, int sockfd, char *cmd)
{
- int n;
- char sendline[MAXLINE];
- char recvline[MAXLINE+1];
- int quit = 0;
-
- while (!quit) {
- if (cmd) {
- strcpy(sendline, cmd); /* one shot command */
-quit = 1;
- } else if (fgets(sendline, MAXLINE, fp) == NULL) {
-break;
- }
- n = strlen(sendline);
- if (net_send(sockfd, sendline, n) != n)
- error_abort("handle_client: write error on socket");
-
- while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
- recvline[n] = 0;
- fputs(recvline, stdout);
- }
- if (n < 0) {
-char msg[200];
- sprintf(msg, "handle_client: net_recv error: %s\n",
strerror(net_errno));
-error_abort(msg);
- }
- }
+int n;
+char sendline[MAXLINE];
+char recvline[MAXLINE+1];
+int quit = 0;
+
+while (!quit) {
+ if (cmd) {
+ strcpy(sendline, cmd); /* one shot command */
+ quit = 1;
+// It's never NULL:
+// } else if (fgets(sendline, MAXLINE, fp) == NULL) {
+// break;
+// }
+ }
+ else {
+ fgets(sendline, MAXLINE, fp);
+ if (strlen (sendline) == 1) {
+ break;
+ }
+ }
+ n = strlen(sendline);
+ if (net_send(sockfd, sendline, n) != n)
+ error_abort("handle_client: write error on socket");
+
+ while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
+ recvline[n] = 0;
+ fputs(recvline, stdout);
+ }
+ if (n < 0) {
+ char msg[200];
+ sprintf(msg, "handle_client: net_recv error: %s\n",
strerror(net_errno));
+ error_abort(msg);
+ }
+}
+}
+
+int net_errno = 0; /* error number -- not yet implemented */
+char *net_errmsg = NULL; /* pointer to error message */
+char net_errbuf[256];/* error message buffer for messages */
+
+
+/*
+ * Read a 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, nread;
+
+nleft = nbytes;
+
+while (nleft > 0) {
+
+ do {
+ nread = read(fd, ptr, nleft);
+ } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
+ if (nread <= 0) {
+ net_errno = errno;
+ return(nread); /* error, or EOF */
+ }
+ nleft -= nread;
+ ptr += nread;
+}
+return(nbytes - nleft); /* return >= 0 */
}
-#else /* HAVE_NISLIB */
+/*
+ * Write nbytes to the network.
+ * It may require several writes.
+ */
+
+static int write_nbytes(int fd, char *ptr, int nbytes)
+{
+int nleft, nwritten;
+
+nleft = nbytes;
+while (nleft > 0) {
+ nwritten = write(fd, ptr, nleft);
+ if (nwritten <= 0) {
+ net_errno = errno;
+ return (nwritten); /* error */
+ }
+
+ nleft -= nwritten;
+ ptr += nwritten;
+}
+return(nbytes-nleft);
+}
+
+/*
+ * Receive a message from the other end. Each message consists of
+ * two packets. The first is a header that contains the size
+ * of the data that follows in the second packet.
+ * Returns number of bytes read
+ * Returns 0 on end of file
+ * Returns -1 on hard end o