Add last and lastb

This commit is contained in:
Roberto E. Vargas Caballero 2014-10-16 11:27:39 +01:00 committed by sin
parent 249b5b5c0a
commit 2f03742e05
3 changed files with 68 additions and 1 deletions

View File

@ -33,6 +33,7 @@ LIB = \
util/tty.o util/tty.o
SRC = \ SRC = \
last.c \
lastlog.c \ lastlog.c \
chvt.c \ chvt.c \
clear.c \ clear.c \
@ -131,7 +132,7 @@ MAN8 = \
umount.8 umount.8
OBJ = $(SRC:.c=.o) $(LIB) OBJ = $(SRC:.c=.o) $(LIB)
BIN = $(SRC:.c=) BIN = $(SRC:.c=) lastb
all: options binlib all: options binlib
@ -165,6 +166,9 @@ util.a: $(LIB)
@$(AR) -r -c $@ $(LIB) @$(AR) -r -c $@ $(LIB)
@ranlib $@ @ranlib $@
lastb: last
ln -f last lastb
install: all install: all
@echo installing executables to $(DESTDIR)$(PREFIX)/bin @echo installing executables to $(DESTDIR)$(PREFIX)/bin
@mkdir -p $(DESTDIR)$(PREFIX)/bin @mkdir -p $(DESTDIR)$(PREFIX)/bin

View File

@ -5,3 +5,7 @@
#define PW_CIPHER "$6$" /* SHA-512 */ #define PW_CIPHER "$6$" /* SHA-512 */
#undef UTMP_PATH #undef UTMP_PATH
#define UTMP_PATH "/var/run/utmp" #define UTMP_PATH "/var/run/utmp"
#undef BTMP_PATH
#define BTMP_PATH "/var/log/btmp"
#undef WTMP_PATH
#define WTMP_PATH "/var/log/wtmp"

59
last.c Normal file
View File

@ -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;
}