#! /bin/sh -e
## 50-lastcomm-forwards.dpatch by dean gaudet <dean@arctic.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: lastcomm support for --forwards and -f - to read from pipes/stdin

if [ $# -lt 1 ]; then
    echo "`basename $0`: script expects -patch|-unpatch as argument" >&2
    exit 1
fi

[ -f debian/patches/00patch-opts ] && . debian/patches/00patch-opts
patch_opts="${patch_opts:--f --no-backup-if-mismatch} ${2:+-d $2}"

case "$1" in
    -patch) patch -p1 ${patch_opts} < $0;;
    -unpatch) patch -R -p1 ${patch_opts} < $0;;
    *)
        echo "`basename $0`: script expects -patch|-unpatch as argument" >&2
        exit 1;;
esac

exit 0

@DPATCH@
diff -urNad /home/dean/tmp/acct/acct-6.3.5/lastcomm.1 acct-6.3.5/lastcomm.1
--- /home/dean/tmp/acct/acct-6.3.5/lastcomm.1	1998-03-22 22:26:12.000000000 -0800
+++ acct-6.3.5/lastcomm.1	2004-07-31 14:55:41.000000000 -0700
@@ -23,6 +23,10 @@
 ]
 .br
 [
+.B \-\-forwards
+]
+.br
+[
 .BI \-f " filename"
 |
 .BI \-\-file " filename"
@@ -126,11 +130,26 @@
 List records for tty
 .I name.
 .TP
+.BI \-\-forwards
+Read file forwards instead of backwards.  This avoids trying to seek
+on the file and can be used to read from a pipe.  This must be specified
+prior to any
+.BI \-f
+arguments.
+.TP
 .BI \-f " filename, " \-\-file " filename"
 Read from the file 
 .I filename 
 instead of
-.I acct
+.I acct.
+A filename of "-" will result in reading from stdin.
+This must either be the first
+.BI \-f
+option, or
+.BI \-\-forwards
+must precede all
+.BI \-f
+options.
 .TP
 .BI \-\-ahz " hz"
 Use this flag to tell the program what
diff -urNad /home/dean/tmp/acct/acct-6.3.5/lastcomm.c acct-6.3.5/lastcomm.c
--- /home/dean/tmp/acct/acct-6.3.5/lastcomm.c	1998-04-08 16:24:49.000000000 -0700
+++ acct-6.3.5/lastcomm.c	2004-07-31 14:55:41.000000000 -0700
@@ -97,13 +97,10 @@
   int c;
   int other_pacct_file_specified = 0; /* nonzero if the user used the
 					 `-f' or `--file' flag */
+  int backwards = 1; /* default to reading backwards */
 
   program_name = argv[0];
   
-  /* Tell the pacct reader that we want to do things backwards! */
-
-  pacct_init (1);
-
   while (1)
     {
       int option_index = 0;
@@ -119,6 +116,7 @@
 	{ "tty", required_argument, NULL, 8 },
 	{ "command", required_argument, NULL, 9 },
 	{ "ahz", required_argument, NULL, 10 },
+	{ "forwards", no_argument, NULL, 11 },
 	{ 0, 0, 0, 0 }
       };
       
@@ -140,6 +138,18 @@
 	  break;
 	case 'f':
 	case 4:
+	  if (strcmp(optarg, "-") == 0)
+	    {
+	      if (backwards && other_pacct_file_specified)
+		{
+		  printf ("%s: -f - must be specified first, or --forwards must precede the first -f\n", program_name);
+		  exit (1);
+		}
+	      backwards = 0;
+	      optarg = "/proc/self/fd/0";
+	    }
+	  if (other_pacct_file_specified == 0)
+	    pacct_init(backwards);
 	  add_pacct_file (optarg);
 	  other_pacct_file_specified = 1;
 	  break;
@@ -174,6 +184,14 @@
 	    }
 
 	  break;
+	case 11:
+	  if (other_pacct_file_specified)
+	    {
+	      printf ("%s: --forwards must come before -f\n", program_name);
+	      exit (1);
+	    }
+	  backwards = 0;
+	  break;
 	case 'h':
 	case 3:
 	  /* This should fall through to default! */
@@ -184,6 +202,9 @@
 	}
     }
 
+  if (other_pacct_file_specified == 0)
+    pacct_init(backwards);
+
   /* if we get here, we expect everything else to be a username,
      terminal name, or command name */
 
@@ -259,7 +280,7 @@
 {
   char *usage = "\
 Usage: %s [-hV] [-f file] [command] ... [user] ... [terminal] ...\n\
-       [--file <file>] [--strict-match] [--print-controls]\n\
+       [--forwards] [--file <file>] [--strict-match] [--print-controls]\n\
        [--user <name>] [--tty <name>] [--command <name>] [--debug]\n\
        [--version] [--help]\n\
 ";
