1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
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);
97 static void users_alloc(struct information *ptr)
99 if (ptr->users.names == NULL) {
100 ptr->users.names = malloc(text_buffer_size);
103 if (ptr->users.terms == NULL) {
104 ptr->users.terms = malloc(text_buffer_size);
106 if (ptr->users.times == NULL) {
107 ptr->users.times = malloc(text_buffer_size);
111 void update_users(void)
113 struct information *current_info = &info;
114 char temp[BUFLEN] = "";
116 users_alloc(current_info);
119 if (current_info->users.names) {
120 free(current_info->users.names);
121 current_info->users.names = 0;
123 current_info->users.names = malloc(text_buffer_size);
124 strncpy(current_info->users.names, temp, text_buffer_size);
126 if (current_info->users.names) {
127 free(current_info->users.names);
128 current_info->users.names = 0;
130 current_info->users.names = malloc(text_buffer_size);
131 strncpy(current_info->users.names, "broken", text_buffer_size);
135 if (current_info->users.number) {
136 current_info->users.number = 0;
138 current_info->users.number = t;
140 current_info->users.number = 0;
145 if (current_info->users.terms) {
146 free(current_info->users.terms);
147 current_info->users.terms = 0;
149 current_info->users.terms = malloc(text_buffer_size);
150 strncpy(current_info->users.terms, temp, text_buffer_size);
152 if (current_info->users.terms) {
153 free(current_info->users.terms);
154 current_info->users.terms = 0;
156 current_info->users.terms = malloc(text_buffer_size);
157 strncpy(current_info->users.terms, "broken", text_buffer_size);
161 if (current_info->users.times) {
162 free(current_info->users.times);
163 current_info->users.times = 0;
165 current_info->users.times = malloc(text_buffer_size);
166 strncpy(current_info->users.times, temp, text_buffer_size);
168 if (current_info->users.times) {
169 free(current_info->users.times);
170 current_info->users.times = 0;
172 current_info->users.times = malloc(text_buffer_size);
173 strncpy(current_info->users.times, "broken", text_buffer_size);