b96c272652afa55fb4e54c5b9bf56eea5098327d
[monky] / src / users.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  * $Id$
27  *
28  */
29
30 #include "conky.h"
31 #include <utmp.h>
32 #include <time.h>
33
34 #define BUFLEN 512
35
36 static void user_name(char *ptr)
37 {
38         const struct utmp *usr = 0;
39
40         setutent();
41         while ((usr = getutent()) != NULL) {
42                 if (usr->ut_type == USER_PROCESS) {
43                         //TODO change the magic number 9 into a constant, does anybody know where it comes from ?
44                         if (strlen(ptr) + (strlen(usr->ut_name) > 9 ? 9 : strlen(usr->ut_name) ) + 1 <= BUFLEN) {
45                                 strncat(ptr, usr->ut_name, 9);
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                         //TODO change the magic number 13 into a constant, does anybody know where it comes from ?
71                         if (strlen(ptr) + (strlen(usr->ut_line) > 13 ? 13 : strlen(usr->ut_line) ) + 1 <= BUFLEN) {
72                                 strncat(ptr, usr->ut_line, 13);
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         struct tm *dtime;
82         char buf[BUFLEN] = "";
83
84         setutent();
85         while ((usr = getutent()) != NULL) {
86                 if (usr->ut_type == USER_PROCESS) {
87                         log_in = usr->ut_time;
88                         time(&real);
89                         diff = difftime(real, log_in);
90                         dtime = localtime(&diff);
91                         dtime->tm_year = dtime->tm_year - 70;
92                         dtime->tm_mon = dtime->tm_mon - 1;
93                         dtime->tm_mday = dtime->tm_mday - 1;
94                         if (dtime->tm_year > 0) {
95                                 strftime(buf, BUFLEN, "%yy %mm %dd %Hh %Mm", dtime);
96                         } else if (dtime->tm_mon > 0) {
97                                 strftime(buf, BUFLEN, "%mm %dd %Hh %Mm", dtime);
98                         } else if (dtime->tm_mday > 0) {
99                                 strftime(buf, BUFLEN, "%dd %Hh %Mm", dtime);
100                         } else if (dtime->tm_hour > 0) {
101                                 strftime(buf, BUFLEN, "%Hh %Mm", dtime);
102                         } else if (dtime->tm_min > 0) {
103                                 strftime(buf, BUFLEN, "%Mm", dtime);
104                         }
105                         if (strlen(ptr) + strlen(buf) + 1 <= BUFLEN) {
106                                 strncat(ptr, buf, BUFLEN);
107                         }
108                 }
109         }
110 }
111
112 static void users_alloc(struct information *ptr)
113 {
114         if (ptr->users.names == NULL) {
115                 ptr->users.names = malloc(text_buffer_size);
116
117         }
118         if (ptr->users.terms == NULL) {
119                 ptr->users.terms = malloc(text_buffer_size);
120         }
121         if (ptr->users.times == NULL) {
122                 ptr->users.times = malloc(text_buffer_size);
123         }
124 }
125
126 void update_users(void)
127 {
128         struct information *current_info = &info;
129         char temp[BUFLEN] = "";
130         int t;
131         users_alloc(current_info);
132         user_name(temp);
133         if (temp != NULL) {
134                 if (current_info->users.names) {
135                         free(current_info->users.names);
136                         current_info->users.names = 0;
137                 }
138                 current_info->users.names = malloc(text_buffer_size);
139                 strncpy(current_info->users.names, temp, text_buffer_size);
140         } else {
141                 if (current_info->users.names) {
142                         free(current_info->users.names);
143                         current_info->users.names = 0;
144                 }
145                 current_info->users.names = malloc(text_buffer_size);
146                 strncpy(current_info->users.names, "broken", text_buffer_size);
147         }
148         user_num(&t);
149         if (t != 0) {
150                 if (current_info->users.number) {
151                         current_info->users.number = 0;
152                 }
153                 current_info->users.number = t;
154         } else {
155                 current_info->users.number = 0;
156         }
157         temp[0] = 0;
158         user_term(temp);
159         if (temp != NULL) {
160                 if (current_info->users.terms) {
161                         free(current_info->users.terms);
162                         current_info->users.terms = 0;
163                 }
164                 current_info->users.terms = malloc(text_buffer_size);
165                 strncpy(current_info->users.terms, temp, text_buffer_size);
166         } else {
167                 if (current_info->users.terms) {
168                         free(current_info->users.terms);
169                         current_info->users.terms = 0;
170                 }
171                 current_info->users.terms = malloc(text_buffer_size);
172                 strncpy(current_info->users.terms, "broken", text_buffer_size);
173         }
174         user_time(temp);
175         if (temp != NULL) {
176                 if (current_info->users.times) {
177                         free(current_info->users.times);
178                         current_info->users.times = 0;
179                 }
180                 current_info->users.times = malloc(text_buffer_size);
181                 strncpy(current_info->users.times, temp, text_buffer_size);
182         } else {
183                 if (current_info->users.times) {
184                         free(current_info->users.times);
185                         current_info->users.times = 0;
186                 }
187                 current_info->users.times = malloc(text_buffer_size);
188                 strncpy(current_info->users.times, "broken", text_buffer_size);
189         }
190 }