add hddtemp.c and fix ? display
authorPhilip Kovacs <pkovacs@users.sourceforge.net>
Thu, 10 Aug 2006 23:49:59 +0000 (23:49 +0000)
committerPhilip Kovacs <pkovacs@users.sourceforge.net>
Thu, 10 Aug 2006 23:49:59 +0000 (23:49 +0000)
git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@680 7f574dfc-610e-0410-a909-a81674777703

src/conky.c
src/hddtemp.c [new file with mode: 0644]

index 23c853e..03b026b 100644 (file)
@@ -3953,7 +3953,7 @@ static void generate_text_internal(char *p, int p_max_size, struct text_object *
                                } else if (unit == '*') {
                                        snprintf(p, p_max_size, "%s", temp);
                                } else {
-                                        snprintf(p, p_max_size, "%s?%c", temp, unit);
+                                        snprintf(p, p_max_size, "%s %c", temp, unit);
                                }
                        }
                        OBJ(offset) {
diff --git a/src/hddtemp.c b/src/hddtemp.c
new file mode 100644 (file)
index 0000000..0df8600
--- /dev/null
@@ -0,0 +1,144 @@
+#include "conky.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+
+#define BUFLEN 512
+#define PORT 7634
+
+char buf[BUFLEN];
+
+int scan_hddtemp(const char *arg, char **dev, char **addr, int *port)
+{
+       char buf1[32], buf2[64];
+       int n, ret;
+       
+       ret = sscanf(arg, "%31s %63s %d", buf1, buf2, &n);
+       
+       if (ret < 1)
+               return -1;
+
+       *dev = strdup(buf1);
+       if (ret >= 2)
+               *addr = strdup(buf2);
+       else
+               *addr = strdup("127.0.0.1");
+
+       if (ret == 3) 
+               *port = n;
+       else
+               *port = PORT;
+
+       return 0;
+}
+
+char *get_hddtemp_info(char *dev, char *hostaddr, int port, char *unit)
+{
+       int sockfd = 0;
+       struct hostent *he;
+       struct sockaddr_in addr;
+       struct timeval tv;
+       fd_set rfds;
+       int len, i, devlen = strlen(dev);
+       char sep;
+       char *p, *out, *r = NULL;
+       
+       if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
+               perror("socket");
+               return NULL;
+       }
+
+       he = gethostbyname(hostaddr);
+       if (!he) {
+               perror("gethostbyname");
+               goto out;
+       }
+
+       addr.sin_family = AF_INET;
+       addr.sin_port = htons(port);
+       addr.sin_addr = *((struct in_addr *)he->h_addr);
+       memset(&(addr.sin_zero), 0, 8);
+
+       if (connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) {
+               perror("connect");
+               goto out;
+       }
+
+       FD_ZERO(&rfds);
+       FD_SET(sockfd, &rfds);
+
+       /* We're going to wait up to a quarter a second to see whether
+        * there's any data available. Polling with timeout set to 0
+        * doesn't seem to work with hddtemp. */
+       tv.tv_sec = 0;
+       tv.tv_usec = 250000;
+       
+       i = select(sockfd+1, &rfds, NULL, NULL, &tv);
+       if (i == -1)
+               perror("select");
+
+       /* No data available */
+       if (i <= 0)
+               goto out;
+       
+       p = buf;
+       len = 0;
+       do {
+               i = recv(sockfd, p, BUFLEN - (p-buf), 0);
+               if (i < 0) {
+                       perror("recv");
+                       goto out;
+               }
+               len += i;
+               p += i;
+       } while (i > 0 && p < buf + BUFLEN - 1);
+       
+       if (len < 2) {
+               goto out;
+       }
+       
+       buf[len] = 0;
+               
+       /* The first character read is the separator. */
+       sep = buf[0];
+       p = buf+1;
+       
+       while (*p) {
+               if (!strncmp(p, dev, devlen)) {
+                       p += devlen + 1; 
+                       p = strchr(p, sep);
+                       if (!p) 
+                               goto out;
+                       p++;
+                       out = p;        
+                       p = strchr(p, sep);
+                       if (!p) 
+                               goto out;
+                       *p = '\0';
+                       p++;
+                       *unit = *p;     
+                       if (!strncmp(out, "NA", 2)) {
+                               strcpy(buf, "N/A");
+                               r = buf;
+                       } else {                                        
+                               r = out;
+                       }
+                       goto out;
+               } else {
+                       for (i = 0; i < 5; i++) {
+                               p = strchr(p, sep);
+                               if (!p)
+                                       goto out;
+                               p++;
+                       }
+               }
+       }
+out:   close(sockfd);
+       return r;
+}
+