ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/user_stats.c
Revision: 1.13
Committed: Tue Jan 6 22:28:41 2004 UTC (20 years, 4 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.12: +13 -0 lines
Log Message:
Moving the MAX_LOGIN_NAME_SIZE to where it should be.

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 ats 1.10 #ifdef ALLBSD
30 pajs 1.8 #include <sys/types.h>
31     #endif
32 pajs 1.1 #include <utmp.h>
33    
34 pajs 1.13 #ifdef SOLARIS
35     #define MAX_LOGIN_NAME_SIZE 8
36     #endif
37     #if defined(LINUX) || defined(FREEBSD)
38     #define MAX_LOGIN_NAME_SIZE UT_NAMESIZE
39     #endif
40     #ifdef NETBSD
41     #define MAX_LOGIN_NAME_SIZE _POSIX_LOGIN_NAME_MAX
42     #endif
43     #if defined(CYGWIN)
44     #define MAX_LOGIN_NAME_SIZE _SC_LOGIN_NAME_MAX
45     #endif
46    
47 pajs 1.5 #define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE))
48 pajs 1.1
49     user_stat_t *get_user_stats(){
50     int num_users=0;
51    
52 pajs 1.5 static user_stat_t user_stats;
53     static int size_of_namelist=-1;
54     char *tmp;
55 ats 1.12 #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
56 pajs 1.1 struct utmp *entry;
57 pajs 1.8 #endif
58 ats 1.10 #ifdef ALLBSD
59 pajs 1.8 struct utmp entry;
60     FILE *f;
61     #endif
62 pajs 1.1
63     /* First case call */
64 pajs 1.5 if (size_of_namelist==-1){
65     user_stats.name_list=malloc(START_VAL);
66     if(user_stats.name_list==NULL){
67 pajs 1.1 return NULL;
68     }
69 pajs 1.5 size_of_namelist=START_VAL;
70 pajs 1.1 }
71    
72 pajs 1.5 /* Essentially blank the list, or give it a inital starting string */
73     strcpy(user_stats.name_list, "");
74 pajs 1.8
75 ats 1.12 #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
76 pajs 1.1 setutent();
77     while((entry=getutent()) != NULL) {
78     if(entry->ut_type==USER_PROCESS) {
79 pajs 1.5 if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
80     tmp=user_stats.name_list;
81     user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
82     if(user_stats.name_list==NULL){
83     user_stats.name_list=tmp;
84 pajs 1.1 return NULL;
85     }
86 pajs 1.5 size_of_namelist=1+(size_of_namelist*2);
87 pajs 1.1 }
88    
89 pajs 1.5 strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE);
90     strcat(user_stats.name_list, " ");
91 pajs 1.1 num_users++;
92     }
93     }
94     endutent();
95 pajs 1.8 #endif
96 ats 1.10 #ifdef ALLBSD
97 pajs 1.8 if ((f=fopen(_PATH_UTMP, "r")) == NULL){
98     return NULL;
99     }
100     while((fread(&entry, sizeof(entry),1,f)) != 0){
101     if (entry.ut_name[0] == '\0') continue;
102     if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
103     tmp=user_stats.name_list;
104     user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
105     if(user_stats.name_list==NULL){
106     user_stats.name_list=tmp;
107     return NULL;
108     }
109     size_of_namelist=1+(size_of_namelist*2);
110    
111     }
112     strncat(user_stats.name_list, entry.ut_name, MAX_LOGIN_NAME_SIZE);
113     strcat(user_stats.name_list, " ");
114     num_users++;
115     }
116     fclose(f);
117 pajs 1.1
118 pajs 1.8 #endif
119 pajs 1.5 /* We want to remove the last " " */
120 pajs 1.7 if(num_users!=0){
121     tmp=strrchr(user_stats.name_list, ' ');
122     if(tmp!=NULL){
123     *tmp='\0';
124     user_stats.num_entries=num_users;
125     }
126     }
127 pajs 1.5
128     return &user_stats;
129 pajs 1.1
130     }