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.20 by ats, Sun Apr 4 23:45:03 2004 UTC vs.
Revision 1.27 by tdb, Mon Nov 1 18:30:17 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 43 | Line 43
43   #ifdef CYGWIN
44   #include <sys/unistd.h>
45   #endif
46 + #ifdef HPUX
47 + #include <utmp.h>
48 + #endif
49  
50 < user_stat_t *get_user_stats(){
50 > sg_user_stats *sg_get_user_stats(){
51          int num_users = 0, pos = 0, new_pos;
52          VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL);
53 <        static user_stat_t user_stats;
51 < #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
52 <        struct utmp *entry;
53 < #endif
53 >        static sg_user_stats user_stats;
54   #ifdef ALLBSD
55          struct utmp entry;
56 <        FILE *f;
57 < #endif
56 >        FILE *f;
57  
58 < #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
59 <        setutent();
60 <        while((entry=getutent()) != NULL) {
61 <                if (entry->ut_type != USER_PROCESS) continue;
58 >        if ((f=fopen(_PATH_UTMP, "r")) == NULL){
59 >                sg_set_error_with_errno(SG_ERROR_OPEN, _PATH_UTMP);
60 >                return NULL;
61 >        }
62 >        while((fread(&entry, sizeof(entry),1,f)) != 0){
63 >                if (entry.ut_name[0] == '\0') continue;
64  
65 <                new_pos = pos + strlen(entry->ut_user) + 1;
65 >                new_pos = pos + strlen(entry.ut_name) + 1;
66                  if (VECTOR_RESIZE(name_list, new_pos) < 0) {
67                          return NULL;
68                  }
69  
70 <                strcpy(name_list + pos, entry->ut_user);
70 >                strcpy(name_list + pos, entry.ut_name);
71                  name_list[new_pos - 1] = ' ';
72                  pos = new_pos;
73                  num_users++;
74          }
75 <        endutent();
76 < #endif
77 < #ifdef ALLBSD
78 <        if ((f=fopen(_PATH_UTMP, "r")) == NULL){
78 <                return NULL;
79 <        }
80 <        while((fread(&entry, sizeof(entry),1,f)) != 0){
81 <                if (entry.ut_name[0] == '\0') continue;
75 >        fclose(f);
76 > #else
77 >        /* This works on everything else. */
78 >        struct utmp *entry;
79  
80 <                new_pos = pos + strlen(entry.ut_name) + 1;
80 >        setutent();
81 >        while((entry=getutent()) != NULL) {
82 >                if (entry->ut_type != USER_PROCESS) continue;
83 >
84 >                new_pos = pos + strlen(entry->ut_user) + 1;
85                  if (VECTOR_RESIZE(name_list, new_pos) < 0) {
86                          return NULL;
87                  }
88  
89 <                strcpy(name_list + pos, entry.ut_name);
89 >                strcpy(name_list + pos, entry->ut_user);
90                  name_list[new_pos - 1] = ' ';
91                  pos = new_pos;
92                  num_users++;
93          }
94 <        fclose(f);
94 >        endutent();
95   #endif
96  
97          /* Remove the extra space at the end, and append a \0. */
98          if (num_users != 0) {
99                  pos--;
100          }
101 <        VECTOR_RESIZE(name_list, pos + 1);
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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines