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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines