Module Name:    src
Committed By:   christos
Date:           Tue Jan 31 16:30:40 UTC 2012

Modified Files:
        src/usr.bin/lastcomm: lastcomm.1 lastcomm.c

Log Message:
PR/45897: Takahiro Kambe: Make default display < 80 characters and add -w flag
to display unlimited


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/usr.bin/lastcomm/lastcomm.1
cvs rdiff -u -r1.21 -r1.22 src/usr.bin/lastcomm/lastcomm.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/lastcomm/lastcomm.1
diff -u src/usr.bin/lastcomm/lastcomm.1:1.11 src/usr.bin/lastcomm/lastcomm.1:1.12
--- src/usr.bin/lastcomm/lastcomm.1:1.11	Fri Dec 22 03:00:20 2006
+++ src/usr.bin/lastcomm/lastcomm.1	Tue Jan 31 11:30:40 2012
@@ -1,4 +1,4 @@
-.\"	$NetBSD: lastcomm.1,v 1.11 2006/12/22 08:00:20 ad Exp $
+.\"	$NetBSD: lastcomm.1,v 1.12 2012/01/31 16:30:40 christos Exp $
 .\"
 .\" Copyright (c) 1980, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"	@(#)lastcomm.1	8.1 (Berkeley) 6/6/93
 .\"
-.Dd December 22, 2006
+.Dd January 31, 2012
 .Dt LASTCOMM 1
 .Os
 .Sh NAME
@@ -37,6 +37,7 @@
 .Nd show last commands executed in reverse order
 .Sh SYNOPSIS
 .Nm
+.Op Fl w
 .Op Fl f Ar file
 .Op Ar command ...
 .Op Ar user ...
@@ -57,6 +58,8 @@ Read from
 .Ar file
 rather than the default
 accounting file.
+.It Fl w
+Use as many columns as needed to print the output instead of limiting it to 80.
 .El
 .Pp
 If called with arguments, only accounting entries with a

Index: src/usr.bin/lastcomm/lastcomm.c
diff -u src/usr.bin/lastcomm/lastcomm.c:1.21 src/usr.bin/lastcomm/lastcomm.c:1.22
--- src/usr.bin/lastcomm/lastcomm.c:1.21	Sun Apr 12 09:08:31 2009
+++ src/usr.bin/lastcomm/lastcomm.c	Tue Jan 31 11:30:40 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: lastcomm.c,v 1.21 2009/04/12 13:08:31 lukem Exp $	*/
+/*	$NetBSD: lastcomm.c,v 1.22 2012/01/31 16:30:40 christos Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 19
 #if 0
 static char sccsid[] = "@(#)lastcomm.c	8.2 (Berkeley) 4/29/95";
 #endif
-__RCSID("$NetBSD: lastcomm.c,v 1.21 2009/04/12 13:08:31 lukem Exp $");
+__RCSID("$NetBSD: lastcomm.c,v 1.22 2012/01/31 16:30:40 christos Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -64,11 +64,9 @@ __RCSID("$NetBSD: lastcomm.c,v 1.21 2009
 static time_t	 	 expand(u_int);
 static char		*flagbits(int);
 static const char	*getdev(dev_t);
-static int	 	 requested(char *[], struct acct *);
+static int	 	 requested(char *[], const struct acct *);
 static void	 	 usage(void) __dead;
 
-int	main(int, char **);
-
 int
 main(int argc, char *argv[])
 {
@@ -79,16 +77,21 @@ main(int argc, char *argv[])
 	off_t size;
 	time_t t;
 	double delta;
-	int ch;
+	int ch, wflag, lwidth;
 	const char *acctfile = _PATH_ACCT;
 
 	setprogname(argv[0]);
+	wflag = 0;
+	lwidth = -6;
 
-	while ((ch = getopt(argc, argv, "f:")) != -1)
+	while ((ch = getopt(argc, argv, "f:w")) != -1)
 		switch((char)ch) {
 		case 'f':
 			acctfile = optarg;
 			break;
+		case 'w':
+			wflag = 1;
+			break;
 		case '?':
 		default:
 			usage();
@@ -104,22 +107,19 @@ main(int argc, char *argv[])
 	 * Round off to integral number of accounting records, probably
 	 * not necessary, but it doesn't hurt.
 	 */
-	size = sb.st_size - sb.st_size % sizeof(struct acct);
+	size = sb.st_size - sb.st_size % sizeof(ab);
 
 	/* Check if any records to display. */
-	if (size < (off_t)sizeof(struct acct))
-		exit(0);
+	if (size < (off_t)sizeof(ab))
+		return 0;
 
-	/*
-	 * Seek to before the last entry in the file; use lseek(2) in case
-	 * the file is bigger than a "long".
-	 */
-	size -= sizeof(struct acct);
-	if (lseek(fileno(fp), size, SEEK_SET) == -1)
+	size -= sizeof(ab);
+	if (fseeko(fp, size, SEEK_SET) == -1)
 		err(1, "%s", acctfile);
 
+	lwidth = (int)fldsiz(acct, ac_comm) - ((wflag)? 0: 6);
 	for (;;) {
-		if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
+		if (fread(&ab, sizeof(ab), 1, fp) != 1)
 			err(1, "%s", acctfile);
 
 		if (ab.ac_comm[0] == '\0') {
@@ -132,32 +132,33 @@ main(int argc, char *argv[])
 					*p = '?';
 		if (!*argv || requested(argv, &ab)) {
 
+			if (!wflag)
+				ab.ac_comm[10] = '\0';
 			t = expand(ab.ac_utime) + expand(ab.ac_stime);
 			(void)printf(
 			    "%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
-			     (int)fldsiz(acct, ac_comm),
-			     (int)fldsiz(acct, ac_comm),
-			     ab.ac_comm, flagbits(ab.ac_flag),
-			     UT_NAMESIZE, UT_NAMESIZE,
-			     user_from_uid(ab.ac_uid, 0), UT_LINESIZE,
-			     UT_LINESIZE, getdev(ab.ac_tty),
-			     t / (double)AHZ, ctime(&ab.ac_btime));
+			    lwidth, lwidth,
+			    ab.ac_comm, flagbits(ab.ac_flag),
+			    UT_NAMESIZE, UT_NAMESIZE,
+			    user_from_uid(ab.ac_uid, 0), UT_LINESIZE,
+			    UT_LINESIZE, getdev(ab.ac_tty),
+			    t / (double)AHZ, ctime(&ab.ac_btime));
 			delta = expand(ab.ac_etime) / (double)AHZ;
-			printf(" (%1.0f:%02.0f:%05.2f)\n",
-			       floor(delta / SECSPERHOUR),
-			       floor(fmod(delta, SECSPERHOUR) / SECSPERMIN),
-			       fmod(delta, SECSPERMIN));
+			(void)printf(" (%1.0f:%02.0f:%05.2f)\n",
+			    floor(delta / SECSPERHOUR),
+			    floor(fmod(delta, SECSPERHOUR) / SECSPERMIN),
+			    fmod(delta, SECSPERMIN));
 		}
 		/* are we at the beginning of the file yet? */
 		if (size == 0)
 			break;
 		/* seek backward over the one we read and the next to read */
-		if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
+		if (fseeko(fp, 2 * -(off_t)sizeof(ab), SEEK_CUR) == -1)
 			err(1, "%s", acctfile);
 		/* and account for its size */
-		size -= sizeof(struct acct);
+		size -= sizeof(ab);
 	}
-	exit(0);
+	return 0;
 }
 
 static time_t
