b97c49c72b8a210f56471f30b6d89da69fab9ef5
[monky] / src / users.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13  *      (see AUTHORS)
14  * All rights reserved.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  *
28  * vim: ts=4 sw=4 noet ai cindent syntax=c
29  *
30  */
31
32 #include "conky.h"
33 #include <utmp.h>
34 #include <time.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
97 static void users_alloc(struct information *ptr)
98 {
99         if (ptr->users.names == NULL) {
100                 ptr->users.names = malloc(text_buffer_size);
101
102         }
103         if (ptr->users.terms == NULL) {
104                 ptr->users.terms = malloc(text_buffer_size);
105         }
106         if (ptr->users.times == NULL) {
107                 ptr->users.times = malloc(text_buffer_size);
108         }
109 }
110
111 void update_users(void)
112 {
113         struct information *current_info = &info;
114         char temp[BUFLEN] = "";
115         int t;
116         users_alloc(current_info);
117         user_name(temp);
118         if (temp != NULL) {
119                 if (current_info->users.names) {
120                         free(current_info->users.names);
121                         current_info->users.names = 0;
122                 }
123                 current_info->users.names = malloc(text_buffer_size);
124                 strncpy(current_info->users.names, temp, text_buffer_size);
125         } else {
126                 if (current_info->users.names) {
127                         free(current_info->users.names);
128                         current_info->users.names = 0;
129                 }
130                 current_info->users.names = malloc(text_buffer_size);
131                 strncpy(current_info->users.names, "broken", text_buffer_size);
132         }
133         user_num(&t);
134         if (t != 0) {
135                 if (current_info->users.number) {
136                         current_info->users.number = 0;
137                 }
138                 current_info->users.number = t;
139         } else {
140                 current_info->users.number = 0;
141         }
142         temp[0] = 0;
143         user_term(temp);
144         if (temp != NULL) {
145                 if (current_info->users.terms) {
146                         free(current_info->users.terms);
147                         current_info->users.terms = 0;
148                 }
149                 current_info->users.terms = malloc(text_buffer_size);
150                 strncpy(current_info->users.terms, temp, text_buffer_size);
151         } else {
152                 if (current_info->users.terms) {
153                         free(current_info->users.terms);
154                         current_info->users.terms = 0;
155                 }
156                 current_info->users.terms = malloc(text_buffer_size);
157                 strncpy(current_info->users.terms, "broken", text_buffer_size);
158         }
159         user_time(temp);
160         if (temp != NULL) {
161                 if (current_info->users.times) {
162                         free(current_info->users.times);
163                         current_info->users.times = 0;
164                 }
165                 current_info->users.times = malloc(text_buffer_size);
166                 strncpy(current_info->users.times, temp, text_buffer_size);
167         } else {
168                 if (current_info->users.times) {
169                         free(current_info->users.times);
170                         current_info->users.times = 0;
171                 }
172                 current_info->users.times = malloc(text_buffer_size);
173                 strncpy(current_info->users.times, "broken", text_buffer_size);
174         }
175 }