ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/user_stats.c
Revision: 1.3
Committed: Fri Feb 28 22:59:35 2003 UTC (21 years, 2 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.2: +2 -4 lines
Log Message:
Tidy up of configure script, and includes.

File Contents

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3     * http://www.i-scream.org.uk
4     * Copyright (C) 2000-2002 i-scream
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * This program 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
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19     */
20    
21     #ifdef HAVE_CONFIG_H
22     #include "config.h"
23     #endif
24 tdb 1.3
25 pajs 1.1 #include <stdlib.h>
26     #include <stdio.h>
27     #include <string.h>
28     #include <utmp.h>
29 tdb 1.3 #include "statgrab.h"
30 pajs 1.1
31     #define START_VAL 5
32    
33     user_stat_t *get_user_stats(){
34     int num_users=0;
35    
36     static user_stat_t user_stat;
37     static int watermark=-1;
38     struct utmp *entry;
39    
40     name *name_ptr;
41    
42     /* First case call */
43     if (watermark==-1){
44     user_stat.name_list=malloc(START_VAL * sizeof *user_stat.name_list);
45     if(user_stat.name_list==NULL){
46     return NULL;
47     }
48     watermark=START_VAL;
49     }
50    
51     setutent();
52     while((entry=getutent()) != NULL) {
53     if(entry->ut_type==USER_PROCESS) {
54     if(num_users>watermark-1){
55     name_ptr=user_stat.name_list;
56     if((user_stat.name_list=realloc(user_stat.name_list, (watermark*2* sizeof *user_stat.name_list)))==NULL){
57     user_stat.name_list=name_ptr;
58     return NULL;
59     }
60     watermark=watermark*2;
61     }
62    
63     strncpy(user_stat.name_list[num_users], entry->ut_user, MAX_LOGIN_NAME_SIZE);
64     /* NULL terminate just in case , the size of user_stat.name_list is MAX_LOGIN_NAME_SIZE+1 */
65    
66     user_stat.name_list[num_users][MAX_LOGIN_NAME_SIZE]='\0';
67    
68     num_users++;
69     }
70     }
71     endutent();
72     user_stat.num_entries=num_users;
73    
74     return &user_stat;
75    
76     }