ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/user_stats.c
Revision: 1.28
Committed: Sat Sep 24 13:29:23 2005 UTC (18 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_16, LIBSTATGRAB_0_15, LIBSTATGRAB_0_14, LIBSTATGRAB_0_13
Changes since 1.27: +58 -1 lines
Log Message:
Add WIN32 support via MINGW. We'll need to add stuff to the README file
about what this requires to build.

All the hard work done by: skel

File Contents

# User Rev Content
1 tdb 1.14 /*
2 tdb 1.23 * i-scream libstatgrab
3 tdb 1.9 * http://www.i-scream.org
4 tdb 1.14 * Copyright (C) 2000-2004 i-scream
5 pajs 1.1 *
6 tdb 1.14 * This library is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public
8     * License as published by the Free Software Foundation; either
9     * version 2.1 of the License, or (at your option) any later version.
10 pajs 1.1 *
11 tdb 1.14 * This library is distributed in the hope that it will be useful,
12 pajs 1.1 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 tdb 1.14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15 pajs 1.1 *
16 tdb 1.14 * You should have received a copy of the GNU Lesser General Public
17     * License along with this library; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19     * 02111-1307 USA
20 tdb 1.15 *
21 tdb 1.28 * $Id: user_stats.c,v 1.27 2004/11/01 18:30:17 tdb Exp $
22 pajs 1.1 */
23    
24     #ifdef HAVE_CONFIG_H
25     #include "config.h"
26     #endif
27 tdb 1.3
28 pajs 1.1 #include <stdlib.h>
29     #include <stdio.h>
30     #include <string.h>
31 pajs 1.8 #include "statgrab.h"
32 ats 1.20 #include "vector.h"
33 ats 1.10 #ifdef ALLBSD
34 pajs 1.8 #include <sys/types.h>
35 ats 1.17 #endif
36 tdb 1.18 #if defined(NETBSD) || defined(OPENBSD)
37 ats 1.17 #include <limits.h>
38 pajs 1.8 #endif
39 tdb 1.18 #ifdef OPENBSD
40     #include <sys/param.h>
41     #endif
42 tdb 1.28 #ifndef WIN32
43 pajs 1.1 #include <utmp.h>
44 tdb 1.28 #endif
45 tdb 1.16 #ifdef CYGWIN
46     #include <sys/unistd.h>
47     #endif
48 tdb 1.27 #ifdef HPUX
49     #include <utmp.h>
50     #endif
51 tdb 1.28 #ifdef WIN32
52     #include <windows.h>
53     #include <lm.h>
54     #endif
55 pajs 1.1
56 ats 1.22 sg_user_stats *sg_get_user_stats(){
57 ats 1.20 int num_users = 0, pos = 0, new_pos;
58     VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL);
59 ats 1.22 static sg_user_stats user_stats;
60 ats 1.10 #ifdef ALLBSD
61 pajs 1.8 struct utmp entry;
62 tdb 1.24 FILE *f;
63 pajs 1.1
64 tdb 1.27 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 ats 1.20
71 tdb 1.27 new_pos = pos + strlen(entry.ut_name) + 1;
72 ats 1.20 if (VECTOR_RESIZE(name_list, new_pos) < 0) {
73     return NULL;
74 pajs 1.1 }
75 ats 1.20
76 tdb 1.27 strcpy(name_list + pos, entry.ut_name);
77 ats 1.20 name_list[new_pos - 1] = ' ';
78     pos = new_pos;
79     num_users++;
80 pajs 1.1 }
81 tdb 1.27 fclose(f);
82 tdb 1.28 #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 tdb 1.27 #else
134     /* This works on everything else. */
135     struct utmp *entry;
136    
137     setutent();
138     while((entry=getutent()) != NULL) {
139     if (entry->ut_type != USER_PROCESS) continue;
140 ats 1.20
141 tdb 1.27 new_pos = pos + strlen(entry->ut_user) + 1;
142 ats 1.20 if (VECTOR_RESIZE(name_list, new_pos) < 0) {
143     return NULL;
144 pajs 1.8 }
145 ats 1.20
146 tdb 1.27 strcpy(name_list + pos, entry->ut_user);
147 ats 1.20 name_list[new_pos - 1] = ' ';
148     pos = new_pos;
149 pajs 1.8 num_users++;
150     }
151 tdb 1.27 endutent();
152 ats 1.20 #endif
153 pajs 1.1
154 ats 1.20 /* Remove the extra space at the end, and append a \0. */
155     if (num_users != 0) {
156     pos--;
157 pajs 1.7 }
158 ats 1.21 if (VECTOR_RESIZE(name_list, pos + 1) < 0) {
159     return NULL;
160     }
161 ats 1.20 name_list[pos] = '\0';
162 pajs 1.5
163 ats 1.20 user_stats.num_entries = num_users;
164     user_stats.name_list = name_list;
165 pajs 1.5 return &user_stats;
166 pajs 1.1 }