Add $conky_user_time (sf.net #2830919)
[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
35 #define BUFLEN 512
36
37 static void user_name(char *ptr)
38 {
39         const struct utmp *usr = 0;
40
41         setutent();
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);
46                         }
47                 }
48         }
49 }
50 static void user_num(int *ptr)
51 {
52         const struct utmp *usr;
53         int users_num = 0;
54
55         setutent();
56         while ((usr = getutent()) != NULL) {
57                 if (usr->ut_type == USER_PROCESS) {
58                         ++users_num;
59                 }
60         }
61         *ptr = users_num;
62 }
63 static void user_term(char *ptr)
64 {
65         const struct utmp *usr;
66
67         setutent();
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);
72                         }
73                 }
74         }
75 }
76 static void user_time(char *ptr)
77 {
78         const struct utmp *usr;
79         time_t log_in, real, diff;
80         char buf[BUFLEN] = "";
81
82         setutent();
83         while ((usr = getutent()) != NULL) {
84                 if (usr->ut_type == USER_PROCESS) {
85                         log_in = usr->ut_time;
86                         time(&real);
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);
91                         }
92                 }
93         }
94 }
95 static void conky_user_time(char *ptr)
96 {
97         time_t real, diff;
98         static time_t log_in = 0;
99         char buf[BUFLEN] = "";
100
101         if (log_in == 0) {
102                 struct utmp *usr, line;
103                 char *real_tty_path = NULL;
104
105                 real_tty_path = ttyname(0);
106                 if (real_tty_path == NULL ) {
107                         return;
108                 }
109
110                 real_tty_path += 5; /* Remove "/dev/". */
111
112                 setutent();
113                 strcpy(line.ut_line, real_tty_path);
114                 usr = getutline(&line);
115                 if (usr == NULL ) {
116                         return;
117                 }
118
119                 log_in = usr->ut_time;
120         }
121         time(&real);
122         diff = difftime(real, log_in);
123         format_seconds(buf, BUFLEN, diff);
124         strncpy(ptr, buf, BUFLEN-1);
125 }
126
127 static void users_alloc(struct information *ptr)
128 {
129         if (ptr->users.names == NULL) {
130                 ptr->users.names = malloc(text_buffer_size);
131
132         }
133         if (ptr->users.terms == NULL) {
134                 ptr->users.terms = malloc(text_buffer_size);
135         }
136         if (ptr->users.times == NULL) {
137                 ptr->users.times = malloc(text_buffer_size);
138         }
139         if (ptr->users.ctime == NULL) {
140                 ptr->users.ctime = malloc(text_buffer_size);
141         }
142 }
143
144 void update_users(void)
145 {
146         struct information *current_info = &info;
147         char temp[BUFLEN] = "";
148         int t;
149         users_alloc(current_info);
150         user_name(temp);
151         temp[0] = 0;
152         conky_user_time(temp);
153         if (temp != NULL) {
154                 if (current_info->users.ctime) {
155                         free(current_info->users.ctime);
156                         current_info->users.ctime = 0;
157                 }
158                 current_info->users.ctime = malloc(text_buffer_size);
159                 strncpy(current_info->users.ctime, temp, text_buffer_size);
160         } else {
161                 if (current_info->users.ctime) {
162                         free(current_info->users.ctime);
163                         current_info->users.ctime = 0;
164                 }
165                 current_info->users.ctime = malloc(text_buffer_size);
166                 strncpy(current_info->users.ctime, "broken", text_buffer_size);
167         }
168         temp[0] = 0;
169         if (temp != NULL) {
170                 if (current_info->users.names) {
171                         free(current_info->users.names);
172                         current_info->users.names = 0;
173                 }
174                 current_info->users.names = malloc(text_buffer_size);
175                 strncpy(current_info->users.names, temp, text_buffer_size);
176         } else {
177                 if (current_info->users.names) {
178                         free(current_info->users.names);
179                         current_info->users.names = 0;
180                 }
181                 current_info->users.names = malloc(text_buffer_size);
182                 strncpy(current_info->users.names, "broken", text_buffer_size);
183         }
184         user_num(&t);
185         if (t != 0) {
186                 if (current_info->users.number) {
187                         current_info->users.number = 0;
188                 }
189                 current_info->users.number = t;
190         } else {
191                 current_info->users.number = 0;
192         }
193         temp[0] = 0;
194         user_term(temp);
195         if (temp != NULL) {
196                 if (current_info->users.terms) {
197                         free(current_info->users.terms);
198                         current_info->users.terms = 0;
199                 }
200                 current_info->users.terms = malloc(text_buffer_size);
201                 strncpy(current_info->users.terms, temp, text_buffer_size);
202         } else {
203                 if (current_info->users.terms) {
204                         free(current_info->users.terms);
205                         current_info->users.terms = 0;
206                 }
207                 current_info->users.terms = malloc(text_buffer_size);
208                 strncpy(current_info->users.terms, "broken", text_buffer_size);
209         }
210         user_time(temp);
211         if (temp != NULL) {
212                 if (current_info->users.times) {
213                         free(current_info->users.times);
214                         current_info->users.times = 0;
215                 }
216                 current_info->users.times = malloc(text_buffer_size);
217                 strncpy(current_info->users.times, temp, text_buffer_size);
218         } else {
219                 if (current_info->users.times) {
220                         free(current_info->users.times);
221                         current_info->users.times = 0;
222                 }
223                 current_info->users.times = malloc(text_buffer_size);
224                 strncpy(current_info->users.times, "broken", text_buffer_size);
225         }
226 }