ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/user_stats.c
Revision: 1.5
Committed: Sun May 19 17:58:58 2002 UTC (22 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.4: +2 -0 lines
Log Message:
fixed bug where the function could only be called once.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * Copyright (C) 2000-2002 i-scream
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #include "ukcprog.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #ifdef FREEBSD
25 #include <sys/types.h>
26 #endif
27 #include <utmp.h>
28
29 char *get_user_stats(){
30 char *xml_user_data;
31 int nousers=0;
32 char *user_list=NULL;
33 char *user_tmp;
34 #if defined(LINUX) || defined(SOLARIS)
35 struct utmp *entry;
36 #endif
37 #ifdef FREEBSD
38 struct utmp entry;
39 FILE *f;
40 #endif
41
42 #if defined(LINUX) || defined(SOLARIS)
43 setutent();
44 while((entry=getutent()) != NULL) {
45 if(entry->ut_type==USER_PROCESS) {
46 user_tmp=user_list;
47 if(user_list==NULL){
48 user_list=strdup(entry->ut_user);
49 }else{
50 user_list=strf("%s %s", user_list, entry->ut_user);
51 }
52 if(user_tmp!=NULL) free(user_tmp);
53 nousers++;
54 }
55 }
56 endutent();
57 #endif
58
59 #ifdef FREEBSD
60 if ((f=fopen(_PATH_UTMP, "r")) == NULL){
61 errf("Failed to open UTMP file (%m)");
62 return NULL;
63 }
64
65 while((fread(&entry, sizeof(entry),1,f)) != 0){
66 if (entry.ut_name[0] == '\0') continue;
67 user_tmp=user_list;
68 if(user_list==NULL){
69 user_list=strdup(entry.ut_name);
70 }else{
71 user_list=strf("%s %s", user_list, entry.ut_name);
72 }
73 if(user_tmp!=NULL) free(user_tmp);
74 nousers++;
75 }
76
77 if ((fclose(f)) != 0) {
78 errf("Failed to close file (%m)");
79 return NULL;
80 }
81
82 #endif
83 if (user_list==NULL) user_list=strdup("");
84 xml_user_data=strf("<users><list>%s</list><count>%d</count></users>",user_list,nousers);
85 if (user_list!=NULL) free(user_list);
86 return xml_user_data;
87 }