ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/user_stats.c
Revision: 1.7
Committed: Mon Mar 10 19:01:32 2003 UTC (21 years, 2 months ago) by pajs
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_3_4, LIBSTATGRAB_0_3_3, LIBSTATGRAB_0_3_2, LIBSTATGRAB_0_3_1, LIBSTATGRAB_0_3
Changes since 1.6: +7 -3 lines
Log Message:
Think i've fixed another bug which causes seg faults on machines with no
users :)

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org.uk
4 * Copyright (C) 2000-2002 i-scream
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <utmp.h>
29 #include "statgrab.h"
30
31 #define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE))
32
33 user_stat_t *get_user_stats(){
34 int num_users=0;
35
36 static user_stat_t user_stats;
37 static int size_of_namelist=-1;
38 char *tmp;
39 struct utmp *entry;
40
41 /* First case call */
42 if (size_of_namelist==-1){
43 user_stats.name_list=malloc(START_VAL);
44 if(user_stats.name_list==NULL){
45 return NULL;
46 }
47 size_of_namelist=START_VAL;
48 }
49
50 /* Essentially blank the list, or give it a inital starting string */
51 strcpy(user_stats.name_list, "");
52 setutent();
53 while((entry=getutent()) != NULL) {
54 if(entry->ut_type==USER_PROCESS) {
55 if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
56 tmp=user_stats.name_list;
57 user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
58 if(user_stats.name_list==NULL){
59 user_stats.name_list=tmp;
60 return NULL;
61 }
62 size_of_namelist=1+(size_of_namelist*2);
63 }
64
65 strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE);
66 strcat(user_stats.name_list, " ");
67 num_users++;
68 }
69 }
70 endutent();
71
72 /* We want to remove the last " " */
73 if(num_users!=0){
74 tmp=strrchr(user_stats.name_list, ' ');
75 if(tmp!=NULL){
76 *tmp='\0';
77 user_stats.num_entries=num_users;
78 }
79 }
80
81 return &user_stats;
82
83 }