cd3c01e83a3da5511b32f4bf2242784b51e3b39c
[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-2010 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)
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         format_seconds(buf, BUFLEN, diff);
115         strncpy(ptr, buf, BUFLEN-1);
116 }
117
118 static void users_alloc(struct information *ptr)
119 {
120         if (ptr->users.names == NULL) {
121                 ptr->users.names = malloc(text_buffer_size);
122
123         }
124         if (ptr->users.terms == NULL) {
125                 ptr->users.terms = malloc(text_buffer_size);
126         }
127         if (ptr->users.times == NULL) {
128                 ptr->users.times = malloc(text_buffer_size);
129         }
130 }
131
132 void update_user_time(char *tty)
133 {
134         struct information *current_info = &info;
135         char temp[BUFLEN] = "";
136
137         if (current_info->users.ctime == NULL) {
138                 current_info->users.ctime = malloc(text_buffer_size);
139         }
140
141         tty_user_time(temp, tty);
142
143         if (temp != NULL) {
144                 if (current_info->users.ctime) {
145                         free(current_info->users.ctime);
146                         current_info->users.ctime = 0;
147                 }
148                 current_info->users.ctime = malloc(text_buffer_size);
149                 strncpy(current_info->users.ctime, temp, text_buffer_size);
150         } else {
151                 if (current_info->users.ctime) {
152                         free(current_info->users.ctime);
153                         current_info->users.ctime = 0;
154                 }
155                 current_info->users.ctime = malloc(text_buffer_size);
156                 strncpy(current_info->users.ctime, "broken", text_buffer_size);
157         }
158 }
159
160 void update_users(void)
161 {
162         struct information *current_info = &info;
163         char temp[BUFLEN] = "";
164         int t;
165         users_alloc(current_info);
166         user_name(temp);
167         if (temp != NULL) {
168                 if (current_info->users.names) {
169                         free(current_info->users.names);
170                         current_info->users.names = 0;
171                 }
172                 current_info->users.names = malloc(text_buffer_size);
173                 strncpy(current_info->users.names, temp, text_buffer_size);
174         } else {
175                 if (current_info->users.names) {
176                         free(current_info->users.names);
177                         current_info->users.names = 0;
178                 }
179                 current_info->users.names = malloc(text_buffer_size);
180                 strncpy(current_info->users.names, "broken", text_buffer_size);
181         }
182         user_num(&t);
183         if (t != 0) {
184                 if (current_info->users.number) {
185                         current_info->users.number = 0;
186                 }
187                 current_info->users.number = t;
188         } else {
189                 current_info->users.number = 0;
190         }
191         temp[0] = 0;
192         user_term(temp);
193         if (temp != NULL) {
194                 if (current_info->users.terms) {
195                         free(current_info->users.terms);
196                         current_info->users.terms = 0;
197                 }
198                 current_info->users.terms = malloc(text_buffer_size);
199                 strncpy(current_info->users.terms, temp, text_buffer_size);
200         } else {
201                 if (current_info->users.terms) {
202                         free(current_info->users.terms);
203                         current_info->users.terms = 0;
204                 }
205                 current_info->users.terms = malloc(text_buffer_size);
206                 strncpy(current_info->users.terms, "broken", text_buffer_size);
207         }
208         user_time(temp);
209         if (temp != NULL) {
210                 if (current_info->users.times) {
211                         free(current_info->users.times);
212                         current_info->users.times = 0;
213                 }
214                 current_info->users.times = malloc(text_buffer_size);
215                 strncpy(current_info->users.times, temp, text_buffer_size);
216         } else {
217                 if (current_info->users.times) {
218                         free(current_info->users.times);
219                         current_info->users.times = 0;
220                 }
221                 current_info->users.times = malloc(text_buffer_size);
222                 strncpy(current_info->users.times, "broken", text_buffer_size);
223         }
224 }