Subject: bin/6626: 'pkg_info -Ia' always trims output to 80 columns
To: None <gnats-bugs@gnats.netbsd.org>
From: None <woods@mail.weird.com>
List: netbsd-bugs
Date: 12/21/1998 14:01:29
>Number:         6626
>Category:       bin
>Synopsis:       'pkg_info -Ia' always trims output to 80 columns
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Dec 21 11:05:00 1998
>Last-Modified:
>Originator:     Greg A. Woods
>Organization:
Planix, Inc.; Toronto, Ontario; Canada
>Release:        NetBSD-current sup'ed Mon Nov 16 08:02:37 EST 1998
>Environment:

System: NetBSD 

>Description:

	Pkg_info's index summary report is trimmed to 80 columns,
	regardless of whether or not the terminal is that wide, or
	whether or not the output is to a terminal.

>How-To-Repeat:

	notice that "pkg_info" seems to truncate lines to columns,

	make a wide window and observe that summary lines are still
	truncated to 80 columns

	pipe the output through cat and observe that summary lines are
	also truncated to 80 columns

>Fix:

	Apply the following patch (which uses code borrowed from ls.c):

	(Note this patch also changes main() to use exit() instead of return;)

Index: usr.sbin/pkg_install/info/info.h
===================================================================
RCS file: /cvs/NetBSD/src/usr.sbin/pkg_install/info/info.h,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 info.h
--- usr.sbin/pkg_install/info/info.h	1998/11/16 21:53:41	1.1.1.2
+++ usr.sbin/pkg_install/info/info.h	1998/12/21 17:51:35
@@ -55,6 +55,7 @@
 extern char PlayPen[];
 extern size_t PlayPenSize;
 extern char *CheckPkg;
+extern size_t termwidth;
 
 extern void	show_file(char *, char *);
 extern void	show_plist(char *, package_t *, pl_ent_t);
Index: usr.sbin/pkg_install/info/main.c
===================================================================
RCS file: /cvs/NetBSD/src/usr.sbin/pkg_install/info/main.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 main.c
--- usr.sbin/pkg_install/info/main.c	1998/11/16 21:53:41	1.1.1.2
+++ usr.sbin/pkg_install/info/main.c	1998/12/21 18:04:10
@@ -30,7 +30,11 @@
  *
  */
 
+#include <sys/ioctl.h>
+
+#include <termios.h>
 #include <err.h>
+
 #include "lib.h"
 #include "info.h"
 
@@ -43,6 +47,7 @@
 char PlayPen[FILENAME_MAX];
 size_t PlayPenSize	= sizeof(PlayPen);
 char *CheckPkg		= NULL;
+size_t termwidth	= 0;
 
 static void
 usage(void)
@@ -171,5 +176,18 @@
     if (pkgs == start && !AllInstalled && !CheckPkg)
 	warnx("missing package name(s)"), usage();
     *pkgs = NULL;
-    return pkg_perform(start);
+
+    if (isatty(STDOUT_FILENO)) {
+	const char *p;
+	struct winsize win;
+
+	if ((p = getenv("COLUMNS")) != NULL)
+	    termwidth = atoi(p);
+	else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
+		 win.ws_col > 0)
+	    termwidth = win.ws_col;
+    }
+
+     exit(pkg_perform(start));
+     /* NOTREACHED */
 }
Index: usr.sbin/pkg_install/info/show.c
===================================================================
RCS file: /cvs/NetBSD/src/usr.sbin/pkg_install/info/show.c,v
retrieving revision 1.1.1.2
diff -u -r1.1.1.2 show.c
--- usr.sbin/pkg_install/info/show.c	1998/11/16 21:53:43	1.1.1.2
+++ usr.sbin/pkg_install/info/show.c	1998/12/21 18:51:48
@@ -90,21 +90,25 @@
 show_index(char *title, char *fname)
 {
 	FILE *fp;
-	char line[MAXINDEXSIZE+2];
+	char *line;
+	size_t linelen;
+	size_t maxline = termwidth;
 
 	if (!Quiet) {
 		printf("%s%s", InfoPrefix, title);
+		maxline -= MAXNAMESIZE;
 	}
 	if ((fp = fopen(fname, "r")) == (FILE *) NULL) {
 		warnx("show_file: can't open '%s' for reading", fname);
 		return;
 	}
-	if (fgets(line, MAXINDEXSIZE+1, fp)) {
-		if (line[MAXINDEXSIZE-1] != '\n') {
-			line[MAXINDEXSIZE] = '\n';
+	if ((line = fgetln(fp, &linelen))) {
+		line[linelen - 1] = '\0'; /* tromp newline & terminate string */
+		if (termwidth && (linelen > maxline)) {
+			/* XXX -1 if term does NOT have xn (or xenl) quirk */
+			line[maxline] = '\0';
 		}
-		line[MAXINDEXSIZE+1] = 0;
-		(void) fputs(line, stdout);
+		(void) printf("%s\n", line);
 	}
 	(void) fclose(fp);
 }
>Audit-Trail:
>Unformatted: