diff --git a/tools/aulastlog/Makefile.am b/tools/aulastlog/Makefile.am index a23dd806a..2caf353e4 100644 --- a/tools/aulastlog/Makefile.am +++ b/tools/aulastlog/Makefile.am @@ -23,12 +23,12 @@ CONFIG_CLEAN_FILES = *.loT *.rej *.orig EXTRA_DIST = $(man_MANS) -AM_CPPFLAGS = -I${top_srcdir} -I${top_srcdir}/auparse +AM_CPPFLAGS = -I${top_srcdir} -I${top_srcdir}/auparse -I${top_srcdir}/src AM_CFLAGS = -D_GNU_SOURCE ${WFLAGS} bin_PROGRAMS = aulastlog -noinst_HEADERS = aulastlog-llist.h +noinst_HEADERS = man_MANS = aulastlog.8 -aulastlog_SOURCES = aulastlog.c aulastlog-llist.c +aulastlog_SOURCES = aulastlog.c ${top_srcdir}/src/generic-llist.c aulastlog_LDADD = ${top_builddir}/auparse/libauparse.la aulastlog_DEPENDENCIES = ${top_builddir}/auparse/libauparse.la diff --git a/tools/aulastlog/aulastlog-llist.c b/tools/aulastlog/aulastlog-llist.c deleted file mode 100644 index 2a8aa141a..000000000 --- a/tools/aulastlog/aulastlog-llist.c +++ /dev/null @@ -1,153 +0,0 @@ -/* -* aulastlog-llist.c - Minimal linked list library -* Copyright (c) 2008 Red Hat Inc.. -* All Rights Reserved. -* -* This software may be freely redistributed and/or modified under the -* terms of the GNU General Public License as published by the Free -* Software Foundation; either version 2, or (at your option) any -* later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; see the file COPYING. If not, write to the -* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor -* Boston, MA 02110-1335, USA. -* -* Authors: -* Steve Grubb -*/ - -#include -#include -#include "aulastlog-llist.h" - -void list_create(llist *l) -{ - l->head = NULL; - l->cur = NULL; - l->cnt = 0; -} - -lnode *list_next(llist *l) -{ - if (l->cur == NULL) - return NULL; - l->cur = l->cur->next; - return l->cur; -} - -int list_append(llist *l, lnode *node) -{ - lnode* newnode; - - newnode = malloc(sizeof(lnode)); - if (newnode == NULL) - return 1; - - newnode->sec = node->sec; - newnode->uid = node->uid; - newnode->name = strdup(node->name); - if (node->host) - newnode->host = strdup(node->host); - else - newnode->host = NULL; - if (node->term) - newnode->term = strdup(node->term); - else - newnode->term = NULL; - newnode->item = l->cnt; - newnode->next = NULL; - - // if we are at top, fix this up - if (l->head == NULL) - l->head = newnode; - else // Otherwise add pointer to newnode - l->cur->next = newnode; - - // make newnode current - l->cur = newnode; - l->cnt++; - - return 0; -} - -void list_clear(llist* l) -{ - lnode* nextnode; - register lnode* current; - - current = l->head; - while (current) { - nextnode=current->next; - free(current->name); - free(current->host); - free(current->term); - free(current); - current=nextnode; - } - l->head = NULL; - l->cur = NULL; - l->cnt = 0; -} - -int list_update_login(llist* l, time_t t) -{ - register lnode* cur; - if (l == NULL) - return 0; - - cur=list_get_cur(l); - cur->sec = t; - return 1; -} - -int list_update_host(llist* l, const char *h) -{ - register lnode* cur; - if (l == NULL) - return 0; - - cur=list_get_cur(l); - if (h) { - free(cur->host); - cur->host = strdup(h); - } else - cur->host = NULL; - return 1; -} - -int list_update_term(llist* l, const char *t) -{ - register lnode* cur; - if (l == NULL) - return 0; - - cur=list_get_cur(l); - if (t) { - free(cur->term); - cur->term = strdup(t); - } else - cur->term = NULL; - return 1; -} - -lnode *list_find_uid(llist *l, uid_t uid) -{ - register lnode* node; - - node = l->head; /* start at the beginning */ - while (node) { - if (node->uid == uid) { - l->cur = node; - return node; - } else - node = node->next; - } - return NULL; -} - diff --git a/tools/aulastlog/aulastlog-llist.h b/tools/aulastlog/aulastlog-llist.h deleted file mode 100644 index 79e23b45d..000000000 --- a/tools/aulastlog/aulastlog-llist.h +++ /dev/null @@ -1,66 +0,0 @@ -/* -* aulastlog-llist.h - Header file for aulastlog-llist.c -* Copyright (c) 2008 Red Hat Inc. -* All Rights Reserved. -* -* This software may be freely redistributed and/or modified under the -* terms of the GNU General Public License as published by the Free -* Software Foundation; either version 2, or (at your option) any -* later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; see the file COPYING. If not, write to the -* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor -* Boston, MA 02110-1335, USA. -* -* Authors: -* Steve Grubb -*/ - -#ifndef AULASTLIST_HEADER -#define AULASTLIST_HEADER - -#include - - -/* This is the node of the linked list. message & item are the only elements - * at this time. Any data elements that are per item goes here. */ -typedef struct _lnode{ - time_t sec; // last time uid logged in - uid_t uid; // user ID - char *name; // users name - char *host; // host where logging in from - char *term; // terminal name - unsigned int item; // Which item of the same event - struct _lnode* next; // Next node pointer -} lnode; - -/* This is the linked list head. Only data elements that are 1 per - * event goes here. */ -typedef struct { - lnode *head; // List head - lnode *cur; // Pointer to current node - unsigned int cnt; // How many items in this list -} llist; - -void list_create(llist *l); -static inline void list_first(llist *l) { l->cur = l->head; } -lnode *list_next(llist *l); -static inline lnode *list_get_cur(llist *l) { return l->cur; } -static inline unsigned int list_get_cnt(llist *l) { return l->cnt; } -int list_append(llist *l, lnode *node); -void list_clear(llist* l); -int list_update_login(llist* l, time_t t); -int list_update_host(llist* l, const char *h); -int list_update_term(llist* l, const char *t); - -/* Given a uid, find that record. */ -lnode *list_find_uid(llist *l, uid_t uid); - -#endif - diff --git a/tools/aulastlog/aulastlog.c b/tools/aulastlog/aulastlog.c index 06a11eba1..4154689a4 100644 --- a/tools/aulastlog/aulastlog.c +++ b/tools/aulastlog/aulastlog.c @@ -27,8 +27,108 @@ #include #include #include +#include +#include +#include #include "auparse.h" -#include "aulastlog-llist.h" +#include "generic-llist.h" + +struct login_rec { + time_t sec; + uid_t uid; + char *name; + char *host; + char *term; +}; + +static void *dup_rec(const void *data, size_t size) +{ + const struct login_rec *in = data; + struct login_rec *rec = malloc(sizeof(*rec)); + + if (rec == NULL) + return NULL; + rec->sec = in->sec; + rec->uid = in->uid; + rec->name = strdup(in->name); + if (in->host) + rec->host = strdup(in->host); + else + rec->host = NULL; + if (in->term) + rec->term = strdup(in->term); + else + rec->term = NULL; + return rec; +} + +static void free_rec(void *data) +{ + struct login_rec *rec = data; + + free(rec->name); + free(rec->host); + free(rec->term); + free(rec); +} + +static inline unsigned int list_get_cnt(const llist *l) +{ + return l->cnt; +} + +static int list_update_login(llist *l, time_t t) +{ + struct login_rec *r = list_get_cur(l)->data; + + if (!r) + return 0; + r->sec = t; + return 1; +} + +static int list_update_host(llist *l, const char *h) +{ + struct login_rec *r = list_get_cur(l)->data; + + if (!r) + return 0; + free(r->host); + if (h) + r->host = strdup(h); + else + r->host = NULL; + return 1; +} + +static int list_update_term(llist *l, const char *t) +{ + struct login_rec *r = list_get_cur(l)->data; + + if (!r) + return 0; + free(r->term); + if (t) + r->term = strdup(t); + else + r->term = NULL; + return 1; +} + +static lnode *list_find_uid(llist *l, uid_t uid) +{ + lnode *node = l->head; + + while (node) { + struct login_rec *r = node->data; + if (r->uid == uid) { + l->cur = node; + return node; + } + node = node->next; + } + return NULL; +} static void usage(void) { @@ -66,19 +166,19 @@ int main(int argc, char *argv[]) // Stuff linked lists with all users // This use is OK because docs say local machine only - while ((p = getpwent()) != NULL) { - lnode n; - - n.sec = 0; - n.uid = p->pw_uid; - n.name = p->pw_name; - n.host = NULL; - n.term = NULL; - if (user == NULL) - list_append(&l, &n); - else if (strcmp(user, p->pw_name) == 0) - list_append(&l, &n); - } + while ((p = getpwent()) != NULL) { + struct login_rec n; + + n.sec = 0; + n.uid = p->pw_uid; + n.name = p->pw_name; + n.host = NULL; + n.term = NULL; + if (user == NULL) + list_append(&l, &n, sizeof(n), dup_rec); + else if (strcmp(user, p->pw_name) == 0) + list_append(&l, &n, sizeof(n), dup_rec); + } endpwent(); if (user && list_get_cnt(&l) == 0) { @@ -138,11 +238,11 @@ int main(int argc, char *argv[]) " Latest\n"); list_first(&l); do { - char tmp[48]; - const char *c, *h, *t; - lnode *cur = list_get_cur(&l); - if (cur->sec == 0) - c = "**Never logged in**"; + char tmp[48]; + const char *c, *h, *t; + struct login_rec *cur = list_get_cur(&l)->data; + if (cur->sec == 0) + c = "**Never logged in**"; else { struct tm *btm; @@ -159,13 +259,13 @@ int main(int argc, char *argv[]) printf("%-16s %-12.12s %-26.26s %s\n", cur->name, t, h, c); } while (list_next(&l)); - list_clear(&l); + list_clear(&l, free_rec); return 0; error_exit_2: auparse_destroy(au); error_exit_1: - list_clear(&l); + list_clear(&l, free_rec); return 1; }