#include <error.h>
#include "parser.h"
+#include "process.h"
-static int str_to_dec(char *src, char **dst)
+static int dec_to_int(char *src, char **dst)
{
int ret;
return ret;
}
-static int get_word(char *src, char **dst, char *word)
+static float dec_to_float(char *src, char **dst)
+{
+ float ret;
+
+ while(((*src < '0') || (*src > '9')) && *src)
+ src++;
+
+ ret = atof(src);
+
+ if (dst) {
+ while((((*src >= '0') && (*src <= '9')) || (*src =='.')) && *src)
+ src++;
+ *dst = src;
+ }
+
+ return ret;
+}
+
+/*
+ * Copy white space separated stirng from *src to *word
+ *
+ * src: source
+ * dst: place where store the end of word pointer from source string
+ * word: destination where to copy the string
+ * size: maximum size of bytes to copy to the output buffer
+ *
+ * return the number of bytes copied
+ */
+
+static int get_word(char *src, char **dst, char *word, int size)
{
int ret = 0;
while(((*src == ' ') || (*src == '\t')) && *src)
src++;
- while(((*src != ' ') && (*src != '\t')) && *src) {
+ while(((*src != ' ') && (*src != '\t')) && *src && (ret < (size - 1))) {
*word = *src;
word++;
src++;
return -1;
}
- user = str_to_dec(str, &str);
- nice = str_to_dec(str, &str);
- sys = str_to_dec(str, &str);
- idle = str_to_dec(str, &str);
- wait = str_to_dec(str, &str);
- irq = str_to_dec(str, &str);
- softirq = str_to_dec(str, &str);
+ user = dec_to_int(str, &str);
+ nice = dec_to_int(str, &str);
+ sys = dec_to_int(str, &str);
+ idle = dec_to_int(str, &str);
+ wait = dec_to_int(str, &str);
+ irq = dec_to_int(str, &str);
+ softirq = dec_to_int(str, &str);
sprintf(data, "%d:%d:%d:%d:%d:%d:%d",
user, nice, sys, idle, wait, irq, softirq);
}
while (fgets(buf, 1024, file)) {
- get_word(buf, 0, word);
+ get_word(buf, 0, word, 1024);
if (!strcmp(word, "MemFree:")) {
- free = str_to_dec(buf, 0);
+ free = dec_to_int(buf, 0);
} else if (!strcmp(word, "Buffers:")) {
- buffered = str_to_dec(buf, 0);
+ buffered = dec_to_int(buf, 0);
} else if (!strcmp(word, "Cached:")) {
- cache = str_to_dec(buf, 0);
+ cache = dec_to_int(buf, 0);
} else if (!strcmp(word, "Active:")) {
- active = str_to_dec(buf, 0);
+ active = dec_to_int(buf, 0);
} else if (!strcmp(word, "Inactive:")) {
- inactive = str_to_dec(buf, 0);
+ inactive = dec_to_int(buf, 0);
} else if (!strcmp(word, "SwapFree:")) {
- swapfree = str_to_dec(buf, 0);
+ swapfree = dec_to_int(buf, 0);
} else if (!strcmp(word, "AnonPages:")) {
- anon = str_to_dec(buf, 0);
+ anon = dec_to_int(buf, 0);
} else if (!strcmp(word, "Slab:")) {
- slab = str_to_dec(buf, 0);
+ slab = dec_to_int(buf, 0);
} else if (!strcmp(word, "PageTables:")) {
- tables = str_to_dec(buf, 0);
+ tables = dec_to_int(buf, 0);
}
}
fclose(file);
return 0;
}
+
+int digitemp_parser(char *data)
+{
+ const char digitemp_cmd[] = "/usr/bin/digitemp";
+ char *const digitemp_args[] = { "", "-o2", "-a", "-q", 0 };
+ FILE *readf, *writef;
+ int pid;
+ char buf[1024], *bptr = buf;
+ char t1[16], t2[16], t3[16];
+
+ pid = run_piped_stream(digitemp_cmd, digitemp_args, &readf, &writef);
+ if (pid < 0) {
+ fprintf(stderr, "Failed to parse digitemp");
+ sprintf(data, "U:U:U");
+ return -1;
+ }
+
+ fgets(buf, 1024, readf);
+
+ get_word(bptr, &bptr, t1, 16);
+ *bptr++ = 0;
+ get_word(bptr, &bptr, t2, 16);
+ *bptr++ = 0;
+ get_word(bptr, &bptr, t3, 16);
+ *bptr++ = 0;
+
+ while (fgets(buf, 1024, readf));
+ harvest_zombies(pid);
+ sprintf(data, "U:%s:%s", t2, t3);
+ return 0;
+}