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