0f2ffcde182ea6c8484ebf216c513c2c601464fe
[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-2009 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  * vim: ts=4 sw=4 noet ai cindent syntax=c
27  * -*- c-basic-offset: 4; tab-width: 4 -*-
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                         if (strlen(ptr) + strlen(usr->ut_name) + 1 <= BUFLEN) {
44                                 strncat(ptr, usr->ut_name, UT_NAMESIZE);
45                         }
46                 }
47         }
48 }
49 static void user_num(int *ptr)
50 {
51         const struct utmp *usr;
52         int users_num = 0;
53
54         setutent();
55         while ((usr = getutent()) != NULL) {
56                 if (usr->ut_type == USER_PROCESS) {
57                         ++users_num;
58                 }
59         }
60         *ptr = users_num;
61 }
62 static void user_term(char *ptr)
63 {
64         const struct utmp *usr;
65
66         setutent();
67         while ((usr = getutent()) != NULL) {
68                 if (usr->ut_type == USER_PROCESS) {
69                         if (strlen(ptr) + strlen(usr->ut_line) + 1 <= BUFLEN) {
70                                 strncat(ptr, usr->ut_line, UT_LINESIZE);
71                         }
72                 }
73         }
74 }
75 static void user_time(char *ptr)
76 {
77         const struct utmp *usr;
78         time_t log_in, real, diff;
79         char buf[BUFLEN] = "";
80
81         setutent();
82         while ((usr = getutent()) != NULL) {
83                 if (usr->ut_type == USER_PROCESS) {
84                         log_in = usr->ut_time;
85                         time(&real);
86                         diff = difftime(real, log_in);
87                         format_seconds(buf, BUFLEN, diff);
88                         if (strlen(ptr) + strlen(buf) + 1 <= BUFLEN) {
89                                 strncat(ptr, buf, BUFLEN-strlen(ptr)-1);
90                         }
91                 }
92         }
93 }
94
95 static void users_alloc(struct information *ptr)
96 {
97         if (ptr->users.names == NULL) {
98                 ptr->users.names = malloc(text_buffer_size);
99
100         }
101         if (ptr->users.terms == NULL) {
102                 ptr->users.terms = malloc(text_buffer_size);
103         }
104         if (ptr->users.times == NULL) {
105                 ptr->users.times = malloc(text_buffer_size);
106         }
107 }
108
109 void update_users(void)
110 {
111         struct information *current_info = &info;
112         char temp[BUFLEN] = "";
113         int t;
114         users_alloc(current_info);
115         user_name(temp);
116         if (temp != NULL) {
117                 if (current_info->users.names) {
118                         free(current_info->users.names);
119                         current_info->users.names = 0;
120                 }
121                 current_info->users.names = malloc(text_buffer_size);
122                 strncpy(current_info->users.names, temp, text_buffer_size);
123         } else {
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, "broken", text_buffer_size);
130         }
131         user_num(&t);
132         if (t != 0) {
133                 if (current_info->users.number) {
134                         current_info->users.number = 0;
135                 }
136                 current_info->users.number = t;
137         } else {
138                 current_info->users.number = 0;
139         }
140         temp[0] = 0;
141         user_term(temp);
142         if (temp != NULL) {
143                 if (current_info->users.terms) {
144                         free(current_info->users.terms);
145                         current_info->users.terms = 0;
146                 }
147                 current_info->users.terms = malloc(text_buffer_size);
148                 strncpy(current_info->users.terms, temp, text_buffer_size);
149         } else {
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, "broken", text_buffer_size);
156         }
157         user_time(temp);
158         if (temp != NULL) {
159                 if (current_info->users.times) {
160                         free(current_info->users.times);
161                         current_info->users.times = 0;
162                 }
163                 current_info->users.times = malloc(text_buffer_size);
164                 strncpy(current_info->users.times, temp, text_buffer_size);
165         } else {
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, "broken", text_buffer_size);
172         }
173 }