ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/user_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/user_stats.c (file contents):
Revision 1.13 by pajs, Tue Jan 6 22:28:41 2004 UTC vs.
Revision 1.20 by ats, Sun Apr 4 23:45:03 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 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.
6 > * This library is free software; you can redistribute it and/or
7 > * modify it under the terms of the GNU Lesser General Public
8 > * License as published by the Free Software Foundation; either
9 > * version 2.1 of the License, or (at your option) any later version.
10   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library 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.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser 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.
16 > * You should have received a copy of the GNU Lesser General Public
17 > * License along with this library; if not, write to the Free Software
18 > * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 > * 02111-1307 USA
20 > *
21 > * $Id$
22   */
23  
24   #ifdef HAVE_CONFIG_H
# Line 26 | Line 29
29   #include <stdio.h>
30   #include <string.h>
31   #include "statgrab.h"
32 + #include "vector.h"
33   #ifdef ALLBSD
34   #include <sys/types.h>
35   #endif
36 < #include <utmp.h>
37 <
34 < #ifdef SOLARIS
35 < #define MAX_LOGIN_NAME_SIZE 8
36 > #if defined(NETBSD) || defined(OPENBSD)
37 > #include <limits.h>
38   #endif
39 < #if defined(LINUX) || defined(FREEBSD)
40 < #define MAX_LOGIN_NAME_SIZE UT_NAMESIZE
39 > #ifdef OPENBSD
40 > #include <sys/param.h>
41   #endif
42 < #ifdef NETBSD
43 < #define MAX_LOGIN_NAME_SIZE _POSIX_LOGIN_NAME_MAX
42 > #include <utmp.h>
43 > #ifdef CYGWIN
44 > #include <sys/unistd.h>
45   #endif
43 #if defined(CYGWIN)
44 #define MAX_LOGIN_NAME_SIZE _SC_LOGIN_NAME_MAX
45 #endif
46  
47 #define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE))
48
47   user_stat_t *get_user_stats(){
48 <        int num_users=0;
49 <
48 >        int num_users = 0, pos = 0, new_pos;
49 >        VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL);
50          static user_stat_t user_stats;
53        static int size_of_namelist=-1;
54        char *tmp;
51   #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
52          struct utmp *entry;
53   #endif
# Line 60 | Line 56 | user_stat_t *get_user_stats(){
56          FILE *f;
57   #endif
58  
63        /* First case call */
64        if (size_of_namelist==-1){
65                user_stats.name_list=malloc(START_VAL);
66                if(user_stats.name_list==NULL){
67                        return NULL;
68                }
69                size_of_namelist=START_VAL;
70        }      
71
72        /* Essentially blank the list, or give it a inital starting string */
73        strcpy(user_stats.name_list, "");
74
59   #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
60          setutent();
61          while((entry=getutent()) != NULL) {
62 <                if(entry->ut_type==USER_PROCESS) {
79 <                        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 <                                        return NULL;
85 <                                }
86 <                                size_of_namelist=1+(size_of_namelist*2);
87 <                        }
62 >                if (entry->ut_type != USER_PROCESS) continue;
63  
64 <                        strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE);
65 <                        strcat(user_stats.name_list, " ");
66 <                        num_users++;
64 >                new_pos = pos + strlen(entry->ut_user) + 1;
65 >                if (VECTOR_RESIZE(name_list, new_pos) < 0) {
66 >                        return NULL;
67                  }
68 +
69 +                strcpy(name_list + pos, entry->ut_user);
70 +                name_list[new_pos - 1] = ' ';
71 +                pos = new_pos;
72 +                num_users++;
73          }
74          endutent();
75   #endif
# Line 99 | Line 79 | user_stat_t *get_user_stats(){
79          }
80          while((fread(&entry, sizeof(entry),1,f)) != 0){
81                  if (entry.ut_name[0] == '\0') continue;
82 <                if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
83 <                        tmp=user_stats.name_list;
84 <                        user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
85 <                        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 <                        
82 >
83 >                new_pos = pos + strlen(entry.ut_name) + 1;
84 >                if (VECTOR_RESIZE(name_list, new_pos) < 0) {
85 >                        return NULL;
86                  }
87 <                strncat(user_stats.name_list, entry.ut_name, MAX_LOGIN_NAME_SIZE);
88 <                strcat(user_stats.name_list, " ");
87 >
88 >                strcpy(name_list + pos, entry.ut_name);
89 >                name_list[new_pos - 1] = ' ';
90 >                pos = new_pos;
91                  num_users++;
92          }
93          fclose(f);
94 + #endif
95  
96 < #endif  
97 <        /* We want to remove the last " " */
98 <        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 <                }
96 >        /* Remove the extra space at the end, and append a \0. */
97 >        if (num_users != 0) {
98 >                pos--;
99          }
100 +        VECTOR_RESIZE(name_list, pos + 1);
101 +        name_list[pos] = '\0';
102  
103 +        user_stats.num_entries = num_users;
104 +        user_stats.name_list = name_list;
105          return &user_stats;
129
106   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines