21 |
|
#ifdef HAVE_CONFIG_H |
22 |
|
#include "config.h" |
23 |
|
#endif |
24 |
< |
#include "statgrab.h" |
24 |
> |
|
25 |
|
#include <stdlib.h> |
26 |
|
#include <stdio.h> |
27 |
|
#include <string.h> |
28 |
– |
#ifdef FREEBSD |
29 |
– |
#include <sys/types.h> |
30 |
– |
#endif |
28 |
|
#include <utmp.h> |
29 |
+ |
#include "statgrab.h" |
30 |
|
|
31 |
< |
#define START_VAL 5 |
31 |
> |
#define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE)) |
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; |
36 |
> |
static user_stat_t user_stats; |
37 |
> |
static int size_of_namelist=-1; |
38 |
> |
char *tmp; |
39 |
|
struct utmp *entry; |
40 |
|
|
42 |
– |
name *name_ptr; |
43 |
– |
|
41 |
|
/* First case call */ |
42 |
< |
if (watermark==-1){ |
43 |
< |
user_stat.name_list=malloc(START_VAL * sizeof *user_stat.name_list); |
44 |
< |
if(user_stat.name_list==NULL){ |
42 |
> |
if (size_of_namelist==-1){ |
43 |
> |
user_stats.name_list=malloc(START_VAL); |
44 |
> |
if(user_stats.name_list==NULL){ |
45 |
|
return NULL; |
46 |
|
} |
47 |
< |
watermark=START_VAL; |
47 |
> |
size_of_namelist=START_VAL; |
48 |
> |
printf("Initially started at %d\n", size_of_namelist); |
49 |
|
} |
50 |
|
|
51 |
+ |
/* Essentially blank the list, or give it a inital starting string */ |
52 |
+ |
strcpy(user_stats.name_list, ""); |
53 |
|
setutent(); |
54 |
|
while((entry=getutent()) != NULL) { |
55 |
|
if(entry->ut_type==USER_PROCESS) { |
56 |
< |
if(num_users>watermark-1){ |
57 |
< |
name_ptr=user_stat.name_list; |
58 |
< |
if((user_stat.name_list=realloc(user_stat.name_list, (watermark*2* sizeof *user_stat.name_list)))==NULL){ |
59 |
< |
user_stat.name_list=name_ptr; |
56 |
> |
if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){ |
57 |
> |
tmp=user_stats.name_list; |
58 |
> |
user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2)); |
59 |
> |
printf("realloc to %d\n", 1+(size_of_namelist*2)); |
60 |
> |
if(user_stats.name_list==NULL){ |
61 |
> |
printf("realloc failed\n"); |
62 |
> |
user_stats.name_list=tmp; |
63 |
|
return NULL; |
64 |
|
} |
65 |
< |
watermark=watermark*2; |
65 |
> |
size_of_namelist=1+(size_of_namelist*2); |
66 |
|
} |
67 |
|
|
68 |
< |
strncpy(user_stat.name_list[num_users], entry->ut_user, MAX_LOGIN_NAME_SIZE); |
69 |
< |
/* NULL terminate just in case , the size of user_stat.name_list is MAX_LOGIN_NAME_SIZE+1 */ |
67 |
< |
|
68 |
< |
user_stat.name_list[num_users][MAX_LOGIN_NAME_SIZE]='\0'; |
69 |
< |
|
68 |
> |
strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE); |
69 |
> |
strcat(user_stats.name_list, " "); |
70 |
|
num_users++; |
71 |
|
} |
72 |
|
} |
73 |
|
endutent(); |
74 |
– |
user_stat.num_entries=num_users; |
74 |
|
|
75 |
< |
return &user_stat; |
75 |
> |
/* We want to remove the last " " */ |
76 |
> |
tmp=strrchr(user_stats.name_list, ' '); |
77 |
> |
*tmp='\0'; |
78 |
> |
user_stats.num_entries=num_users; |
79 |
> |
|
80 |
> |
return &user_stats; |
81 |
|
|
82 |
|
} |