ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/user_stats.c
Revision: 1.9
Committed: Sun Aug 24 20:24:09 2003 UTC (20 years, 8 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_6_1, LIBSTATGRAB_0_6, LIBSTATGRAB_0_5_1, LIBSTATGRAB_0_5
Changes since 1.8: +2 -2 lines
Log Message:
Tidy up of lots of little things. :)

File Contents

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3 tdb 1.9 * http://www.i-scream.org
4     * Copyright (C) 2000-2003 i-scream
5 pajs 1.1 *
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 tdb 1.3
25 pajs 1.1 #include <stdlib.h>
26     #include <stdio.h>
27     #include <string.h>
28 pajs 1.8 #include "statgrab.h"
29     #ifdef FREEBSD
30     #include <sys/types.h>
31     #endif
32 pajs 1.1 #include <utmp.h>
33    
34 pajs 1.5 #define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE))
35 pajs 1.1
36     user_stat_t *get_user_stats(){
37     int num_users=0;
38    
39 pajs 1.5 static user_stat_t user_stats;
40     static int size_of_namelist=-1;
41     char *tmp;
42 pajs 1.8 #if defined(SOLARIS) || defined(LINUX)
43 pajs 1.1 struct utmp *entry;
44 pajs 1.8 #endif
45     #ifdef FREEBSD
46     struct utmp entry;
47     FILE *f;
48     #endif
49 pajs 1.1
50     /* First case call */
51 pajs 1.5 if (size_of_namelist==-1){
52     user_stats.name_list=malloc(START_VAL);
53     if(user_stats.name_list==NULL){
54 pajs 1.1 return NULL;
55     }
56 pajs 1.5 size_of_namelist=START_VAL;
57 pajs 1.1 }
58    
59 pajs 1.5 /* Essentially blank the list, or give it a inital starting string */
60     strcpy(user_stats.name_list, "");
61 pajs 1.8
62     #if defined(SOLARIS) || defined(LINUX)
63 pajs 1.1 setutent();
64     while((entry=getutent()) != NULL) {
65     if(entry->ut_type==USER_PROCESS) {
66 pajs 1.5 if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
67     tmp=user_stats.name_list;
68     user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
69     if(user_stats.name_list==NULL){
70     user_stats.name_list=tmp;
71 pajs 1.1 return NULL;
72     }
73 pajs 1.5 size_of_namelist=1+(size_of_namelist*2);
74 pajs 1.1 }
75    
76 pajs 1.5 strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE);
77     strcat(user_stats.name_list, " ");
78 pajs 1.1 num_users++;
79     }
80     }
81     endutent();
82 pajs 1.8 #endif
83     #ifdef FREEBSD
84     if ((f=fopen(_PATH_UTMP, "r")) == NULL){
85     return NULL;
86     }
87     while((fread(&entry, sizeof(entry),1,f)) != 0){
88     if (entry.ut_name[0] == '\0') continue;
89     if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
90     tmp=user_stats.name_list;
91     user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
92     if(user_stats.name_list==NULL){
93     user_stats.name_list=tmp;
94     return NULL;
95     }
96     size_of_namelist=1+(size_of_namelist*2);
97    
98     }
99     strncat(user_stats.name_list, entry.ut_name, MAX_LOGIN_NAME_SIZE);
100     strcat(user_stats.name_list, " ");
101     num_users++;
102     }
103     fclose(f);
104 pajs 1.1
105 pajs 1.8 #endif
106 pajs 1.5 /* We want to remove the last " " */
107 pajs 1.7 if(num_users!=0){
108     tmp=strrchr(user_stats.name_list, ' ');
109     if(tmp!=NULL){
110     *tmp='\0';
111     user_stats.num_entries=num_users;
112     }
113     }
114 pajs 1.5
115     return &user_stats;
116 pajs 1.1
117     }