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.4 by pajs, Sat Mar 1 02:38:22 2003 UTC vs.
Revision 1.5 by pajs, Sat Mar 1 15:01:01 2003 UTC

# Line 28 | Line 28
28   #include <utmp.h>
29   #include "statgrab.h"
30  
31 < #define START_VAL 5
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_stat;
37 <        static int watermark=-1;
36 >        static user_stat_t user_stats;
37 >        static int size_of_namelist=-1;
38 >        char *tmp;
39          struct utmp *entry;
40  
40        name *name_ptr;
41
41          /* First case call */
42 <        if (watermark==-1){
43 <                user_stat.name_list=malloc(START_VAL * sizeof(user_stat.name_list));
44 <                if(user_stat.name_list==NULL){
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 <                watermark=START_VAL;
47 >                size_of_namelist=START_VAL;
48 >                printf("Initially started at %d\n", size_of_namelist);
49          }      
50  
51 +        /* Essentially blank the list, or give it a inital starting string */
52 +        strcpy(user_stats.name_list, "");
53          setutent();
54          while((entry=getutent()) != NULL) {
55                  if(entry->ut_type==USER_PROCESS) {
56 <                        if(num_users>watermark-1){
57 <                                name_ptr=user_stat.name_list;
58 <                                if((user_stat.name_list=realloc(user_stat.name_list, (watermark*2* sizeof(user_stat.name_list))))==NULL){
59 <                                        user_stat.name_list=name_ptr;
56 >                        if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
57 >                                tmp=user_stats.name_list;
58 >                                user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
59 >                                printf("realloc to %d\n", 1+(size_of_namelist*2));
60 >                                if(user_stats.name_list==NULL){
61 >                                        printf("realloc failed\n");
62 >                                        user_stats.name_list=tmp;
63                                          return NULL;
64                                  }
65 <                                watermark=watermark*2;
65 >                                size_of_namelist=1+(size_of_namelist*2);
66                          }
67  
68 <                        strncpy(user_stat.name_list[num_users], entry->ut_user, MAX_LOGIN_NAME_SIZE);
69 <                        /* NULL terminate just in case , the size of user_stat.name_list is MAX_LOGIN_NAME_SIZE+1 */
65 <
66 <                        user_stat.name_list[num_users][MAX_LOGIN_NAME_SIZE]='\0';
67 <                                
68 >                        strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE);
69 >                        strcat(user_stats.name_list, " ");
70                          num_users++;
71                  }
72          }
73          endutent();
72        user_stat.num_entries=num_users;
74  
75 <        return &user_stat;
75 >        /* We want to remove the last " " */
76 >        tmp=strrchr(user_stats.name_list, ' ');
77 >        *tmp='\0';
78 >        user_stats.num_entries=num_users;
79 >
80 >        return &user_stats;
81  
82   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines