diff --git a/src/linux/get_user_count.cpp b/src/linux/get_user_count.cpp index 134a498128526b490b32092caab32e97be08c71d..12ca8c5e5339421f4aa14c614b0fe5d19be7e0e3 100644 --- a/src/linux/get_user_count.cpp +++ b/src/linux/get_user_count.cpp @@ -31,28 +31,24 @@ */ #include "agent/linux/get_user_count.h" - -struct utmplist *utmplist = NULL; +#include <string> +#include <set> /* Global variables */ -int recsdone = 0; /* Number of records listed */ -int fulltime = 0; /* Print full dates and times */ -int name_len = 8; /* Default print 8 characters of name */ -int domain_len = 16; /* Default print 16 characters of domain */ +struct utmplist *utmplist = NULL; char *ufile; /* Filename of this file */ time_t lastdate; /* Last date we've seen */ - +struct tm * timeinfo; /* * Read one utmp entry, return in new format. * Automatically reposition file pointer. */ static int uread(FILE *fp, struct utmp *u, int *quit) { - static int utsize; + static int utsize, bpos; static char buf[UCHUNKSIZE]; char tmp[1024]; static off_t fpos; - static int bpos; off_t o; if (quit == NULL && u != NULL) { @@ -131,102 +127,26 @@ static int uread(FILE *fp, struct utmp *u, int *quit) { return 1; } -/* - * Get the basename of a filename - */ -static char *mybasename(char *s) { - char *p; - - if ((p = strrchr(s, '/')) != NULL) - p++; - else - p = s; - return p; -} - - -static void month_number(char *orig, char*dest) { - char mon[] = {orig[4], orig[5], orig[6], '\0'}; - - if (!strcmp(mon, "Jan")) { - dest[2] = '0'; - dest[3] = '1'; - return; - } - if (!strcmp(mon, "Feb")) { - dest[2] = '0'; - dest[3] = '2'; - return; - } - if (!strcmp(mon, "Mar")) { - dest[2] = '0'; - dest[3] = '3'; - return; - } - if (!strcmp(mon, "Apr")) { - dest[2] = '0'; - dest[3] = '4'; - return; - } - if (!strcmp(mon, "May")) { - dest[2] = '0'; - dest[3] = '5'; - return; - } - if (!strcmp(mon, "Jun")) { - dest[2] = '0'; - dest[3] = '6'; - return; - } - if (!strcmp(mon, "Jul")) { - dest[2] = '0'; - dest[3] = '7'; - return; - } - if (!strcmp(mon, "Aug")) { - dest[2] = '0'; - dest[3] = '8'; - return; - } - if (!strcmp(mon, "Sep")) { - dest[2] = '0'; - dest[3] = '9'; - return; - } - if (!strcmp(mon, "Oct")) { - dest[2] = '1'; - dest[3] = '0'; - return; - } - if (!strcmp(mon, "Nov")) { - dest[2] = '1'; - dest[3] = '1'; - return; - } - if (!strcmp(mon, "Dec")) { - dest[2] = '1'; - dest[3] = '2'; - return; - } - -} - /* * Show one line of information on screen */ static struct user_log *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]; + char login[25], logout[25], month[3]; /* * Calculate times */ tmp = (time_t)p->ut_time; - - strcpy(u->user, p->ut_name); - strcpy(login, ctime(&tmp)); - month_number(login, u->login); + timeinfo = gmtime(&tmp); // NOLINT(runtime/threadsafe_fn) + strftime(month, 3, "%m", timeinfo); + month[3]='\0'; + + 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[4] = login[8] == ' '?'0':login[8]; @@ -235,24 +155,25 @@ static struct user_log *list(struct utmp *p, time_t t, int what) { switch (what) { case R_NOW: - sprintf(u->logout, "999999"); + snprintf(u->logout, sizeof(u->logout), "999999"); break; case R_PHANTOM: - sprintf(u->logout, "000000"); + snprintf(u->logout, sizeof(u->logout), "000000"); break; case R_NORMAL: - strcpy(logout, ctime(&t)); - month_number(logout, u->logout); + timeinfo = gmtime(&t); // NOLINT(runtime/threadsafe_fn) + strftime(month, 3, "%m", timeinfo); + month[3]='\0'; + snprintf(logout, sizeof(logout), ctime(&t), 0); u->logout[0] = logout[22]; u->logout[1] = logout[23]; + u->logout[2] = month[0]; + u->logout[3] = month[1]; u->logout[4] = logout[8] == ' '?'0':logout[8]; u->logout[5] = logout[9]; u->logout[6] = '\0'; break; } - - recsdone++; - return u; } @@ -261,8 +182,7 @@ static struct user_log * last_modified() { struct utmp ut; /* Current utmp entry */ struct utmp oldut; /* Old utmp entry to check for duplicates */ - struct utmplist *p; /* Pointer into utmplist */ - struct utmplist *next;/* Pointer into utmplist */ + struct utmplist *p, *next; /* Pointer into utmplist */ time_t lastboot = 0; /* Last boottime */ time_t lastdown; /* Last downtime */ @@ -277,8 +197,6 @@ static struct user_log * last_modified() { struct user_log * v = NULL; struct user_log * w = NULL; - fulltime++; - /* * Which file do we want to read? */ @@ -329,7 +247,6 @@ static struct user_log * last_modified() { */ while (!quit && skip) { - if (uread(fp, &ut, &quit) != 1) break; @@ -441,12 +358,10 @@ static struct user_log * last_modified() { if (utmplist) utmplist->prev = p; utmplist = p; break; - } } while (!quit) { - if (uread(fp, &ut, &quit) != 1) break; @@ -558,7 +473,6 @@ static struct user_log * last_modified() { if (utmplist) utmplist->prev = p; utmplist = p; break; - } } @@ -569,15 +483,15 @@ static struct user_log * last_modified() { int get_user_count() { struct user_log * u = last_modified(); struct user_log * i; - int count; time_t rawtime; - struct tm * timeinfo; - char yesterday[7]; + char yesterday[7], today[7]; set<string> users; time(&rawtime); timeinfo = localtime(&rawtime); // NOLINT(runtime/threadsafe_fn) + strftime(today, 7, "%y%m%d", timeinfo); + today[6]='\0'; timeinfo->tm_mday--; mktime(timeinfo); @@ -585,23 +499,17 @@ int get_user_count() { yesterday[6] = '\0'; for (i = u; i; i = u) { - if (!strcmp(i->logout, "999999")) { - if (strcmp(i->login, yesterday) <= 0) { + if (!strcmp(i->logout, yesterday)) { users.insert(i->user); - } - } else { - if (!strcmp(i->login, yesterday)) { + } else if (!strcmp(i->login, yesterday)) { users.insert(i->user); - } else if (strcmp(i->login, yesterday) < 0 && - strcmp(i->logout, yesterday) >= 0) { + } 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); } - - count = users.size(); - - return count; + return users.size(); }