changes related to temperature and layout
[monky] / src / exec.c
index 4057d09..c9e4261 100644 (file)
@@ -10,7 +10,7 @@
  * Please see COPYING for details
  *
  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
@@ -93,6 +93,7 @@ static FILE* pid_popen(const char *command, const char *mode, pid_t *child) {
                } else {
                        close(1);
                }
+               close(parentend);
                dup(childend);  //by dupping childend, the returned fd will have close-on-exec turned off
                execl("/bin/sh", "sh", "-c", command, (char *) NULL);
                _exit(EXIT_FAILURE); //child should die here, (normally execl will take care of this but it can fail)
@@ -140,7 +141,8 @@ static inline double get_barnum(char *buf)
        return barnum;
 }
 
-static inline void read_exec(const char *data, char *buf, const int size)
+static inline void read_exec(const char *data, char *buf, const int size, const
+               char use_alarm)
 {
        FILE *fp;
 
@@ -149,13 +151,13 @@ static inline void read_exec(const char *data, char *buf, const int size)
        if (!data)
                return;
 
-       alarm(update_interval);
+       if (use_alarm) alarm(update_interval);
        fp = pid_popen(data, "r", &childpid);
        if(fp) {
                int length;
 
                length = fread(buf, 1, size, fp);
-               pclose(fp);
+               fclose(fp);
                buf[length] = '\0';
                if (length > 0 && buf[length - 1] == '\n') {
                        buf[length - 1] = '\0';
@@ -163,7 +165,7 @@ static inline void read_exec(const char *data, char *buf, const int size)
        } else {
                buf[0] = '\0';
        }
-       alarm(0);
+       if (use_alarm) alarm(0);
 }
 
 static void *threaded_exec(void *) __attribute__((noreturn));
@@ -176,7 +178,7 @@ static void *threaded_exec(void *arg)
 
        while (1) {
                buff = malloc(text_buffer_size);
-               read_exec(ed->cmd, buff, text_buffer_size);
+               read_exec(ed->cmd, buff, text_buffer_size, 0);
                p2 = buff;
                while (*p2) {
                        if (*p2 == '\001') {
@@ -216,7 +218,7 @@ void scan_pre_exec_arg(struct text_object *obj, const char *arg)
        char buf[2048];
 
        obj->type = OBJ_text;
-       read_exec(arg, buf, sizeof(buf));
+       read_exec(arg, buf, sizeof(buf), 1);
        obj->data.s = strndup(buf, text_buffer_size);
 }
 
@@ -258,7 +260,7 @@ void scan_execgraph_arg(struct text_object *obj, const char *arg)
 
 void print_exec(struct text_object *obj, char *p, int p_max_size)
 {
-       read_exec(obj->data.s, p, p_max_size);
+       read_exec(obj->data.s, p, p_max_size, 1);
        remove_deleted_chars(p);
 }
 
@@ -271,7 +273,7 @@ void print_execp(struct text_object *obj, char *p, int p_max_size)
        buf = malloc(text_buffer_size);
        memset(buf, 0, text_buffer_size);
 
-       read_exec(obj->data.s, buf, text_buffer_size);
+       read_exec(obj->data.s, buf, text_buffer_size, 1);
 
        tmp_info = malloc(sizeof(struct information));
        memcpy(tmp_info, &info, sizeof(struct information));
@@ -292,7 +294,7 @@ void print_execi(struct text_object *obj, char *p, int p_max_size)
        if (time_to_update(ed)) {
                if (!ed->buffer)
                        ed->buffer = malloc(text_buffer_size);
-               read_exec(ed->cmd, ed->buffer, text_buffer_size);
+               read_exec(ed->cmd, ed->buffer, text_buffer_size, 1);
                ed->last_update = current_update_time;
        }
        snprintf(p, p_max_size, "%s", ed->buffer);
@@ -319,7 +321,7 @@ void print_execpi(struct text_object *obj, char *p, int p_max_size)
                        ed->buffer = malloc(text_buffer_size);
 
                length = fread(ed->buffer, 1, text_buffer_size, fp);
-               pclose(fp);
+               fclose(fp);
 
                output = ed->buffer;
                output[length] = '\0';
@@ -365,7 +367,7 @@ void print_execgauge(struct text_object *obj, char *p, int p_max_size)
 {
        double barnum;
 
-       read_exec(obj->data.s, p, p_max_size);
+       read_exec(obj->data.s, p, p_max_size, 1);
        barnum = get_barnum(p); /*using the same function*/
 
        if (barnum >= 0.0) {
@@ -383,7 +385,7 @@ void print_execgraph(struct text_object *obj, char *p, int p_max_size)
        if (!ed)
                return;
 
-       read_exec(ed->cmd, p, p_max_size);
+       read_exec(ed->cmd, p, p_max_size, 1);
        barnum = get_barnum(p);
 
        if (barnum > 0) {
@@ -401,7 +403,7 @@ void print_execigraph(struct text_object *obj, char *p, int p_max_size)
        if (time_to_update(ed)) {
                double barnum;
 
-               read_exec(ed->cmd, p, p_max_size);
+               read_exec(ed->cmd, p, p_max_size, 1);
                barnum = get_barnum(p);
 
                if (barnum >= 0.0) {
@@ -423,7 +425,7 @@ void print_execigauge(struct text_object *obj, char *p, int p_max_size)
        if (time_to_update(ed)) {
                double barnum;
 
-               read_exec(ed->cmd, p, p_max_size);
+               read_exec(ed->cmd, p, p_max_size, 1);
                barnum = get_barnum(p);
 
                if (barnum >= 0.0) {
@@ -437,7 +439,7 @@ void print_execigauge(struct text_object *obj, char *p, int p_max_size)
 void print_execbar(struct text_object *obj, char *p, int p_max_size)
 {
        double barnum;
-       read_exec(obj->data.s, p, p_max_size);
+       read_exec(obj->data.s, p, p_max_size, 1);
        barnum = get_barnum(p);
 
        if (barnum >= 0.0) {
@@ -455,7 +457,7 @@ void print_execibar(struct text_object *obj, char *p, int p_max_size)
                return;
 
        if (time_to_update(ed)) {
-               read_exec(ed->cmd, p, p_max_size);
+               read_exec(ed->cmd, p, p_max_size, 1);
                barnum = get_barnum(p);
 
                if (barnum >= 0.0) {