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.12 by ats, Mon Nov 10 23:25:45 2003 UTC

# Line 1 | Line 1
1   /*
2   * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
3 > * http://www.i-scream.org
4 > * Copyright (C) 2000-2003 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
# Line 25 | Line 25
25   #include <stdlib.h>
26   #include <stdio.h>
27   #include <string.h>
28 #include <utmp.h>
28   #include "statgrab.h"
29 + #ifdef ALLBSD
30 + #include <sys/types.h>
31 + #endif
32 + #include <utmp.h>
33  
34 < #define START_VAL 5
34 > #define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE))
35  
36   user_stat_t *get_user_stats(){
37          int num_users=0;
38  
39 <        static user_stat_t user_stat;
40 <        static int watermark=-1;
39 >        static user_stat_t user_stats;
40 >        static int size_of_namelist=-1;
41 >        char *tmp;
42 > #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
43          struct utmp *entry;
44 + #endif
45 + #ifdef ALLBSD
46 +        struct utmp entry;
47 +        FILE *f;
48 + #endif
49  
40        name *name_ptr;
41
50          /* First case call */
51 <        if (watermark==-1){
52 <                user_stat.name_list=malloc(START_VAL * sizeof(user_stat.name_list));
53 <                if(user_stat.name_list==NULL){
51 >        if (size_of_namelist==-1){
52 >                user_stats.name_list=malloc(START_VAL);
53 >                if(user_stats.name_list==NULL){
54                          return NULL;
55                  }
56 <                watermark=START_VAL;
56 >                size_of_namelist=START_VAL;
57          }      
58  
59 +        /* Essentially blank the list, or give it a inital starting string */
60 +        strcpy(user_stats.name_list, "");
61 +
62 + #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
63          setutent();
64          while((entry=getutent()) != NULL) {
65                  if(entry->ut_type==USER_PROCESS) {
66 <                        if(num_users>watermark-1){
67 <                                name_ptr=user_stat.name_list;
68 <                                if((user_stat.name_list=realloc(user_stat.name_list, (watermark*2* sizeof(user_stat.name_list))))==NULL){
69 <                                        user_stat.name_list=name_ptr;
66 >                        if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
67 >                                tmp=user_stats.name_list;
68 >                                user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
69 >                                if(user_stats.name_list==NULL){
70 >                                        user_stats.name_list=tmp;
71                                          return NULL;
72                                  }
73 <                                watermark=watermark*2;
73 >                                size_of_namelist=1+(size_of_namelist*2);
74                          }
75  
76 <                        strncpy(user_stat.name_list[num_users], entry->ut_user, MAX_LOGIN_NAME_SIZE);
77 <                        /* 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 <                                
76 >                        strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE);
77 >                        strcat(user_stats.name_list, " ");
78                          num_users++;
79                  }
80          }
81          endutent();
82 <        user_stat.num_entries=num_users;
82 > #endif
83 > #ifdef ALLBSD
84 >        if ((f=fopen(_PATH_UTMP, "r")) == NULL){
85 >                return NULL;
86 >        }
87 >        while((fread(&entry, sizeof(entry),1,f)) != 0){
88 >                if (entry.ut_name[0] == '\0') continue;
89 >                if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
90 >                        tmp=user_stats.name_list;
91 >                        user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
92 >                        if(user_stats.name_list==NULL){
93 >                                user_stats.name_list=tmp;
94 >                                return NULL;
95 >                        }
96 >                        size_of_namelist=1+(size_of_namelist*2);
97 >                        
98 >                }
99 >                strncat(user_stats.name_list, entry.ut_name, MAX_LOGIN_NAME_SIZE);
100 >                strcat(user_stats.name_list, " ");
101 >                num_users++;
102 >        }
103 >        fclose(f);
104  
105 <        return &user_stat;
105 > #endif  
106 >        /* We want to remove the last " " */
107 >        if(num_users!=0){
108 >                tmp=strrchr(user_stats.name_list, ' ');
109 >                if(tmp!=NULL){
110 >                        *tmp='\0';
111 >                        user_stats.num_entries=num_users;
112 >                }
113 >        }
114 >
115 >        return &user_stats;
116  
117   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines