[dev] [ubase][PATCH] Add lastlog.1

From: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
Date: Mon, 18 Aug 2014 15:26:30 +0200

---
 Makefile  |  2 ++
 lastlog.1 | 12 ++++++++++
 lastlog.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+)
 create mode 100644 lastlog.1
 create mode 100644 lastlog.c
diff --git a/Makefile b/Makefile
index 5a3f0fd..e54e6fa 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -33,6 +33,7 @@ LIB = \
 	util/tty.o
 
 SRC = \
+	lastlog.c           \
 	chvt.c              \
 	clear.c             \
 	ctrlaltdel.c        \
_AT_@ -80,6 +81,7 @@ SRC = \
 	who.c
 
 MAN1 = \
+	lastlog.1           \
 	chvt.1              \
 	clear.1             \
 	dd.1                \
diff --git a/lastlog.1 b/lastlog.1
new file mode 100644
index 0000000..ef0669b
--- /dev/null
+++ b/lastlog.1
_AT_@ -0,0 +1,12 @@
+.TH LASTLOG 1 ubase-VERSION
+.SH NAME
+\fBlastlog\fR - Show last login of users
+.SH SYPNOSIS
+\fBlastlog\fI [user ...]
+.SH DESCRIPTION
+\fBlastlog\fR Show time, tty, and host (if it was a remote
+connexion) of last login of users. If some user names are passed
+as parameter then information about last login of these users is
+shown, otherwise is shown for all the users in /etc/passwd in the
+order they appear in it.
+
diff --git a/lastlog.c b/lastlog.c
new file mode 100644
index 0000000..f3fd221
--- /dev/null
+++ b/lastlog.c
_AT_@ -0,0 +1,79 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <pwd.h>
+
+#ifdef USE_UTMPX
+# include <utmpx.h>
+#else
+# include <utmp.h>
+#endif
+
+#define LASTLOG  "/var/log/lastlog"
+#define PASSWD   "/etc/passwd"
+
+FILE *last;
+
+void
+lastlog(char *user)
+{
+	struct passwd *pwd;
+	struct lastlog ll;
+
+	if ((pwd = getpwnam(user)) == NULL) {
+		fprintf(stderr, "unknown user: %s\n", user);
+		return;
+	}
+
+	fseek(last, pwd->pw_uid * sizeof(struct lastlog), 0);
+	fread(&ll, sizeof(struct lastlog), 1, last);
+
+	if (ferror(last)) {
+		perror("error reading lastlog");
+		exit(-1);
+	}
+
+	printf("%-8.8s %-8.8s %-16.16s %s",
+	       user, ll.ll_line, ll.ll_host, ctime(&ll.ll_time));
+}
+
+int
+main(int argc, char **argv)
+{
+	FILE *fp;
+	char line[512], *p;
+
+	if ((last = fopen(LASTLOG, "r")) == NULL) {
+	    perror(LASTLOG);
+	    exit(1);
+	}
+
+	if (argc > 1) {
+		while (*++argv)
+			lastlog(*argv);
+	} else {
+		if ((fp = fopen(PASSWD, "r")) == NULL) {
+			perror(PASSWD);
+			exit(1);
+		}
+		while ((fgets(line, sizeof(line), fp)) != NULL) {
+			if ((p = strchr(line, ':')) == NULL) {
+				fputs("incorrect password file", stderr);
+				exit(-1);
+			}
+			*p = '\0';
+			lastlog(line);
+		}
+		if (fclose(fp))
+			perror(PASSWD);
+	}
+
+	fclose(last);
+
+	return 0;
+}
+
-- 
1.9.3
Received on Mon Aug 18 2014 - 15:26:30 CEST

This archive was generated by hypermail 2.3.0 : Mon Aug 18 2014 - 15:36:07 CEST