@@ -171,7 +172,7 @@ expand(u_int t)
 		t--;
 		nt <<= 3;
 	}
-	return (nt);
+	return nt;
 }
 
 static char *
@@ -189,44 +190,43 @@ flagbits(int f)
 	BIT(ACORE, 'D');
 	BIT(AXSIG, 'X');
 	*p = '\0';
-	return (flags);
+	return flags;
 }
 
 static int
-requested(char *argv[], struct acct *acp)
+requested(char *argv[], const struct acct *acp)
 {
 	do {
 		if (!strcmp(user_from_uid(acp->ac_uid, 0), *argv))
-			return (1);
+			return 1;
 		if (!strcmp(getdev(acp->ac_tty), *argv))
-			return (1);
+			return 1;
 		if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
-			return (1);
+			return 1;
 	} while (*++argv);
-	return (0);
+	return 0;
 }
 
 static const char *
 getdev(dev_t dev)
 {
-	static dev_t lastdev = (dev_t)-1;
+	static dev_t lastdev = NODEV;
 	static const char *lastname;
 
 	if (dev == NODEV)			/* Special case. */
-		return ("__");
+		return "__";
 	if (dev == lastdev)			/* One-element cache. */
-		return (lastname);
-	lastdev = dev;
-	if ((lastname = devname(dev, S_IFCHR)) == NULL)
-		lastname = "??";
-	return (lastname);
+		return lastname;
+	if ((lastname = devname(dev, S_IFCHR)) != NULL)
+		return lastname;
+	return "??";
 }
 
 static void
 usage(void)
 {
 	(void)fprintf(stderr,
-	    "Usage: %s [ -f file ] [command ...] [user ...] [tty ...]\n",
+	    "Usage: %s [ -f file ] [-w] [command ...] [user ...] [tty ...]\n",
 	    getprogname());
 	exit(1);
 }

Reply via email to