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.24 by tdb, Wed Apr 7 14:53:40 2004 UTC vs.
Revision 1.28 by tdb, Sat Sep 24 13:29:23 2005 UTC

# Line 39 | Line 39
39   #ifdef OPENBSD
40   #include <sys/param.h>
41   #endif
42 + #ifndef WIN32
43   #include <utmp.h>
44 + #endif
45   #ifdef CYGWIN
46   #include <sys/unistd.h>
47   #endif
48 + #ifdef HPUX
49 + #include <utmp.h>
50 + #endif
51 + #ifdef WIN32
52 + #include <windows.h>
53 + #include <lm.h>
54 + #endif
55  
56   sg_user_stats *sg_get_user_stats(){
57          int num_users = 0, pos = 0, new_pos;
58          VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL);
59          static sg_user_stats user_stats;
51 #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
52        struct utmp *entry;
53 #endif
60   #ifdef ALLBSD
61          struct utmp entry;
62          FILE *f;
57 #endif
63  
64 < #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
65 <        setutent();
66 <        while((entry=getutent()) != NULL) {
67 <                if (entry->ut_type != USER_PROCESS) continue;
64 >        if ((f=fopen(_PATH_UTMP, "r")) == NULL){
65 >                sg_set_error_with_errno(SG_ERROR_OPEN, _PATH_UTMP);
66 >                return NULL;
67 >        }
68 >        while((fread(&entry, sizeof(entry),1,f)) != 0){
69 >                if (entry.ut_name[0] == '\0') continue;
70  
71 <                new_pos = pos + strlen(entry->ut_user) + 1;
71 >                new_pos = pos + strlen(entry.ut_name) + 1;
72                  if (VECTOR_RESIZE(name_list, new_pos) < 0) {
73                          return NULL;
74                  }
75  
76 <                strcpy(name_list + pos, entry->ut_user);
76 >                strcpy(name_list + pos, entry.ut_name);
77                  name_list[new_pos - 1] = ' ';
78                  pos = new_pos;
79                  num_users++;
80          }
81 <        endutent();
82 < #endif
83 < #ifdef ALLBSD
84 <        if ((f=fopen(_PATH_UTMP, "r")) == NULL){
85 <                return NULL;
81 >        fclose(f);
82 > #elif defined (WIN32)
83 >        LPWKSTA_USER_INFO_0 buf = NULL;
84 >        LPWKSTA_USER_INFO_0 tmp_buf;
85 >        unsigned long entries_read = 0;
86 >        unsigned long entries_tot = 0;
87 >        unsigned long resumehandle = 0;
88 >        NET_API_STATUS nStatus;
89 >        int i;
90 >        char name[256];
91 >
92 >        do {
93 >                nStatus = NetWkstaUserEnum(NULL, 0, (LPBYTE*)&buf,
94 >                                MAX_PREFERRED_LENGTH, &entries_read,
95 >                                &entries_tot, &resumehandle);
96 >                if((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA)) {
97 >                        if((tmp_buf = buf) == NULL) {
98 >                                continue;
99 >                        }
100 >                        for (i=0; i<entries_read; i++) {
101 >                                //assert(tmp_buf != NULL);
102 >                                if (tmp_buf == NULL) {
103 >                                        sg_set_error(SG_ERROR_PERMISSION, "User list");
104 >                                        break;
105 >                                }
106 >                                /* It's in unicode. We are not. Convert */
107 >                                WideCharToMultiByte(CP_ACP, 0, tmp_buf->wkui0_username, -1, name, sizeof(name), NULL, NULL);
108 >
109 >                                new_pos = pos + strlen(name) + 1;
110 >                                if(VECTOR_RESIZE(name_list, new_pos) < 0) {
111 >                                        NetApiBufferFree(buf);
112 >                                        return NULL;
113 >                                }
114 >                                strcpy(name_list + pos, name);
115 >                                name_list[new_pos - 1] = ' ';
116 >                                pos = new_pos;
117 >
118 >                                tmp_buf++;
119 >                                num_users++;
120 >                        }
121 >                } else {
122 >                        sg_set_error(SG_ERROR_PERMISSION, "User enum");
123 >                        return NULL;
124 >                }
125 >                if (buf != NULL) {
126 >                        NetApiBufferFree(buf);
127 >                        buf=NULL;
128 >                }
129 >        } while (nStatus == ERROR_MORE_DATA);
130 >        if (buf != NULL) {
131 >                NetApiBufferFree(buf);
132          }
133 <        while((fread(&entry, sizeof(entry),1,f)) != 0){
134 <                if (entry.ut_name[0] == '\0') continue;
133 > #else
134 >        /* This works on everything else. */
135 >        struct utmp *entry;
136  
137 <                new_pos = pos + strlen(entry.ut_name) + 1;
137 >        setutent();
138 >        while((entry=getutent()) != NULL) {
139 >                if (entry->ut_type != USER_PROCESS) continue;
140 >
141 >                new_pos = pos + strlen(entry->ut_user) + 1;
142                  if (VECTOR_RESIZE(name_list, new_pos) < 0) {
143                          return NULL;
144                  }
145  
146 <                strcpy(name_list + pos, entry.ut_name);
146 >                strcpy(name_list + pos, entry->ut_user);
147                  name_list[new_pos - 1] = ' ';
148                  pos = new_pos;
149                  num_users++;
150          }
151 <        fclose(f);
151 >        endutent();
152   #endif
153  
154          /* Remove the extra space at the end, and append a \0. */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines