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