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-2010 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)
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 format_seconds(buf, BUFLEN, diff);
115 strncpy(ptr, buf, BUFLEN-1);
118 static void users_alloc(struct information *ptr)
120 if (ptr->users.names == NULL) {
121 ptr->users.names = malloc(text_buffer_size);
124 if (ptr->users.terms == NULL) {
125 ptr->users.terms = malloc(text_buffer_size);
127 if (ptr->users.times == NULL) {
128 ptr->users.times = malloc(text_buffer_size);
132 void update_user_time(char *tty)
134 struct information *current_info = &info;
135 char temp[BUFLEN] = "";
137 if (current_info->users.ctime == NULL) {
138 current_info->users.ctime = malloc(text_buffer_size);
141 tty_user_time(temp, tty);
144 if (current_info->users.ctime) {
145 free(current_info->users.ctime);
146 current_info->users.ctime = 0;
148 current_info->users.ctime = malloc(text_buffer_size);
149 strncpy(current_info->users.ctime, temp, text_buffer_size);
151 if (current_info->users.ctime) {
152 free(current_info->users.ctime);
153 current_info->users.ctime = 0;
155 current_info->users.ctime = malloc(text_buffer_size);
156 strncpy(current_info->users.ctime, "broken", text_buffer_size);
160 void update_users(void)
162 struct information *current_info = &info;
163 char temp[BUFLEN] = "";
165 users_alloc(current_info);
168 if (current_info->users.names) {
169 free(current_info->users.names);
170 current_info->users.names = 0;
172 current_info->users.names = malloc(text_buffer_size);
173 strncpy(current_info->users.names, temp, text_buffer_size);
175 if (current_info->users.names) {
176 free(current_info->users.names);
177 current_info->users.names = 0;
179 current_info->users.names = malloc(text_buffer_size);
180 strncpy(current_info->users.names, "broken", text_buffer_size);
184 if (current_info->users.number) {
185 current_info->users.number = 0;
187 current_info->users.number = t;
189 current_info->users.number = 0;
194 if (current_info->users.terms) {
195 free(current_info->users.terms);
196 current_info->users.terms = 0;
198 current_info->users.terms = malloc(text_buffer_size);
199 strncpy(current_info->users.terms, temp, text_buffer_size);
201 if (current_info->users.terms) {
202 free(current_info->users.terms);
203 current_info->users.terms = 0;
205 current_info->users.terms = malloc(text_buffer_size);
206 strncpy(current_info->users.terms, "broken", text_buffer_size);
210 if (current_info->users.times) {
211 free(current_info->users.times);
212 current_info->users.times = 0;
214 current_info->users.times = malloc(text_buffer_size);
215 strncpy(current_info->users.times, temp, text_buffer_size);
217 if (current_info->users.times) {
218 free(current_info->users.times);
219 current_info->users.times = 0;
221 current_info->users.times = malloc(text_buffer_size);
222 strncpy(current_info->users.times, "broken", text_buffer_size);