Fix for segfault in top_name stuff.
[monky] / src / users.c
index f0421c0..5ba7959 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.
  *
@@ -31,6 +31,7 @@
 #include "conky.h"
 #include <utmp.h>
 #include <time.h>
+#include <unistd.h>
 
 #define BUFLEN 512
 
@@ -92,6 +93,27 @@ static void user_time(char *ptr)
                }
        }
 }
+static void tty_user_time(char *ptr, char *tty)
+{
+       time_t real, diff, log_in;
+       char buf[BUFLEN] = "";
+
+       struct utmp *usr, line;
+
+       setutent();
+       strcpy(line.ut_line, tty);
+       usr = getutline(&line);
+       if (usr == NULL ) {
+               return;
+       }
+
+       log_in = usr->ut_time;
+
+       time(&real);
+       diff = difftime(real, log_in);
+       format_seconds(buf, BUFLEN, diff);
+       strncpy(ptr, buf, BUFLEN-1);
+}
 
 static void users_alloc(struct information *ptr)
 {
@@ -107,7 +129,35 @@ static void users_alloc(struct information *ptr)
        }
 }
 
-void update_users(void)
+void update_user_time(char *tty)
+{
+       struct information *current_info = &info;
+       char temp[BUFLEN] = "";
+
+       if (current_info->users.ctime == NULL) {
+               current_info->users.ctime = malloc(text_buffer_size);
+       }
+
+       tty_user_time(temp, tty);
+
+       if (temp != NULL) {
+               if (current_info->users.ctime) {
+                       free(current_info->users.ctime);
+                       current_info->users.ctime = 0;
+               }
+               current_info->users.ctime = malloc(text_buffer_size);
+               strncpy(current_info->users.ctime, temp, text_buffer_size);
+       } else {
+               if (current_info->users.ctime) {
+                       free(current_info->users.ctime);
+                       current_info->users.ctime = 0;
+               }
+               current_info->users.ctime = malloc(text_buffer_size);
+               strncpy(current_info->users.ctime, "broken", text_buffer_size);
+       }
+}
+
+int update_users(void)
 {
        struct information *current_info = &info;
        char temp[BUFLEN] = "";
@@ -171,4 +221,5 @@ void update_users(void)
                current_info->users.times = malloc(text_buffer_size);
                strncpy(current_info->users.times, "broken", text_buffer_size);
        }
+       return 0;
 }