ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/user_stats.c
Revision: 1.25
Committed: Wed Apr 7 21:08:40 2004 UTC (20 years, 1 month ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_10
Changes since 1.24: +2 -1 lines
Log Message:
The rest of the error handling stuff (except the vector code).

I've been extremely unimaginative with the string names in error.c, but
they're all in one place so much easier to tidy up. I'm also beginning to
wonder if we actually needed an SG_ERROR_SYSTEM_CALL to indicate some call
into the system failed - because the majority of our errors are those :-)

Still to do, then:
 - vector code
 - better string names in error.c
 - deal with arg string in some way
 - make use of the error status in statgrab/saidar/examples

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.25 * $Id: user_stats.c,v 1.24 2004/04/07 14:53:40 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 pajs 1.1 #include <utmp.h>
43 tdb 1.16 #ifdef CYGWIN
44     #include <sys/unistd.h>
45     #endif
46 pajs 1.1
47 ats 1.22 sg_user_stats *sg_get_user_stats(){
48 ats 1.20 int num_users = 0, pos = 0, new_pos;
49     VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL);
50 ats 1.22 static sg_user_stats user_stats;
51 ats 1.12 #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
52 pajs 1.1 struct utmp *entry;
53 pajs 1.8 #endif
54 ats 1.10 #ifdef ALLBSD
55 pajs 1.8 struct utmp entry;
56 tdb 1.24 FILE *f;
57 pajs 1.8 #endif
58 pajs 1.1
59 ats 1.12 #if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN)
60 pajs 1.1 setutent();
61     while((entry=getutent()) != NULL) {
62 ats 1.20 if (entry->ut_type != USER_PROCESS) continue;
63    
64     new_pos = pos + strlen(entry->ut_user) + 1;
65     if (VECTOR_RESIZE(name_list, new_pos) < 0) {
66     return NULL;
67 pajs 1.1 }
68 ats 1.20
69     strcpy(name_list + pos, entry->ut_user);
70     name_list[new_pos - 1] = ' ';
71     pos = new_pos;
72     num_users++;
73 pajs 1.1 }
74     endutent();
75 pajs 1.8 #endif
76 ats 1.10 #ifdef ALLBSD
77 pajs 1.8 if ((f=fopen(_PATH_UTMP, "r")) == NULL){
78 tdb 1.25 sg_set_error(SG_ERROR_OPEN, _PATH_UTMP);
79 pajs 1.8 return NULL;
80     }
81     while((fread(&entry, sizeof(entry),1,f)) != 0){
82     if (entry.ut_name[0] == '\0') continue;
83 ats 1.20
84     new_pos = pos + strlen(entry.ut_name) + 1;
85     if (VECTOR_RESIZE(name_list, new_pos) < 0) {
86     return NULL;
87 pajs 1.8 }
88 ats 1.20
89     strcpy(name_list + pos, entry.ut_name);
90     name_list[new_pos - 1] = ' ';
91     pos = new_pos;
92 pajs 1.8 num_users++;
93     }
94     fclose(f);
95 ats 1.20 #endif
96 pajs 1.1
97 ats 1.20 /* Remove the extra space at the end, and append a \0. */
98     if (num_users != 0) {
99     pos--;
100 pajs 1.7 }
101 ats 1.21 if (VECTOR_RESIZE(name_list, pos + 1) < 0) {
102     return NULL;
103     }
104 ats 1.20 name_list[pos] = '\0';
105 pajs 1.5
106 ats 1.20 user_stats.num_entries = num_users;
107     user_stats.name_list = name_list;
108 pajs 1.5 return &user_stats;
109 pajs 1.1 }