-/* Conky, a system monitor, based on torsmo
+/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
+ * vim: ts=4 sw=4 noet ai cindent syntax=c
+ *
+ * Conky, a system monitor, based on torsmo
*
* Any original torsmo code is licensed under the BSD license
*
* Please see COPYING for details
*
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
- * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* 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 "conky.h"
#include <utmp.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
#include <time.h>
+#include <unistd.h>
-static void user_name(char **ptr) {
- const struct utmp *usr;
- char buf[512];
+#define BUFLEN 512
+
+static void user_name(char *ptr)
+{
+ const struct utmp *usr = 0;
setutent();
- while((usr=getutent())!=NULL) {
- if (usr->ut_type==USER_PROCESS) {
- strncat(buf, usr->ut_name, 9); strcat(buf, "\n");
+ while ((usr = getutent()) != NULL) {
+ if (usr->ut_type == USER_PROCESS) {
+ if (strlen(ptr) + strlen(usr->ut_name) + 1 <= BUFLEN) {
+ strncat(ptr, usr->ut_name, UT_NAMESIZE);
+ }
}
}
- *ptr = buf;
}
-static void user_num(int *ptr) {
+static void user_num(int *ptr)
+{
const struct utmp *usr;
int users_num = 0;
setutent();
- while ((usr=getutent())!=NULL) {
- if (usr->ut_type==USER_PROCESS) {
+ while ((usr = getutent()) != NULL) {
+ if (usr->ut_type == USER_PROCESS) {
++users_num;
}
}
*ptr = users_num;
}
-static void user_term(char **ptr) {
+static void user_term(char *ptr)
+{
const struct utmp *usr;
- char buf[512];
setutent();
- while((usr=getutent())!=NULL) {
- if (usr->ut_type==USER_PROCESS) {
- strncat(buf, usr->ut_line, 13); strncat(buf, "\n", 3);
+ while ((usr = getutent()) != NULL) {
+ if (usr->ut_type == USER_PROCESS) {
+ if (strlen(ptr) + strlen(usr->ut_line) + 1 <= BUFLEN) {
+ strncat(ptr, usr->ut_line, UT_LINESIZE);
+ }
}
}
- *ptr = buf;
}
-static void user_time(char **ptr) {
+static void user_time(char *ptr)
+{
const struct utmp *usr;
- time_t login, real, diff;
- struct tm *dtime;
- char buf[512] = "";
- char output[512] = "";
+ time_t log_in, real, diff;
+ char buf[BUFLEN] = "";
setutent();
- while ((usr=getutent())!=NULL) {
- if (usr->ut_type==USER_PROCESS) {
- login=usr->ut_time;
+ while ((usr = getutent()) != NULL) {
+ if (usr->ut_type == USER_PROCESS) {
+ log_in = usr->ut_time;
time(&real);
- diff = difftime(real, login);
- dtime = localtime(&diff);
- dtime->tm_year = dtime->tm_year-70;
- dtime->tm_mon = dtime->tm_mon-1;
- dtime->tm_mday = dtime->tm_mday-1;
- if(dtime->tm_year>0){strftime(buf,512,"%yy %mm %dd %Hh %Mm\n", dtime); goto end;}
- else if(dtime->tm_mon>0){strftime(buf,512,"%mm %dd %Hh %Mm\n", dtime); goto end;}
- else if(dtime->tm_mday>0){strftime(buf,512,"%dd %Hh %Mm\n", dtime); goto end;}
- else if(dtime->tm_hour>0){strftime(buf,512,"%Hh %Mm\n", dtime); goto end;}
- else if(dtime->tm_min>0){strftime(buf,512,"%Mm\n", dtime); goto end;}
-end:
- strncat(output, buf, 512);
+ diff = difftime(real, log_in);
+ format_seconds(buf, BUFLEN, diff);
+ if (strlen(ptr) + strlen(buf) + 1 <= BUFLEN) {
+ strncat(ptr, buf, BUFLEN-strlen(ptr)-1);
+ }
}
}
- *ptr = output;
+}
+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) {
+static void users_alloc(struct information *ptr)
+{
if (ptr->users.names == NULL) {
- ptr->users.names = malloc(TEXT_BUFFER_SIZE);
+ ptr->users.names = malloc(text_buffer_size);
}
if (ptr->users.terms == NULL) {
- ptr->users.terms = malloc(TEXT_BUFFER_SIZE);
+ ptr->users.terms = malloc(text_buffer_size);
}
if (ptr->users.times == NULL) {
- ptr->users.times = malloc(TEXT_BUFFER_SIZE);
+ ptr->users.times = malloc(text_buffer_size);
+ }
+}
+
+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);
}
}
-void update_users() {
- struct information * current_info = &info;
- char *temp;
+int update_users(void)
+{
+ struct information *current_info = &info;
+ char temp[BUFLEN] = "";
int t;
users_alloc(current_info);
- user_name(&temp);
- if (temp!=NULL) {
+ user_name(temp);
+ if (temp != NULL) {
if (current_info->users.names) {
- free(current_info->users.names); current_info->users.names = 0;
+ free(current_info->users.names);
+ current_info->users.names = 0;
}
- current_info->users.names = malloc(TEXT_BUFFER_SIZE);
- strncpy(current_info->users.names, temp, TEXT_BUFFER_SIZE);
+ current_info->users.names = malloc(text_buffer_size);
+ strncpy(current_info->users.names, temp, text_buffer_size);
} else {
if (current_info->users.names) {
- free(current_info->users.names); current_info->users.names = 0;
+ free(current_info->users.names);
+ current_info->users.names = 0;
}
- current_info->users.names = malloc(TEXT_BUFFER_SIZE);
- strncpy(current_info->users.names, "broken", TEXT_BUFFER_SIZE);
+ current_info->users.names = malloc(text_buffer_size);
+ strncpy(current_info->users.names, "broken", text_buffer_size);
}
user_num(&t);
- if (t!=0) {
+ if (t != 0) {
if (current_info->users.number) {
current_info->users.number = 0;
}
} else {
current_info->users.number = 0;
}
- temp = "\0";
- user_term(&temp);
- if (temp!=NULL) {
+ temp[0] = 0;
+ user_term(temp);
+ if (temp != NULL) {
if (current_info->users.terms) {
- free(current_info->users.terms); current_info->users.terms = 0;
+ free(current_info->users.terms);
+ current_info->users.terms = 0;
}
- current_info->users.terms = malloc(TEXT_BUFFER_SIZE);
- strncpy(current_info->users.terms, temp, TEXT_BUFFER_SIZE);
+ current_info->users.terms = malloc(text_buffer_size);
+ strncpy(current_info->users.terms, temp, text_buffer_size);
} else {
if (current_info->users.terms) {
- free(current_info->users.terms); current_info->users.terms = 0;
+ free(current_info->users.terms);
+ current_info->users.terms = 0;
}
- current_info->users.terms = malloc(TEXT_BUFFER_SIZE);
- strncpy(current_info->users.terms, "broken", TEXT_BUFFER_SIZE);
+ current_info->users.terms = malloc(text_buffer_size);
+ strncpy(current_info->users.terms, "broken", text_buffer_size);
}
- user_time(&temp);
- if (temp!=NULL) {
+ user_time(temp);
+ if (temp != NULL) {
if (current_info->users.times) {
- free(current_info->users.times); current_info->users.times = 0;
+ free(current_info->users.times);
+ current_info->users.times = 0;
}
- current_info->users.times = malloc(TEXT_BUFFER_SIZE);
- strncpy(current_info->users.times, temp, TEXT_BUFFER_SIZE);
+ current_info->users.times = malloc(text_buffer_size);
+ strncpy(current_info->users.times, temp, text_buffer_size);
} else {
if (current_info->users.times) {
- free(current_info->users.times); current_info->users.times = 0;
+ free(current_info->users.times);
+ current_info->users.times = 0;
}
- current_info->users.times = malloc(TEXT_BUFFER_SIZE);
- strncpy(current_info->users.times, "broken", TEXT_BUFFER_SIZE);
+ current_info->users.times = malloc(text_buffer_size);
+ strncpy(current_info->users.times, "broken", text_buffer_size);
}
+ return 0;
}