1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 static void user_name(char *ptr)
40 const struct utmp *usr = 0;
43 while ((usr = getutent()) != NULL) {
44 if (usr->ut_type == USER_PROCESS) {
45 if (strlen(ptr) + strlen(usr->ut_name) + 1 <= BUFLEN) {
46 strncat(ptr, usr->ut_name, UT_NAMESIZE);
51 static void user_num(int *ptr)
53 const struct utmp *usr;
57 while ((usr = getutent()) != NULL) {
58 if (usr->ut_type == USER_PROCESS) {
64 static void user_term(char *ptr)
66 const struct utmp *usr;
69 while ((usr = getutent()) != NULL) {
70 if (usr->ut_type == USER_PROCESS) {
71 if (strlen(ptr) + strlen(usr->ut_line) + 1 <= BUFLEN) {
72 strncat(ptr, usr->ut_line, UT_LINESIZE);
77 static void user_time(char *ptr)
79 const struct utmp *usr;
80 time_t log_in, real, diff;
81 char buf[BUFLEN] = "";
84 while ((usr = getutent()) != NULL) {
85 if (usr->ut_type == USER_PROCESS) {
86 log_in = usr->ut_time;
88 diff = difftime(real, log_in);
89 format_seconds(buf, BUFLEN, diff);
90 if (strlen(ptr) + strlen(buf) + 1 <= BUFLEN) {
91 strncat(ptr, buf, BUFLEN-strlen(ptr)-1);
96 static void tty_user_time(char *ptr, char *tty, char times_in_seconds)
98 time_t real, diff, log_in;
99 char buf[BUFLEN] = "";
101 struct utmp *usr, line;
104 strcpy(line.ut_line, tty);
105 usr = getutline(&line);
110 log_in = usr->ut_time;
113 diff = difftime(real, log_in);
114 if(times_in_seconds) {
115 snprintf(buf, BUFLEN, "%d", (int) diff);
117 format_seconds(buf, BUFLEN, diff);
119 strncpy(ptr, buf, BUFLEN-1);
122 static void users_alloc(struct information *ptr)
124 if (ptr->users.names == NULL) {
125 ptr->users.names = malloc(text_buffer_size);
128 if (ptr->users.terms == NULL) {
129 ptr->users.terms = malloc(text_buffer_size);
131 if (ptr->users.times == NULL) {
132 ptr->users.times = malloc(text_buffer_size);
136 void update_user_time(char *tty, char times_in_seconds)
138 struct information *current_info = &info;
139 char temp[BUFLEN] = "";
141 if (current_info->users.ctime == NULL) {
142 current_info->users.ctime = malloc(text_buffer_size);
145 tty_user_time(temp, tty, times_in_seconds);
148 if (current_info->users.ctime) {
149 free(current_info->users.ctime);
150 current_info->users.ctime = 0;
152 current_info->users.ctime = malloc(text_buffer_size);
153 strncpy(current_info->users.ctime, temp, text_buffer_size);
155 if (current_info->users.ctime) {
156 free(current_info->users.ctime);
157 current_info->users.ctime = 0;
159 current_info->users.ctime = malloc(text_buffer_size);
160 strncpy(current_info->users.ctime, "broken", text_buffer_size);
164 void update_users(void)
166 struct information *current_info = &info;
167 char temp[BUFLEN] = "";
169 users_alloc(current_info);
172 if (current_info->users.names) {
173 free(current_info->users.names);
174 current_info->users.names = 0;
176 current_info->users.names = malloc(text_buffer_size);
177 strncpy(current_info->users.names, temp, text_buffer_size);
179 if (current_info->users.names) {
180 free(current_info->users.names);
181 current_info->users.names = 0;
183 current_info->users.names = malloc(text_buffer_size);
184 strncpy(current_info->users.names, "broken", text_buffer_size);
188 if (current_info->users.number) {
189 current_info->users.number = 0;
191 current_info->users.number = t;
193 current_info->users.number = 0;
198 if (current_info->users.terms) {
199 free(current_info->users.terms);
200 current_info->users.terms = 0;
202 current_info->users.terms = malloc(text_buffer_size);
203 strncpy(current_info->users.terms, temp, text_buffer_size);
205 if (current_info->users.terms) {
206 free(current_info->users.terms);
207 current_info->users.terms = 0;
209 current_info->users.terms = malloc(text_buffer_size);
210 strncpy(current_info->users.terms, "broken", text_buffer_size);
214 if (current_info->users.times) {
215 free(current_info->users.times);
216 current_info->users.times = 0;
218 current_info->users.times = malloc(text_buffer_size);
219 strncpy(current_info->users.times, temp, text_buffer_size);
221 if (current_info->users.times) {
222 free(current_info->users.times);
223 current_info->users.times = 0;
225 current_info->users.times = malloc(text_buffer_size);
226 strncpy(current_info->users.times, "broken", text_buffer_size);