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

# Content
1 /*
2 * i-scream libstatgrab
3 * http://www.i-scream.org
4 * Copyright (C) 2000-2004 i-scream
5 *
6 * 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 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * 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 *
21 * $Id: user_stats.c,v 1.27 2004/11/01 18:30:17 tdb Exp $
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include "statgrab.h"
32 #include "vector.h"
33 #ifdef ALLBSD
34 #include <sys/types.h>
35 #endif
36 #if defined(NETBSD) || defined(OPENBSD)
37 #include <limits.h>
38 #endif
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;
60 #ifdef ALLBSD
61 struct utmp entry;
62 FILE *f;
63
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_name) + 1;
72 if (VECTOR_RESIZE(name_list, new_pos) < 0) {
73 return NULL;
74 }
75
76 strcpy(name_list + pos, entry.ut_name);
77 name_list[new_pos - 1] = ' ';
78 pos = new_pos;
79 num_users++;
80 }
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 #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
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_user);
147 name_list[new_pos - 1] = ' ';
148 pos = new_pos;
149 num_users++;
150 }
151 endutent();
152 #endif
153
154 /* Remove the extra space at the end, and append a \0. */
155 if (num_users != 0) {
156 pos--;
157 }
158 if (VECTOR_RESIZE(name_list, pos + 1) < 0) {
159 return NULL;
160 }
161 name_list[pos] = '\0';
162
163 user_stats.num_entries = num_users;
164 user_stats.name_list = name_list;
165 return &user_stats;
166 }