updating new file props
[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         const struct utmp *usr;
39         char buf[512];
40
41         setutent();
42         while((usr=getutent())!=NULL) {
43                 if (usr->ut_type==USER_PROCESS) {
44                         strncat(buf, usr->ut_name, 9); strcat(buf, "\n");
45                 }
46         }
47         *ptr = buf;
48 }
49 static void user_num(int *ptr) {
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         const struct utmp *usr;
63         char buf[512];
64
65         setutent();
66         while((usr=getutent())!=NULL) {
67                 if (usr->ut_type==USER_PROCESS) {
68                         strncat(buf, usr->ut_line, 13); strncat(buf, "\n", 3);
69                 }
70         }
71         *ptr = buf;
72 }
73 static void user_time(char **ptr) {
74         const struct utmp *usr;
75         time_t login, real, diff;
76         struct tm *dtime;
77         char buf[512] = "";
78         char output[512] = "";
79
80         setutent();
81         while ((usr=getutent())!=NULL) {
82                 if (usr->ut_type==USER_PROCESS) {
83                         login=usr->ut_time;
84                         time(&real);
85                         diff = difftime(real, login);
86                         dtime = localtime(&diff);
87                         dtime->tm_year = dtime->tm_year-70;
88                         dtime->tm_mon = dtime->tm_mon-1;
89                         dtime->tm_mday = dtime->tm_mday-1;
90                         if(dtime->tm_year>0){strftime(buf,512,"%yy %mm %dd %Hh %Mm\n", dtime); goto end;}
91                         else if(dtime->tm_mon>0){strftime(buf,512,"%mm %dd %Hh %Mm\n", dtime); goto end;}
92                         else if(dtime->tm_mday>0){strftime(buf,512,"%dd %Hh %Mm\n", dtime); goto end;}
93                         else if(dtime->tm_hour>0){strftime(buf,512,"%Hh %Mm\n", dtime); goto end;}
94                         else if(dtime->tm_min>0){strftime(buf,512,"%Mm\n", dtime); goto end;}
95 end:
96                         strncat(output, buf, 512);
97                 }
98         }
99         *ptr = output;
100 }
101
102 static void users_alloc(struct information *ptr) {
103         if (ptr->users.names == NULL) {
104                 ptr->users.names = malloc(TEXT_BUFFER_SIZE);
105
106         }
107         if (ptr->users.terms == NULL) {
108                 ptr->users.terms = malloc(TEXT_BUFFER_SIZE);
109         }
110         if (ptr->users.times == NULL) {
111                 ptr->users.times = malloc(TEXT_BUFFER_SIZE);
112         }
113 }
114
115 void update_users() {
116         struct information * current_info = &info;      
117         char *temp;
118         int t;
119         users_alloc(current_info);
120         user_name(&temp);
121         if (temp!=NULL) {
122                 if (current_info->users.names) {
123                         free(current_info->users.names); current_info->users.names = 0;
124                 }
125                 current_info->users.names = malloc(TEXT_BUFFER_SIZE);
126                 strncpy(current_info->users.names, temp, TEXT_BUFFER_SIZE);
127         } else {
128                 if (current_info->users.names) {
129                         free(current_info->users.names); current_info->users.names = 0;
130                 }
131                 current_info->users.names = malloc(TEXT_BUFFER_SIZE);
132                 strncpy(current_info->users.names, "broken", TEXT_BUFFER_SIZE);
133         }
134         user_num(&t);
135         if (t!=0) {
136                 if (current_info->users.number) {
137                         current_info->users.number = 0;
138                 }
139                 current_info->users.number = t;
140         } else {
141                 current_info->users.number = 0;
142         }
143         temp = "\0";
144         user_term(&temp);
145         if (temp!=NULL) {
146                 if (current_info->users.terms) {
147                         free(current_info->users.terms); 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); 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); 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); current_info->users.times = 0;
168                 }
169                 current_info->users.times = malloc(TEXT_BUFFER_SIZE);
170                 strncpy(current_info->users.times, "broken", TEXT_BUFFER_SIZE);
171         }
172 }