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);
96 static void users_alloc(struct information *ptr)
98 if (ptr->users.names == NULL) {
99 ptr->users.names = malloc(text_buffer_size);
102 if (ptr->users.terms == NULL) {
103 ptr->users.terms = malloc(text_buffer_size);
105 if (ptr->users.times == NULL) {
106 ptr->users.times = malloc(text_buffer_size);
110 void update_users(void)
112 struct information *current_info = &info;
113 char temp[BUFLEN] = "";
115 users_alloc(current_info);
118 if (current_info->users.names) {
119 free(current_info->users.names);
120 current_info->users.names = 0;
122 current_info->users.names = malloc(text_buffer_size);
123 strncpy(current_info->users.names, temp, text_buffer_size);
125 if (current_info->users.names) {
126 free(current_info->users.names);
127 current_info->users.names = 0;
129 current_info->users.names = malloc(text_buffer_size);
130 strncpy(current_info->users.names, "broken", text_buffer_size);
134 if (current_info->users.number) {
135 current_info->users.number = 0;
137 current_info->users.number = t;
139 current_info->users.number = 0;
144 if (current_info->users.terms) {
145 free(current_info->users.terms);
146 current_info->users.terms = 0;
148 current_info->users.terms = malloc(text_buffer_size);
149 strncpy(current_info->users.terms, temp, text_buffer_size);
151 if (current_info->users.terms) {
152 free(current_info->users.terms);
153 current_info->users.terms = 0;
155 current_info->users.terms = malloc(text_buffer_size);
156 strncpy(current_info->users.terms, "broken", text_buffer_size);
160 if (current_info->users.times) {
161 free(current_info->users.times);
162 current_info->users.times = 0;
164 current_info->users.times = malloc(text_buffer_size);
165 strncpy(current_info->users.times, temp, text_buffer_size);
167 if (current_info->users.times) {
168 free(current_info->users.times);
169 current_info->users.times = 0;
171 current_info->users.times = malloc(text_buffer_size);
172 strncpy(current_info->users.times, "broken", text_buffer_size);