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.15 by tdb, Mon Jan 19 16:49:21 2004 UTC vs.
Revision 1.26 by ats, Sun Jul 18 21:30:12 2004 UTC

# Line 1 | Line 1
1   /*
2 < * i-scream central monitoring system
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4   * Copyright (C) 2000-2004 i-scream
5   *
# Line 29 | 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 <
37 < #ifdef SOLARIS
38 < #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
46 #if defined(CYGWIN)
47 #define MAX_LOGIN_NAME_SIZE _SC_LOGIN_NAME_MAX
48 #endif
46  
47 < #define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE))
48 <
49 < user_stat_t *get_user_stats(){
50 <        int num_users=0;
54 <
55 <        static user_stat_t user_stats;
56 <        static int size_of_namelist=-1;
57 <        char *tmp;
47 > sg_user_stats *sg_get_user_stats(){
48 >        int num_users = 0, pos = 0, new_pos;
49 >        VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL);
50 >        static sg_user_stats user_stats;
51   #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
52          struct utmp *entry;
53   #endif
54   #ifdef ALLBSD
55          struct utmp entry;
56 <        FILE *f;
56 >        FILE *f;
57   #endif
58  
66        /* First case call */
67        if (size_of_namelist==-1){
68                user_stats.name_list=malloc(START_VAL);
69                if(user_stats.name_list==NULL){
70                        return NULL;
71                }
72                size_of_namelist=START_VAL;
73        }      
74
75        /* Essentially blank the list, or give it a inital starting string */
76        strcpy(user_stats.name_list, "");
77
59   #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
60          setutent();
61          while((entry=getutent()) != NULL) {
62 <                if(entry->ut_type==USER_PROCESS) {
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){
86 <                                        user_stats.name_list=tmp;
87 <                                        return NULL;
88 <                                }
89 <                                size_of_namelist=1+(size_of_namelist*2);
90 <                        }
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
76   #ifdef ALLBSD
77          if ((f=fopen(_PATH_UTMP, "r")) == NULL){
78 +                sg_set_error_with_errno(SG_ERROR_OPEN, _PATH_UTMP);
79                  return NULL;
80          }
81          while((fread(&entry, sizeof(entry),1,f)) != 0){
82                  if (entry.ut_name[0] == '\0') continue;
83 <                if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
84 <                        tmp=user_stats.name_list;
85 <                        user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
86 <                        if(user_stats.name_list==NULL){
109 <                                user_stats.name_list=tmp;
110 <                                return NULL;
111 <                        }
112 <                        size_of_namelist=1+(size_of_namelist*2);
113 <                        
83 >
84 >                new_pos = pos + strlen(entry.ut_name) + 1;
85 >                if (VECTOR_RESIZE(name_list, new_pos) < 0) {
86 >                        return NULL;
87                  }
88 <                strncat(user_stats.name_list, entry.ut_name, MAX_LOGIN_NAME_SIZE);
89 <                strcat(user_stats.name_list, " ");
88 >
89 >                strcpy(name_list + pos, entry.ut_name);
90 >                name_list[new_pos - 1] = ' ';
91 >                pos = new_pos;
92                  num_users++;
93          }
94          fclose(f);
95 + #endif
96  
97 < #endif  
98 <        /* We want to remove the last " " */
99 <        if(num_users!=0){
124 <                tmp=strrchr(user_stats.name_list, ' ');
125 <                if(tmp!=NULL){
126 <                        *tmp='\0';
127 <                        user_stats.num_entries=num_users;
128 <                }
97 >        /* Remove the extra space at the end, and append a \0. */
98 >        if (num_users != 0) {
99 >                pos--;
100          }
101 +        if (VECTOR_RESIZE(name_list, pos + 1) < 0) {
102 +                return NULL;
103 +        }
104 +        name_list[pos] = '\0';
105  
106 +        user_stats.num_entries = num_users;
107 +        user_stats.name_list = name_list;
108          return &user_stats;
132
109   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines