From e26a4dd4d56b85a08cb02c1df6cda6fba3719c6b Mon Sep 17 00:00:00 2001 From: Philip Kovacs Date: Thu, 10 Aug 2006 23:49:59 +0000 Subject: [PATCH] add hddtemp.c and fix ? display git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@680 7f574dfc-610e-0410-a909-a81674777703 --- src/conky.c | 2 +- src/hddtemp.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 src/hddtemp.c diff --git a/src/conky.c b/src/conky.c index 23c853e6..03b026b3 100644 --- a/src/conky.c +++ b/src/conky.c @@ -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 index 00000000..0df8600c --- /dev/null +++ b/src/hddtemp.c @@ -0,0 +1,144 @@ +#include "conky.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +} +