diff --git a/src/linux/get_user_count.cpp b/src/linux/get_user_count.cpp index b3e95ccf35e5f2f778fcd1265d3eb39608b79913..7ffe91ec469bde6bd6fd3cb0f64f5d68e39e4f6f 100644 --- a/src/linux/get_user_count.cpp +++ b/src/linux/get_user_count.cpp @@ -29,15 +29,17 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ - #include "agent/linux/get_user_count.h" - #include <set> +#include <string> +#include <set> /* Global variables */ struct utmplist *utmplist = NULL; char *ufile; /* Filename of this file */ time_t lastdate; /* Last date we've seen */ struct tm * timeinfo; +set<string> users; +char yesterday[7], today[7]; /* * Read one utmp entry, return in new format. @@ -126,17 +128,11 @@ static int uread(FILE *fp, struct utmp *u, int *quit) { return 1; } -/* - * Show one line of information on screen - */ -static struct user_log *list(struct utmp *p, time_t t, int what) { +static void list(struct utmp *p, time_t t, int what) { time_t tmp; struct user_log * u = (struct user_log *)malloc(sizeof(struct user_log)); char login[25], logout[25], month[3]; - /* - * Calculate times - */ tmp = (time_t)p->ut_time; timeinfo = gmtime(&tmp); // NOLINT(runtime/threadsafe_fn) strftime(month, 3, "%m", timeinfo); @@ -144,10 +140,10 @@ static struct user_log *list(struct utmp *p, time_t t, int what) { snprintf(u->user, sizeof(u->user), p->ut_name, 0); snprintf(login, sizeof(login), ctime(&tmp), 0); - u->login[2] = month[0]; - u->login[3] = month[1]; u->login[0] = login[22]; u->login[1] = login[23]; + u->login[2] = month[0]; + u->login[3] = month[1]; u->login[4] = login[8] == ' '?'0':login[8]; u->login[5] = login[9]; u->login[6] = '\0'; @@ -173,7 +169,16 @@ static struct user_log *list(struct utmp *p, time_t t, int what) { u->logout[6] = '\0'; break; } - return u; + + if (!strcmp(u->logout, yesterday)) { + users.insert(u->user); + } else if (!strcmp(u->login, yesterday)) { + users.insert(u->user); + } else if (( strcmp(u->login, yesterday) && + strcmp(u->login, today)) && (!strcmp(u->logout, today) || + !strcmp(u->logout, "999999"))) { + users.insert(u->user); + } } static struct user_log * last_modified() { @@ -305,9 +310,7 @@ static struct user_log * last_modified() { /* Show it */ if (c == 0) { quit = 0; - u = list(&ut, p->ut.ut_time, R_NORMAL); - v = u; - v->next = NULL; + list(&ut, p->ut.ut_time, R_NORMAL); c = 1; } if (p->next) p->next->prev = p->prev; @@ -332,9 +335,7 @@ static struct user_log * last_modified() { c = R_PHANTOM; quit = 0; - u = list(&ut, lastboot, c); - v = u; - v->next = NULL; + list(&ut, lastboot, c); } /* FALLTHRU */ @@ -420,11 +421,7 @@ static struct user_log * last_modified() { /* Show it */ if (c == 0) { quit = 0; - w = list(&ut, p->ut.ut_time, R_NORMAL); - c = 1; - v->next = w; - w->next = NULL; - v = w; + list(&ut, p->ut.ut_time, R_NORMAL); } if (p->next) p->next->prev = p->prev; if (p->prev) @@ -448,10 +445,7 @@ static struct user_log * last_modified() { c = R_PHANTOM; quit = 0; - w = list(&ut, lastboot, c); - v->next = w; - w->next = NULL; - v = w; + list(&ut, lastboot, c); } /* FALLTHRU */ @@ -476,16 +470,10 @@ static struct user_log * last_modified() { } fclose(fp); - return u; } int get_user_count() { - struct user_log * u = last_modified(); - struct user_log * i; - time_t rawtime; - char yesterday[7], today[7]; - set<string> users; time(&rawtime); timeinfo = localtime(&rawtime); // NOLINT(runtime/threadsafe_fn) @@ -497,18 +485,7 @@ int get_user_count() { strftime(yesterday, 7, "%y%m%d", timeinfo); yesterday[6] = '\0'; - for (i = u; i; i = u) { - if (!strcmp(i->logout, yesterday)) { - users.insert(i->user); - } else if (!strcmp(i->login, yesterday)) { - users.insert(i->user); - } else if (( strcmp(i->login, yesterday) && - strcmp(i->login, today)) && (!strcmp(i->logout, today) || - !strcmp(i->logout, "999999"))) { - users.insert(i->user); - } - u = i->next; - free(i); - } + last_modified(); + return users.size(); }