public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 16/16] last: add --present option
Date: Sat, 17 Aug 2013 19:15:21 +0100	[thread overview]
Message-ID: <1376763321-22782-17-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1376763321-22782-1-git-send-email-kerolasa@iki.fi>

The --present option will allow user to easily determine who where logged
in at a given time.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 login-utils/last.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/login-utils/last.c b/login-utils/last.c
index 9f12772..616be97 100644
--- a/login-utils/last.c
+++ b/login-utils/last.c
@@ -250,7 +250,7 @@ static int dns_lookup(char *result, int size, int useip, int32_t *a)
 /*
  *	Show one line of information on screen
  */
-static int list(struct utmp *p, time_t t, int what)
+static int list(struct utmp *p, time_t t, int what, time_t present)
 {
 	time_t		secs, tmp;
 	struct tm	*tm;
@@ -292,6 +292,8 @@ static int list(struct utmp *p, time_t t, int what)
 	 *	Calculate times
 	 */
 	tmp = (time_t)p->ut_time;
+	if (present && (present < tmp || 0 == t || t < present))
+		return 0;
 	if (fulltime) {
 		tm = localtime(&tmp);
 		strftime(logintime, sizeof(logintime), "%Y-%m-%dT%H:%M:%S%z", tm);
@@ -426,6 +428,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 	fputs(_(" -n, --limit <number> how many lines to show\n"), out);
 	fputs(_(" -R, --nohostname     don't display the hostname field\n"), out);
 	fputs(_(" -t, --until <YYYYMMDDHHMMSS>  display the state of the specified time\n"), out);
+	fputs(_(" -p, --present <YYYYMMDDHHMMSS>  display who where present at the specified time\n"), out);
 	fputs(_(" -w, --fullnames      display full user and domain names\n"), out);
 	fputs(_(" -x, --system         display system shutdown entries and run level changes\n"), out);
 
@@ -477,7 +480,8 @@ static time_t parsetm(char *ts)
 	return tm;
 }
 
-static void process_wtmp_file(char *ufile, int lastb, int extended, time_t until)
+static void process_wtmp_file(char *ufile, int lastb, int extended,
+			      time_t until, time_t present)
 {
 	FILE *fp;		/* Filepointer of wtmp file */
 
@@ -552,7 +556,7 @@ static void process_wtmp_file(char *ufile, int lastb, int extended, time_t until
 		lastdate = ut.ut_time;
 
 		if (lastb) {
-			quit = list(&ut, ut.ut_time, R_NORMAL);
+			quit = list(&ut, ut.ut_time, R_NORMAL, present);
 			continue;
 		}
 
@@ -600,7 +604,7 @@ static void process_wtmp_file(char *ufile, int lastb, int extended, time_t until
 		case SHUTDOWN_TIME:
 			if (extended) {
 				strcpy(ut.ut_line, "system down");
-				quit = list(&ut, lastboot, R_NORMAL);
+				quit = list(&ut, lastboot, R_NORMAL, present);
 			}
 			lastdown = lastrch = ut.ut_time;
 			down = 1;
@@ -611,12 +615,12 @@ static void process_wtmp_file(char *ufile, int lastb, int extended, time_t until
 				strcpy(ut.ut_line,
 				ut.ut_type == NEW_TIME ? "new time" :
 					"old time");
-				quit = list(&ut, lastdown, R_TIMECHANGE);
+				quit = list(&ut, lastdown, R_TIMECHANGE, present);
 			}
 			break;
 		case BOOT_TIME:
 			strcpy(ut.ut_line, "system boot");
-			quit = list(&ut, lastdown, R_REBOOT);
+			quit = list(&ut, lastdown, R_REBOOT, present);
 			lastboot = ut.ut_time;
 			down = 1;
 			break;
@@ -624,7 +628,7 @@ static void process_wtmp_file(char *ufile, int lastb, int extended, time_t until
 			x = ut.ut_pid & 255;
 			if (extended) {
 				sprintf(ut.ut_line, "(to lvl %c)", x);
-				quit = list(&ut, lastrch, R_NORMAL);
+				quit = list(&ut, lastrch, R_NORMAL, present);
 			}
 			if (x == '0' || x == '6') {
 				lastdown = ut.ut_time;
@@ -648,7 +652,7 @@ static void process_wtmp_file(char *ufile, int lastb, int extended, time_t until
 					/* Show it */
 					if (c == 0) {
 						quit = list(&ut, p->ut.ut_time,
-							R_NORMAL);
+							R_NORMAL, present);
 						c = 1;
 					}
 					if (p->next) p->next->prev = p->prev;
@@ -673,7 +677,7 @@ static void process_wtmp_file(char *ufile, int lastb, int extended, time_t until
 						c = R_PHANTOM;
 				} else
 					c = whydown;
-				quit = list(&ut, lastboot, c);
+				quit = list(&ut, lastboot, c, present);
 			}
 			/* FALLTHRU */
 
@@ -741,6 +745,7 @@ int main(int argc, char **argv)
 	int extended = 0;	/* Lots of info. */
 
 	time_t until = 0;	/* at what time to stop parsing the file */
+	time_t present = 0;	/* who where present at time_t */
 
 	static const struct option long_opts[] = {
 	      { "limit",	required_argument, NULL, 'n' },
@@ -750,6 +755,7 @@ int main(int argc, char **argv)
 	      { "version",    no_argument,       NULL, 'V' },
 	      { "hostlast",   no_argument,       NULL, 'a' },
 	      { "until",      required_argument, NULL, 't' },
+	      { "present",    required_argument, NULL, 'p' },
 	      { "system",     no_argument,       NULL, 'x' },
 	      { "dns",        no_argument,       NULL, 'd' },
 	      { "ip",         no_argument,       NULL, 'i' },
@@ -764,7 +770,7 @@ int main(int argc, char **argv)
 	atexit(close_stdout);
 
 	while ((c = getopt_long(argc, argv,
-			"hVf:n:RxadFit:0123456789w", long_opts, NULL)) != -1) {
+			"hVf:n:RxadFit:p:0123456789w", long_opts, NULL)) != -1) {
 		switch(c) {
 		case 'h':
 			usage(stdout);
@@ -798,6 +804,11 @@ int main(int argc, char **argv)
 		case 'F':
 			fulltime++;
 			break;
+		case 'p':
+			present = parsetm(optarg);
+			if (present == (time_t) -1)
+				errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg);
+			break;
 		case 't':
 			until = parsetm(optarg);
 			if (until == (time_t) -1)
@@ -836,7 +847,7 @@ int main(int argc, char **argv)
 	}
 
 	for (i = 0; i < altc; i++) {
-		process_wtmp_file(altv[i], lastb, extended, until);
+		process_wtmp_file(altv[i], lastb, extended, until, present);
 		free(altv[i]);
 	}
 	free(altv);
-- 
1.8.3.4


  parent reply	other threads:[~2013-08-17 18:17 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-17 18:15 [PATCH 00/16] pull: additions recent to last(1) work, plus few small changes Sami Kerola
2013-08-17 18:15 ` [PATCH 01/16] build-sys: remove unnecessary environment variable Sami Kerola
2013-08-17 18:15 ` [PATCH 02/16] docs: update TODO Sami Kerola
2013-08-17 18:15 ` [PATCH 03/16] build-sys: complete elvtune removal Sami Kerola
2013-08-17 18:15 ` [PATCH 04/16] docs: be clear which last(1) is deprecated Sami Kerola
2013-08-17 18:15 ` [PATCH 05/16] docs: add long options to last.1 manual page Sami Kerola
2013-08-17 18:15 ` [PATCH 06/16] include: carefulput: print determined char when unprintable char is found Sami Kerola
2013-08-17 18:15 ` [PATCH 07/16] last: use carefulput() for printable character output Sami Kerola
2013-08-17 18:15 ` [PATCH 08/16] last: check expected numeric user input is number Sami Kerola
2013-08-17 18:15 ` [PATCH 09/16] last: make switch cases complete, and inform if impossible occurs Sami Kerola
2013-08-17 18:15 ` [PATCH 10/16] last: prefer enum rather than #definition list Sami Kerola
2013-08-17 18:15 ` [PATCH 11/16] last: remove broken code Sami Kerola
2013-08-17 18:15 ` [PATCH 12/16] last: use as narrow variable scoping as possible Sami Kerola
2013-08-17 18:15 ` [PATCH 13/16] last: use ISO 8601 time format for --fulltimes Sami Kerola
2013-08-17 18:15 ` [PATCH 14/16] last: global variables are initialize automatically to zero Sami Kerola
2013-08-17 18:15 ` [PATCH 15/16] last: allow --file to be defined multiple times Sami Kerola
2013-08-17 18:15 ` Sami Kerola [this message]
2013-08-23 10:07 ` [PATCH 00/16] pull: additions recent to last(1) work, plus few small changes Karel Zak
2013-08-23 10:42   ` Sami Kerola
2013-08-23 11:04     ` Karel Zak
2013-08-23 11:08     ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1376763321-22782-17-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox