here comes the big header include rewrite
[monky] / src / common.c
index 7875440..aff0bcd 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * $Id$ */
+ */
 
+#include "config.h"
 #include "conky.h"
+#include "fs.h"
+#include "logging.h"
 #include <ctype.h>
 #include <errno.h>
 #include <sys/time.h>
 #include <pthread.h>
 
+/* check for OS and include appropriate headers */
+#if defined(__linux__)
+#include "linux.h"
+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+#include "freebsd.h"
+#elif defined(__OpenBSD__)
+#include "openbsd.h"
+#endif
+
+/* OS specific prototypes to be implemented by linux.c & Co. */
+void update_entropy(void);
+
 #ifndef HAVE_STRNDUP
 // use our own strndup() if it's not available
 char *strndup(const char *s, size_t n)
 {
-       if (strlen(s) + 1 > n) {
-               char *ret = malloc(n);
+       if (strlen(s) > n) {
+               char *ret = malloc(n + 1);
                strncpy(ret, s, n);
+               ret[n] = 0;
                return ret;
        } else {
                return strdup(s);
@@ -182,7 +198,7 @@ void free_dns_data(void)
 
 //static double last_dns_update;
 
-void update_dns_data(void)
+static void update_dns_data(void)
 {
        FILE *fp;
        char line[256];
@@ -213,26 +229,43 @@ void update_dns_data(void)
        fclose(fp);
 }
 
-void format_seconds(char *buf, unsigned int n, long t)
+void format_seconds(char *buf, unsigned int n, long seconds)
 {
-       if (t >= 24 * 60 * 60) {        /* hours necessary when there are days? */
-               snprintf(buf, n, "%ldd %ldh %ldm", t / 60 / 60 / 24, (t / 60 / 60) % 24,
-                       (t / 60) % 60);
-       } else if (t >= 60 * 60) {
-               snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60);
+       long days;
+       int hours, minutes;
+
+       days = seconds / 86400;
+       seconds %= 86400;
+       hours = seconds / 3600;
+       seconds %= 3600;
+       minutes = seconds / 60;
+       seconds %= 60;
+
+       if (days > 0) {
+               snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
        } else {
-               snprintf(buf, n, "%ldm %lds", t / 60, t % 60);
+               snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
        }
 }
 
-void format_seconds_short(char *buf, unsigned int n, long t)
+void format_seconds_short(char *buf, unsigned int n, long seconds)
 {
-       if (t >= 24 * 60 * 60) {
-               snprintf(buf, n, "%ldd %ldh", t / 60 / 60 / 24, (t / 60 / 60) % 24);
-       } else if (t >= 60 * 60) {
-               snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60);
+       long days;
+       int hours, minutes;
+
+       days = seconds / 86400;
+       seconds %= 86400;
+       hours = seconds / 3600;
+       seconds %= 3600;
+       minutes = seconds / 60;
+       seconds %= 60;
+
+       if (days > 0) {
+               snprintf(buf, n, "%ldd %dh", days, hours);
+       } else if (hours > 0) {
+               snprintf(buf, n, "%dh %dm", hours, minutes);
        } else {
-               snprintf(buf, n, "%ldm", t / 60);
+               snprintf(buf, n, "%dm %lds", minutes, seconds);
        }
 }
 
@@ -240,6 +273,7 @@ static double last_meminfo_update;
 static double last_fs_update;
 
 unsigned long long need_mask;
+int no_buffers;
 
 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
 
@@ -313,6 +347,23 @@ void update_stuff(void)
        }
 #endif
 
+#ifdef MOC
+  if (NEED(INFO_MOC)) {
+    if (!info.moc.timed_thread) {
+      init_moc(&info.moc);
+      info.moc.timed_thread = timed_thread_create(&update_moc,
+        (void *) &info.moc, info.music_player_interval * 100000);
+      if (!info.moc.timed_thread) {
+        ERR("Failed to create MOC timed thread");
+      }
+      timed_thread_register(info.moc.timed_thread, &info.moc.timed_thread);
+      if (timed_thread_run(info.moc.timed_thread)) {
+        ERR("Failed to run MOC timed thread");
+      }
+    }
+  }
+#endif
+  
 #ifdef XMMS2
        if (NEED(INFO_XMMS2)) {
                update_xmms2();
@@ -340,9 +391,16 @@ void update_stuff(void)
                update_meminfo();
                if (no_buffers) {
                        info.mem -= info.bufmem;
+                       info.memeasyfree += info.bufmem;
                }
                last_meminfo_update = current_update_time;
        }
+       
+#ifdef X11
+       if (NEED(INFO_X11)) {
+               update_x11info();
+       }
+#endif
 
        if (NEED(INFO_TOP)) {
                update_top();
@@ -355,18 +413,20 @@ void update_stuff(void)
        }
 #ifdef TCP_PORT_MONITOR
        if (NEED(INFO_TCP_PORT_MONITOR)) {
-               update_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
+               tcp_portmon_update();
        }
 #endif
        if (NEED(INFO_ENTROPY)) {
                update_entropy();
        }
+#if defined(__linux__)
        if (NEED(INFO_USERS)) {
                update_users();
        }
        if (NEED(INFO_GW)) {
                update_gateway_info();
        }
+#endif /* __linux__ */
        if (NEED(INFO_DNS)) {
                update_dns_data();
        }