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-2009 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/>.
34 static void user_name(char *ptr)
36 const struct utmp *usr = 0;
39 while ((usr = getutent()) != NULL) {
40 if (usr->ut_type == USER_PROCESS) {
41 if (strlen(ptr) + strlen(usr->ut_name) + 1 <= BUFLEN) {
42 strncat(ptr, usr->ut_name, UT_NAMESIZE);
47 static void user_num(int *ptr)
49 const struct utmp *usr;
53 while ((usr = getutent()) != NULL) {
54 if (usr->ut_type == USER_PROCESS) {
60 static void user_term(char *ptr)
62 const struct utmp *usr;
65 while ((usr = getutent()) != NULL) {
66 if (usr->ut_type == USER_PROCESS) {
67 if (strlen(ptr) + strlen(usr->ut_line) + 1 <= BUFLEN) {
68 strncat(ptr, usr->ut_line, UT_LINESIZE);
73 static void user_time(char *ptr)
75 const struct utmp *usr;
76 time_t log_in, real, diff;
78 char buf[BUFLEN] = "";
81 while ((usr = getutent()) != NULL) {
82 if (usr->ut_type == USER_PROCESS) {
83 log_in = usr->ut_time;
85 diff = difftime(real, log_in);
86 dtime = localtime(&diff);
87 dtime->tm_year = dtime->tm_year - 70;
88 dtime->tm_mon = dtime->tm_mon - 1;
89 dtime->tm_mday = dtime->tm_mday - 1;
90 if (dtime->tm_year > 0) {
91 strftime(buf, BUFLEN, "%yy %mm %dd %Hh %Mm", dtime);
92 } else if (dtime->tm_mon > 0) {
93 strftime(buf, BUFLEN, "%mm %dd %Hh %Mm", dtime);
94 } else if (dtime->tm_mday > 0) {
95 strftime(buf, BUFLEN, "%dd %Hh %Mm", dtime);
96 } else if (dtime->tm_hour > 0) {
97 strftime(buf, BUFLEN, "%Hh %Mm", dtime);
98 } else if (dtime->tm_min > 0) {
99 strftime(buf, BUFLEN, "%Mm", dtime);
101 if (strlen(ptr) + strlen(buf) + 1 <= BUFLEN) {
102 strncat(ptr, buf, BUFLEN-strlen(ptr)-1);
108 static void users_alloc(struct information *ptr)
110 if (ptr->users.names == NULL) {
111 ptr->users.names = malloc(text_buffer_size);
114 if (ptr->users.terms == NULL) {
115 ptr->users.terms = malloc(text_buffer_size);
117 if (ptr->users.times == NULL) {
118 ptr->users.times = malloc(text_buffer_size);
122 void update_users(void)
124 struct information *current_info = &info;
125 char temp[BUFLEN] = "";
127 users_alloc(current_info);
130 if (current_info->users.names) {
131 free(current_info->users.names);
132 current_info->users.names = 0;
134 current_info->users.names = malloc(text_buffer_size);
135 strncpy(current_info->users.names, temp, text_buffer_size);
137 if (current_info->users.names) {
138 free(current_info->users.names);
139 current_info->users.names = 0;
141 current_info->users.names = malloc(text_buffer_size);
142 strncpy(current_info->users.names, "broken", text_buffer_size);
146 if (current_info->users.number) {
147 current_info->users.number = 0;
149 current_info->users.number = t;
151 current_info->users.number = 0;
156 if (current_info->users.terms) {
157 free(current_info->users.terms);
158 current_info->users.terms = 0;
160 current_info->users.terms = malloc(text_buffer_size);
161 strncpy(current_info->users.terms, temp, text_buffer_size);
163 if (current_info->users.terms) {
164 free(current_info->users.terms);
165 current_info->users.terms = 0;
167 current_info->users.terms = malloc(text_buffer_size);
168 strncpy(current_info->users.terms, "broken", text_buffer_size);
172 if (current_info->users.times) {
173 free(current_info->users.times);
174 current_info->users.times = 0;
176 current_info->users.times = malloc(text_buffer_size);
177 strncpy(current_info->users.times, temp, text_buffer_size);
179 if (current_info->users.times) {
180 free(current_info->users.times);
181 current_info->users.times = 0;
183 current_info->users.times = malloc(text_buffer_size);
184 strncpy(current_info->users.times, "broken", text_buffer_size);