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.2 by tdb, Thu Feb 20 13:19:52 2003 UTC vs.
Revision 1.6 by pajs, Sun Mar 2 16:43:48 2003 UTC

# Line 21 | Line 21
21   #ifdef HAVE_CONFIG_H
22   #include "config.h"
23   #endif
24 < #include "statgrab.h"
24 >
25   #include <stdlib.h>
26   #include <stdio.h>
27   #include <string.h>
28 #ifdef FREEBSD
29 #include <sys/types.h>
30 #endif
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  
42        name *name_ptr;
43
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          }      
49  
50 +        /* Essentially blank the list, or give it a inital starting string */
51 +        strcpy(user_stats.name_list, "");
52          setutent();
53          while((entry=getutent()) != NULL) {
54                  if(entry->ut_type==USER_PROCESS) {
55 <                        if(num_users>watermark-1){
56 <                                name_ptr=user_stat.name_list;
57 <                                if((user_stat.name_list=realloc(user_stat.name_list, (watermark*2* sizeof *user_stat.name_list)))==NULL){
58 <                                        user_stat.name_list=name_ptr;
55 >                        if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
56 >                                tmp=user_stats.name_list;
57 >                                user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
58 >                                if(user_stats.name_list==NULL){
59 >                                        user_stats.name_list=tmp;
60                                          return NULL;
61                                  }
62 <                                watermark=watermark*2;
62 >                                size_of_namelist=1+(size_of_namelist*2);
63                          }
64  
65 <                        strncpy(user_stat.name_list[num_users], entry->ut_user, MAX_LOGIN_NAME_SIZE);
66 <                        /* NULL terminate just in case , the size of user_stat.name_list is MAX_LOGIN_NAME_SIZE+1 */
67 <
68 <                        user_stat.name_list[num_users][MAX_LOGIN_NAME_SIZE]='\0';
69 <                                
65 >                        strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE);
66 >                        strcat(user_stats.name_list, " ");
67                          num_users++;
68                  }
69          }
70          endutent();
74        user_stat.num_entries=num_users;
71  
72 <        return &user_stat;
72 >        /* We want to remove the last " " */
73 >        tmp=strrchr(user_stats.name_list, ' ');
74 >        *tmp='\0';
75 >        user_stats.num_entries=num_users;
76 >
77 >        return &user_stats;
78  
79   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines