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.26 by ats, Sun Jul 18 21:30:12 2004 UTC vs.
Revision 1.29 by tdb, Sat Feb 20 17:55:04 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines