1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 #include <sys/select.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
45 int scan_hddtemp(const char *arg, char **dev, char **addr, int *port, char** temp)
47 char buf1[32], buf2[64];
50 ret = sscanf(arg, "%31s %63s %d", buf1, buf2, &n);
56 if (strncmp(buf1, "/dev/", 5)) {
57 strncpy(buf1 + 5, buf1, 32 - 5);
58 strncpy(buf1, "/dev/", 5);
60 *dev = strndup(buf1, text_buffer_size);
63 *addr = strndup(buf2, text_buffer_size);
65 *addr = strndup("127.0.0.1", text_buffer_size);
74 *temp = malloc(text_buffer_size);
75 memset(*temp, 0, text_buffer_size);
80 char *get_hddtemp_info(char *dev, char *hostaddr, int port, char *unit)
83 struct hostent he, *he_res = 0;
86 struct sockaddr_in addr;
89 int len, i, devlen = strlen(dev);
91 char *p, *out, *r = NULL;
93 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
99 #ifdef HAVE_GETHOSTBYNAME_R
100 if (gethostbyname_r(hostaddr, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
101 ERR("hddtemp gethostbyname_r: %s", hstrerror(h_errno));
104 #else /* HAVE_GETHOSTBYNAME_R */
105 he_res = gethostbyname(hostaddr);
107 perror("gethostbyname");
110 #endif /* HAVE_GETHOSTBYNAME_R */
112 addr.sin_family = AF_INET;
113 addr.sin_port = htons(port);
114 addr.sin_addr = *((struct in_addr *) he_res->h_addr);
115 memset(&(addr.sin_zero), 0, 8);
117 if (connect(sockfd, (struct sockaddr *) &addr,
118 sizeof(struct sockaddr)) == -1) {
124 FD_SET(sockfd, &rfds);
126 /* We're going to wait up to a quarter a second to see whether there's
127 * any data available. Polling with timeout set to 0 doesn't seem to work
132 i = select(sockfd + 1, &rfds, NULL, NULL, &tv);
134 if (errno == EINTR) { /* silently ignore interrupted system call */
141 /* No data available */
149 i = recv(sockfd, p, BUFLEN - (p - buf), 0);
156 } while (i > 0 && p < buf + BUFLEN - 1);
164 /* The first character read is the separator. */
169 if (!strncmp(p, dev, devlen)) {
184 if (!strncmp(out, "NA", 2)) {
185 strncpy(buf, "N/A", BUFLEN);
192 for (i = 0; i < 5; i++) {