| 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 |
|
#include <utmp.h> |
| 43 |
|
#ifdef CYGWIN |
| 44 |
|
#include <sys/unistd.h> |
| 45 |
|
#endif |
| 46 |
|
|
| 40 |
– |
#ifdef SOLARIS |
| 41 |
– |
#define MAX_LOGIN_NAME_SIZE 8 |
| 42 |
– |
#endif |
| 43 |
– |
#if defined(LINUX) || defined(FREEBSD) |
| 44 |
– |
#define MAX_LOGIN_NAME_SIZE UT_NAMESIZE |
| 45 |
– |
#endif |
| 46 |
– |
#ifdef NETBSD |
| 47 |
– |
#define MAX_LOGIN_NAME_SIZE _POSIX_LOGIN_NAME_MAX |
| 48 |
– |
#endif |
| 49 |
– |
#if defined(CYGWIN) |
| 50 |
– |
#define MAX_LOGIN_NAME_SIZE _SC_LOGIN_NAME_MAX |
| 51 |
– |
#endif |
| 52 |
– |
|
| 53 |
– |
#define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE)) |
| 54 |
– |
|
| 47 |
|
user_stat_t *get_user_stats(){ |
| 48 |
< |
int num_users=0; |
| 49 |
< |
|
| 48 |
> |
int num_users = 0, pos = 0, new_pos; |
| 49 |
> |
VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL); |
| 50 |
|
static user_stat_t user_stats; |
| 59 |
– |
static int size_of_namelist=-1; |
| 60 |
– |
char *tmp; |
| 51 |
|
#if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN) |
| 52 |
|
struct utmp *entry; |
| 53 |
|
#endif |
| 56 |
|
FILE *f; |
| 57 |
|
#endif |
| 58 |
|
|
| 69 |
– |
/* First case call */ |
| 70 |
– |
if (size_of_namelist==-1){ |
| 71 |
– |
user_stats.name_list=malloc(START_VAL); |
| 72 |
– |
if(user_stats.name_list==NULL){ |
| 73 |
– |
return NULL; |
| 74 |
– |
} |
| 75 |
– |
size_of_namelist=START_VAL; |
| 76 |
– |
} |
| 77 |
– |
|
| 78 |
– |
/* Essentially blank the list, or give it a inital starting string */ |
| 79 |
– |
strcpy(user_stats.name_list, ""); |
| 80 |
– |
|
| 59 |
|
#if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN) |
| 60 |
|
setutent(); |
| 61 |
|
while((entry=getutent()) != NULL) { |
| 62 |
< |
if(entry->ut_type==USER_PROCESS) { |
| 85 |
< |
if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){ |
| 86 |
< |
tmp=user_stats.name_list; |
| 87 |
< |
user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2)); |
| 88 |
< |
if(user_stats.name_list==NULL){ |
| 89 |
< |
user_stats.name_list=tmp; |
| 90 |
< |
return NULL; |
| 91 |
< |
} |
| 92 |
< |
size_of_namelist=1+(size_of_namelist*2); |
| 93 |
< |
} |
| 62 |
> |
if (entry->ut_type != USER_PROCESS) continue; |
| 63 |
|
|
| 64 |
< |
strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE); |
| 65 |
< |
strcat(user_stats.name_list, " "); |
| 66 |
< |
num_users++; |
| 64 |
> |
new_pos = pos + strlen(entry->ut_user) + 1; |
| 65 |
> |
if (VECTOR_RESIZE(name_list, new_pos) < 0) { |
| 66 |
> |
return NULL; |
| 67 |
|
} |
| 68 |
+ |
|
| 69 |
+ |
strcpy(name_list + pos, entry->ut_user); |
| 70 |
+ |
name_list[new_pos - 1] = ' '; |
| 71 |
+ |
pos = new_pos; |
| 72 |
+ |
num_users++; |
| 73 |
|
} |
| 74 |
|
endutent(); |
| 75 |
|
#endif |
| 79 |
|
} |
| 80 |
|
while((fread(&entry, sizeof(entry),1,f)) != 0){ |
| 81 |
|
if (entry.ut_name[0] == '\0') continue; |
| 82 |
< |
if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){ |
| 83 |
< |
tmp=user_stats.name_list; |
| 84 |
< |
user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2)); |
| 85 |
< |
if(user_stats.name_list==NULL){ |
| 112 |
< |
user_stats.name_list=tmp; |
| 113 |
< |
return NULL; |
| 114 |
< |
} |
| 115 |
< |
size_of_namelist=1+(size_of_namelist*2); |
| 116 |
< |
|
| 82 |
> |
|
| 83 |
> |
new_pos = pos + strlen(entry.ut_name) + 1; |
| 84 |
> |
if (VECTOR_RESIZE(name_list, new_pos) < 0) { |
| 85 |
> |
return NULL; |
| 86 |
|
} |
| 87 |
< |
strncat(user_stats.name_list, entry.ut_name, MAX_LOGIN_NAME_SIZE); |
| 88 |
< |
strcat(user_stats.name_list, " "); |
| 87 |
> |
|
| 88 |
> |
strcpy(name_list + pos, entry.ut_name); |
| 89 |
> |
name_list[new_pos - 1] = ' '; |
| 90 |
> |
pos = new_pos; |
| 91 |
|
num_users++; |
| 92 |
|
} |
| 93 |
|
fclose(f); |
| 94 |
+ |
#endif |
| 95 |
|
|
| 96 |
< |
#endif |
| 97 |
< |
/* We want to remove the last " " */ |
| 98 |
< |
if(num_users!=0){ |
| 127 |
< |
tmp=strrchr(user_stats.name_list, ' '); |
| 128 |
< |
if(tmp!=NULL){ |
| 129 |
< |
*tmp='\0'; |
| 130 |
< |
user_stats.num_entries=num_users; |
| 131 |
< |
} |
| 96 |
> |
/* Remove the extra space at the end, and append a \0. */ |
| 97 |
> |
if (num_users != 0) { |
| 98 |
> |
pos--; |
| 99 |
|
} |
| 100 |
+ |
if (VECTOR_RESIZE(name_list, pos + 1) < 0) { |
| 101 |
+ |
return NULL; |
| 102 |
+ |
} |
| 103 |
+ |
name_list[pos] = '\0'; |
| 104 |
|
|
| 105 |
+ |
user_stats.num_entries = num_users; |
| 106 |
+ |
user_stats.name_list = name_list; |
| 107 |
|
return &user_stats; |
| 135 |
– |
|
| 108 |
|
} |