1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 static void user_name(char *ptr)
38 const struct utmp *usr = 0;
41 while ((usr = getutent()) != NULL) {
42 if (usr->ut_type == USER_PROCESS) {
43 //TODO change the magic number 9 into a constant, does anybody know where it comes from ?
44 if (strlen(ptr) + (strlen(usr->ut_name) > 9 ? 9 : strlen(usr->ut_name) ) + 1 <= BUFLEN) {
45 strncat(ptr, usr->ut_name, 9);
50 static void user_num(int *ptr)
52 const struct utmp *usr;
56 while ((usr = getutent()) != NULL) {
57 if (usr->ut_type == USER_PROCESS) {
63 static void user_term(char *ptr)
65 const struct utmp *usr;
68 while ((usr = getutent()) != NULL) {
69 if (usr->ut_type == USER_PROCESS) {
70 //TODO change the magic number 13 into a constant, does anybody know where it comes from ?
71 if (strlen(ptr) + (strlen(usr->ut_line) > 13 ? 13 : strlen(usr->ut_line) ) + 1 <= BUFLEN) {
72 strncat(ptr, usr->ut_line, 13);
77 static void user_time(char *ptr)
79 const struct utmp *usr;
80 time_t log_in, real, diff;
82 char buf[BUFLEN] = "";
85 while ((usr = getutent()) != NULL) {
86 if (usr->ut_type == USER_PROCESS) {
87 log_in = usr->ut_time;
89 diff = difftime(real, log_in);
90 dtime = localtime(&diff);
91 dtime->tm_year = dtime->tm_year - 70;
92 dtime->tm_mon = dtime->tm_mon - 1;
93 dtime->tm_mday = dtime->tm_mday - 1;
94 if (dtime->tm_year > 0) {
95 strftime(buf, BUFLEN, "%yy %mm %dd %Hh %Mm", dtime);
96 } else if (dtime->tm_mon > 0) {
97 strftime(buf, BUFLEN, "%mm %dd %Hh %Mm", dtime);
98 } else if (dtime->tm_mday > 0) {
99 strftime(buf, BUFLEN, "%dd %Hh %Mm", dtime);
100 } else if (dtime->tm_hour > 0) {
101 strftime(buf, BUFLEN, "%Hh %Mm", dtime);
102 } else if (dtime->tm_min > 0) {
103 strftime(buf, BUFLEN, "%Mm", dtime);
105 if (strlen(ptr) + strlen(buf) + 1 <= BUFLEN) {
106 strncat(ptr, buf, BUFLEN);
112 static void users_alloc(struct information *ptr)
114 if (ptr->users.names == NULL) {
115 ptr->users.names = malloc(text_buffer_size);
118 if (ptr->users.terms == NULL) {
119 ptr->users.terms = malloc(text_buffer_size);
121 if (ptr->users.times == NULL) {
122 ptr->users.times = malloc(text_buffer_size);
126 void update_users(void)
128 struct information *current_info = &info;
129 char temp[BUFLEN] = "";
131 users_alloc(current_info);
134 if (current_info->users.names) {
135 free(current_info->users.names);
136 current_info->users.names = 0;
138 current_info->users.names = malloc(text_buffer_size);
139 strncpy(current_info->users.names, temp, text_buffer_size);
141 if (current_info->users.names) {
142 free(current_info->users.names);
143 current_info->users.names = 0;
145 current_info->users.names = malloc(text_buffer_size);
146 strncpy(current_info->users.names, "broken", text_buffer_size);
150 if (current_info->users.number) {
151 current_info->users.number = 0;
153 current_info->users.number = t;
155 current_info->users.number = 0;
160 if (current_info->users.terms) {
161 free(current_info->users.terms);
162 current_info->users.terms = 0;
164 current_info->users.terms = malloc(text_buffer_size);
165 strncpy(current_info->users.terms, temp, text_buffer_size);
167 if (current_info->users.terms) {
168 free(current_info->users.terms);
169 current_info->users.terms = 0;
171 current_info->users.terms = malloc(text_buffer_size);
172 strncpy(current_info->users.terms, "broken", text_buffer_size);
176 if (current_info->users.times) {
177 free(current_info->users.times);
178 current_info->users.times = 0;
180 current_info->users.times = malloc(text_buffer_size);
181 strncpy(current_info->users.times, temp, text_buffer_size);
183 if (current_info->users.times) {
184 free(current_info->users.times);
185 current_info->users.times = 0;
187 current_info->users.times = malloc(text_buffer_size);
188 strncpy(current_info->users.times, "broken", text_buffer_size);