d2d5cf597456b8ff8056155545078855dd7090e1
[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  */
27
28 #include "conky.h"
29 #include <utmp.h>
30 #include <time.h>
31
32 #define BUFLEN 512
33
34 static void user_name(char *ptr)
35 {
36         const struct utmp *usr = 0;
37
38         setutent();
39         while ((usr = getutent()) != NULL) {
40                 if (usr->ut_type == USER_PROCESS) {
41                         if (strlen(ptr) + strlen(usr->ut_name) + 1 <= BUFLEN) {
42                                 strncat(ptr, usr->ut_name, UT_NAMESIZE);
43                         }
44                 }
45         }
46 }
47 static void user_num(int *ptr)
48 {
49         const struct utmp *usr;
50         int users_num = 0;
51
52         setutent();
53         while ((usr = getutent()) != NULL) {
54                 if (usr->ut_type == USER_PROCESS) {
55                         ++users_num;
56                 }
57         }
58         *ptr = users_num;
59 }
60 static void user_term(char *ptr)
61 {
62         const struct utmp *usr;
63
64         setutent();
65         while ((usr = getutent()) != NULL) {
66                 if (usr->ut_type == USER_PROCESS) {
67                         if (strlen(ptr) + strlen(usr->ut_line) + 1 <= BUFLEN) {
68                                 strncat(ptr, usr->ut_line, UT_LINESIZE);
69                         }
70                 }
71         }
72 }
73 static void user_time(char *ptr)
74 {
75         const struct utmp *usr;
76         time_t log_in, real, diff;
77         struct tm *dtime;
78         char buf[BUFLEN] = "";
79
80         setutent();
81         while ((usr = getutent()) != NULL) {
82                 if (usr->ut_type == USER_PROCESS) {
83                         log_in = usr->ut_time;
84                         time(&real);
85                         diff = difftime(real, log_in);
86                         dtime = localtime(&diff);
87                         dtime->tm_year = dtime->tm_year - 70;
88                         dtime->tm_mon = dtime->tm_mon - 1;
89                         dtime->tm_mday = dtime->tm_mday - 1;
90                         if (dtime->tm_year > 0) {
91                                 strftime(buf, BUFLEN, "%yy %mm %dd %Hh %Mm", dtime);
92                         } else if (dtime->tm_mon > 0) {
93                                 strftime(buf, BUFLEN, "%mm %dd %Hh %Mm", dtime);
94                         } else if (dtime->tm_mday > 0) {
95                                 strftime(buf, BUFLEN, "%dd %Hh %Mm", dtime);
96                         } else if (dtime->tm_hour > 0) {
97                                 strftime(buf, BUFLEN, "%Hh %Mm", dtime);
98                         } else if (dtime->tm_min > 0) {
99                                 strftime(buf, BUFLEN, "%Mm", dtime);
100                         }
101                         if (strlen(ptr) + strlen(buf) + 1 <= BUFLEN) {
102                                 strncat(ptr, buf, BUFLEN-strlen(ptr)-1);
103                         }
104                 }
105         }
106 }
107
108 static void users_alloc(struct information *ptr)
109 {
110         if (ptr->users.names == NULL) {
111                 ptr->users.names = malloc(text_buffer_size);
112
113         }
114         if (ptr->users.terms == NULL) {
115                 ptr->users.terms = malloc(text_buffer_size);
116         }
117         if (ptr->users.times == NULL) {
118                 ptr->users.times = malloc(text_buffer_size);
119         }
120 }
121
122 void update_users(void)
123 {
124         struct information *current_info = &info;
125         char temp[BUFLEN] = "";
126         int t;
127         users_alloc(current_info);
128         user_name(temp);
129         if (temp != NULL) {
130                 if (current_info->users.names) {
131                         free(current_info->users.names);
132                         current_info->users.names = 0;
133                 }
134                 current_info->users.names = malloc(text_buffer_size);
135                 strncpy(current_info->users.names, temp, text_buffer_size);
136         } else {
137                 if (current_info->users.names) {
138                         free(current_info->users.names);
139                         current_info->users.names = 0;
140                 }
141                 current_info->users.names = malloc(text_buffer_size);
142                 strncpy(current_info->users.names, "broken", text_buffer_size);
143         }
144         user_num(&t);
145         if (t != 0) {
146                 if (current_info->users.number) {
147                         current_info->users.number = 0;
148                 }
149                 current_info->users.number = t;
150         } else {
151                 current_info->users.number = 0;
152         }
153         temp[0] = 0;
154         user_term(temp);
155         if (temp != NULL) {
156                 if (current_info->users.terms) {
157                         free(current_info->users.terms);
158                         current_info->users.terms = 0;
159                 }
160                 current_info->users.terms = malloc(text_buffer_size);
161                 strncpy(current_info->users.terms, temp, text_buffer_size);
162         } else {
163                 if (current_info->users.terms) {
164                         free(current_info->users.terms);
165                         current_info->users.terms = 0;
166                 }
167                 current_info->users.terms = malloc(text_buffer_size);
168                 strncpy(current_info->users.terms, "broken", text_buffer_size);
169         }
170         user_time(temp);
171         if (temp != NULL) {
172                 if (current_info->users.times) {
173                         free(current_info->users.times);
174                         current_info->users.times = 0;
175                 }
176                 current_info->users.times = malloc(text_buffer_size);
177                 strncpy(current_info->users.times, temp, text_buffer_size);
178         } else {
179                 if (current_info->users.times) {
180                         free(current_info->users.times);
181                         current_info->users.times = 0;
182                 }
183                 current_info->users.times = malloc(text_buffer_size);
184                 strncpy(current_info->users.times, "broken", text_buffer_size);
185         }
186 }