[hackers] [ubase] Add last and lastb || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Thu, 16 Oct 2014 12:28:19 +0200

commit 2f03742e05db4657dcef27541670e75620c7f633
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
Date: Thu Oct 16 11:27:39 2014 +0100

    Add last and lastb

diff --git a/Makefile b/Makefile
index 3989a77..07c2de0 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -33,6 +33,7 @@ LIB = \
         util/tty.o
 
 SRC = \
+ last.c \
         lastlog.c \
         chvt.c \
         clear.c \
_AT_@ -131,7 +132,7 @@ MAN8 = \
         umount.8
 
 OBJ = $(SRC:.c=.o) $(LIB)
-BIN = $(SRC:.c=)
+BIN = $(SRC:.c=) lastb
 
 all: options binlib
 
_AT_@ -165,6 +166,9 @@ util.a: $(LIB)
         _AT_$(AR) -r -c $@ $(LIB)
         _AT_ranlib $@
 
+lastb: last
+ ln -f last lastb
+
 install: all
         _AT_echo installing executables to $(DESTDIR)$(PREFIX)/bin
         _AT_mkdir -p $(DESTDIR)$(PREFIX)/bin
diff --git a/config.def.h b/config.def.h
index 1ea5ca5..577833e 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -5,3 +5,7 @@
 #define PW_CIPHER "$6$" /* SHA-512 */
 #undef UTMP_PATH
 #define UTMP_PATH "/var/run/utmp"
+#undef BTMP_PATH
+#define BTMP_PATH "/var/log/btmp"
+#undef WTMP_PATH
+#define WTMP_PATH "/var/log/wtmp"
diff --git a/last.c b/last.c
new file mode 100644
index 0000000..14fb386
--- /dev/null
+++ b/last.c
_AT_@ -0,0 +1,59 @@
+/* See LICENSE file for copyright and license details. */
+#include <errno.h>
+#include <paths.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <utmp.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "util.h"
+
+void
+usage(void)
+{
+ fputs("last [user]\n", stderr);
+ exit(1);
+}
+
+int
+main(int argc, char **argv)
+{
+ FILE *fp;
+ struct utmp ut;
+ char *user, *file, *prog;
+ time_t t;
+
+ switch (argc) {
+ case 1:
+ user = NULL;
+ break;
+ case 2:
+ user = argv[1];
+ break;
+ default:
+ usage();
+ }
+
+ prog = basename(argv[0]);
+ file = (!strcmp(prog, "last")) ? WTMP_PATH : BTMP_PATH;
+ if ((fp = fopen(file, "r")) == NULL)
+ eprintf("fopen %s:", file);
+
+ while (fread(&ut, sizeof(ut), 1, fp) == 1) {
+ if (ut.ut_type != USER_PROCESS ||
+ (user && strcmp(user, ut.ut_name))) {
+ continue;
+ }
+
+ t = ut.ut_time;
+ printf("%-8.8s %-8.8s %-16.16s %s",
+ ut.ut_user, ut.ut_line, ut.ut_host, ctime(&t));
+ }
+ if (fclose(fp))
+ eprintf("fclose %s:", file);
+ return 0;
+}
Received on Thu Oct 16 2014 - 12:28:19 CEST

This archive was generated by hypermail 2.3.0 : Thu Oct 16 2014 - 12:36:10 CEST