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