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/>.
37 static void user_name(char *ptr)
39 const struct utmp *usr = 0;
42 while ((usr = getutent()) != NULL) {
43 if (usr->ut_type == USER_PROCESS) {
44 if (strlen(ptr) + strlen(usr->ut_name) + 1 <= BUFLEN) {
45 strncat(ptr, usr->ut_name, UT_NAMESIZE);
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 if (strlen(ptr) + strlen(usr->ut_line) + 1 <= BUFLEN) {
71 strncat(ptr, usr->ut_line, UT_LINESIZE);
76 static void user_time(char *ptr)
78 const struct utmp *usr;
79 time_t log_in, real, diff;
80 char buf[BUFLEN] = "";
83 while ((usr = getutent()) != NULL) {
84 if (usr->ut_type == USER_PROCESS) {
85 log_in = usr->ut_time;
87 diff = difftime(real, log_in);
88 format_seconds(buf, BUFLEN, diff);
89 if (strlen(ptr) + strlen(buf) + 1 <= BUFLEN) {
90 strncat(ptr, buf, BUFLEN-strlen(ptr)-1);
95 static void conky_user_time(char *ptr)
98 static time_t log_in = 0;
99 char buf[BUFLEN] = "";
102 struct utmp *usr, line;
103 char *real_tty_path = NULL;
105 real_tty_path = ttyname(0);
106 if (real_tty_path == NULL ) {
110 real_tty_path += 5; /* Remove "/dev/". */
113 strcpy(line.ut_line, real_tty_path);
114 usr = getutline(&line);
119 log_in = usr->ut_time;
122 diff = difftime(real, log_in);
123 format_seconds(buf, BUFLEN, diff);
124 strncpy(ptr, buf, BUFLEN-1);
127 static void users_alloc(struct information *ptr)
129 if (ptr->users.names == NULL) {
130 ptr->users.names = malloc(text_buffer_size);
133 if (ptr->users.terms == NULL) {
134 ptr->users.terms = malloc(text_buffer_size);
136 if (ptr->users.times == NULL) {
137 ptr->users.times = malloc(text_buffer_size);
139 if (ptr->users.ctime == NULL) {
140 ptr->users.ctime = malloc(text_buffer_size);
144 void update_users(void)
146 struct information *current_info = &info;
147 char temp[BUFLEN] = "";
149 users_alloc(current_info);
152 conky_user_time(temp);
154 if (current_info->users.ctime) {
155 free(current_info->users.ctime);
156 current_info->users.ctime = 0;
158 current_info->users.ctime = malloc(text_buffer_size);
159 strncpy(current_info->users.ctime, temp, text_buffer_size);
161 if (current_info->users.ctime) {
162 free(current_info->users.ctime);
163 current_info->users.ctime = 0;
165 current_info->users.ctime = malloc(text_buffer_size);
166 strncpy(current_info->users.ctime, "broken", text_buffer_size);
170 if (current_info->users.names) {
171 free(current_info->users.names);
172 current_info->users.names = 0;
174 current_info->users.names = malloc(text_buffer_size);
175 strncpy(current_info->users.names, temp, text_buffer_size);
177 if (current_info->users.names) {
178 free(current_info->users.names);
179 current_info->users.names = 0;
181 current_info->users.names = malloc(text_buffer_size);
182 strncpy(current_info->users.names, "broken", text_buffer_size);
186 if (current_info->users.number) {
187 current_info->users.number = 0;
189 current_info->users.number = t;
191 current_info->users.number = 0;
196 if (current_info->users.terms) {
197 free(current_info->users.terms);
198 current_info->users.terms = 0;
200 current_info->users.terms = malloc(text_buffer_size);
201 strncpy(current_info->users.terms, temp, text_buffer_size);
203 if (current_info->users.terms) {
204 free(current_info->users.terms);
205 current_info->users.terms = 0;
207 current_info->users.terms = malloc(text_buffer_size);
208 strncpy(current_info->users.terms, "broken", text_buffer_size);
212 if (current_info->users.times) {
213 free(current_info->users.times);
214 current_info->users.times = 0;
216 current_info->users.times = malloc(text_buffer_size);
217 strncpy(current_info->users.times, temp, text_buffer_size);
219 if (current_info->users.times) {
220 free(current_info->users.times);
221 current_info->users.times = 0;
223 current_info->users.times = malloc(text_buffer_size);
224 strncpy(current_info->users.times, "broken", text_buffer_size);