Fix segfault in mail folder (SF: 3007493)
[monky] / src / fs.c
index 8766a12..54617c6 100644 (file)
--- a/src/fs.c
+++ b/src/fs.c
@@ -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.
  *
@@ -31,6 +31,9 @@
 #include "conky.h"
 #include "logging.h"
 #include "fs.h"
+#include "specials.h"
+#include "text_object.h"
+#include <ctype.h>
 #include <unistd.h>
 #include <errno.h>
 #include <sys/types.h>
@@ -62,13 +65,13 @@ static void update_fs_stat(struct fs_stat *fs);
 
 void get_fs_type(const char *path, char *result);
 
-void update_fs_stats(void)
+int update_fs_stats(void)
 {
        unsigned i;
        static double last_fs_update = 0.0;
 
        if (current_update_time - last_fs_update < 13)
-               return;
+               return 0;
 
        for (i = 0; i < MAX_FS_STATS; ++i) {
                if (fs_stats[i].set) {
@@ -76,6 +79,7 @@ void update_fs_stats(void)
                }
        }
        last_fs_update = current_update_time;
+       return 0;
 }
 
 void clear_fs_stats(void)
@@ -191,3 +195,78 @@ void get_fs_type(const char *path, char *result)
        strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
 
 }
+
+void init_fs_bar(struct text_object *obj, const char *arg)
+{
+       arg = scan_bar(obj, arg);
+       if (arg) {
+               while (isspace(*arg)) {
+                       arg++;
+               }
+               if (*arg == '\0') {
+                       arg = "/";
+               }
+       } else {
+               arg = "/";
+       }
+       obj->data.opaque = prepare_fs_stat(arg);
+}
+
+void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size)
+{
+       double val = 1.0;
+       struct fs_stat *fs = obj->data.opaque;
+
+       if (!fs)
+               return;
+
+       if (fs->size)
+               val = (double)fs->avail / (double)fs->size;
+
+       if (!be_free_bar)
+               val = 1.0 - val;
+
+       new_bar(obj, p, p_max_size, (int)(255 * val));
+}
+
+void init_fs(struct text_object *obj, const char *arg)
+{
+       obj->data.opaque = prepare_fs_stat(arg ? arg : "/");
+}
+
+void print_fs_perc(struct text_object *obj, int be_free, char *p, int p_max_size)
+{
+       struct fs_stat *fs = obj->data.opaque;
+       int val = 100;
+
+       if (!fs)
+               return;
+
+       if (fs->size)
+               val = fs->avail * 100 / fs->size;
+
+       if (!be_free)
+               val = 100 - val;
+
+       percent_print(p, p_max_size, val);
+}
+
+#define HUMAN_PRINT_FS_GENERATOR(name, expr)                           \
+void print_fs_##name(struct text_object *obj, char *p, int p_max_size) \
+{                                                                      \
+       struct fs_stat *fs = obj->data.opaque;                             \
+       if (fs)                                                            \
+               human_readable(expr, p, p_max_size);                           \
+}
+
+HUMAN_PRINT_FS_GENERATOR(free, fs->avail)
+HUMAN_PRINT_FS_GENERATOR(size, fs->size)
+HUMAN_PRINT_FS_GENERATOR(used, fs->size - fs->free)
+
+void print_fs_type(struct text_object *obj, char *p, int p_max_size)
+{
+       struct fs_stat *fs = obj->data.opaque;
+
+       if (fs)
+               snprintf(p, p_max_size, "%s", fs->type);
+}