Make $user_time, $mpd_elapsed and $mpd_length compatible with times_in_seconds
[monky] / src / users.c
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
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
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.
21  *
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/>.
28  *
29  */
30
31 #include "conky.h"
32 #include <utmp.h>
33 #include <time.h>
34 #include <unistd.h>
35
36 #define BUFLEN 512
37
38 static void user_name(char *ptr)
39 {
40         const struct utmp *usr = 0;
41
42         setutent();
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);
47                         }
48                 }
49         }
50 }
51 static void user_num(int *ptr)
52 {
53         const struct utmp *usr;
54         int users_num = 0;
55
56         setutent();
57         while ((usr = getutent()) != NULL) {
58                 if (usr->ut_type == USER_PROCESS) {
59                         ++users_num;
60                 }
61         }
62         *ptr = users_num;
63 }
64 static void user_term(char *ptr)
65 {
66         const struct utmp *usr;
67
68         setutent();
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);
73                         }
74                 }
75         }
76 }
77 static void user_time(char *ptr)
78 {
79         const struct utmp *usr;
80         time_t log_in, real, diff;
81         char buf[BUFLEN] = "";
82
83         setutent();
84         while ((usr = getutent()) != NULL) {
85                 if (usr->ut_type == USER_PROCESS) {
86                         log_in = usr->ut_time;
87                         time(&real);
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);
92                         }
93                 }
94         }
95 }
96 static void tty_user_time(char *ptr, char *tty, char times_in_seconds)
97 {
98         time_t real, diff, log_in;
99         char buf[BUFLEN] = "";
100
101         struct utmp *usr, line;
102
103         setutent();
104         strcpy(line.ut_line, tty);
105         usr = getutline(&line);
106         if (usr == NULL ) {
107                 return;
108         }
109
110         log_in = usr->ut_time;
111
112         time(&real);
113         diff = difftime(real, log_in);
114         if(times_in_seconds) {
115                 snprintf(buf, BUFLEN, "%d", (int) diff);
116         } else {
117                 format_seconds(buf, BUFLEN, diff);
118         }
119         strncpy(ptr, buf, BUFLEN-1);
120 }
121
122 static void users_alloc(struct information *ptr)
123 {
124         if (ptr->users.names == NULL) {
125                 ptr->users.names = malloc(text_buffer_size);
126
127         }
128         if (ptr->users.terms == NULL) {
129                 ptr->users.terms = malloc(text_buffer_size);
130         }
131         if (ptr->users.times == NULL) {
132                 ptr->users.times = malloc(text_buffer_size);
133         }
134 }
135
136 void update_user_time(char *tty, char times_in_seconds)
137 {
138         struct information *current_info = &info;
139         char temp[BUFLEN] = "";
140
141         if (current_info->users.ctime == NULL) {
142                 current_info->users.ctime = malloc(text_buffer_size);
143         }
144
145         tty_user_time(temp, tty, times_in_seconds);
146
147         if (temp != NULL) {
148                 if (current_info->users.ctime) {
149                         free(current_info->users.ctime);
150                         current_info->users.ctime = 0;
151                 }
152                 current_info->users.ctime = malloc(text_buffer_size);
153                 strncpy(current_info->users.ctime, temp, text_buffer_size);
154         } else {
155                 if (current_info->users.ctime) {
156                         free(current_info->users.ctime);
157                         current_info->users.ctime = 0;
158                 }
159                 current_info->users.ctime = malloc(text_buffer_size);
160                 strncpy(current_info->users.ctime, "broken", text_buffer_size);
161         }
162 }
163
164 void update_users(void)
165 {
166         struct information *current_info = &info;
167         char temp[BUFLEN] = "";
168         int t;
169         users_alloc(current_info);
170         user_name(temp);
171         if (temp != NULL) {
172                 if (current_info->users.names) {
173                         free(current_info->users.names);
174                         current_info->users.names = 0;
175                 }
176                 current_info->users.names = malloc(text_buffer_size);
177                 strncpy(current_info->users.names, temp, text_buffer_size);
178         } else {
179                 if (current_info->users.names) {
180                         free(current_info->users.names);
181                         current_info->users.names = 0;
182                 }
183                 current_info->users.names = malloc(text_buffer_size);
184                 strncpy(current_info->users.names, "broken", text_buffer_size);
185         }
186         user_num(&t);
187         if (t != 0) {
188                 if (current_info->users.number) {
189                         current_info->users.number = 0;
190                 }
191                 current_info->users.number = t;
192         } else {
193                 current_info->users.number = 0;
194         }
195         temp[0] = 0;
196         user_term(temp);
197         if (temp != NULL) {
198                 if (current_info->users.terms) {
199                         free(current_info->users.terms);
200                         current_info->users.terms = 0;
201                 }
202                 current_info->users.terms = malloc(text_buffer_size);
203                 strncpy(current_info->users.terms, temp, text_buffer_size);
204         } else {
205                 if (current_info->users.terms) {
206                         free(current_info->users.terms);
207                         current_info->users.terms = 0;
208                 }
209                 current_info->users.terms = malloc(text_buffer_size);
210                 strncpy(current_info->users.terms, "broken", text_buffer_size);
211         }
212         user_time(temp);
213         if (temp != NULL) {
214                 if (current_info->users.times) {
215                         free(current_info->users.times);
216                         current_info->users.times = 0;
217                 }
218                 current_info->users.times = malloc(text_buffer_size);
219                 strncpy(current_info->users.times, temp, text_buffer_size);
220         } else {
221                 if (current_info->users.times) {
222                         free(current_info->users.times);
223                         current_info->users.times = 0;
224                 }
225                 current_info->users.times = malloc(text_buffer_size);
226                 strncpy(current_info->users.times, "broken", text_buffer_size);
227         }
228 }