From owner-freebsd-current@FreeBSD.ORG Fri May 13 12:33:37 2005 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E7AA316A4D0; Fri, 13 May 2005 12:33:36 +0000 (GMT) Received: from lexi.siliconlandmark.com (lexi.siliconlandmark.com [209.69.98.4]) by mx1.FreeBSD.org (Postfix) with ESMTP id 799E643D69; Fri, 13 May 2005 12:33:36 +0000 (GMT) (envelope-from andy@siliconlandmark.com) Received: from lexi.siliconlandmark.com (localhost [127.0.0.1]) j4DCXXFx067873; Fri, 13 May 2005 08:33:33 -0400 (EDT) (envelope-from andy@siliconlandmark.com) Received: from localhost (andy@localhost)j4DCXWvH067870; Fri, 13 May 2005 08:33:33 -0400 (EDT) (envelope-from andy@siliconlandmark.com) X-Authentication-Warning: lexi.siliconlandmark.com: andy owned process doing -bs Date: Fri, 13 May 2005 08:33:32 -0400 (EDT) From: Andre Guibert de Bruet To: Giorgos Keramidas In-Reply-To: <20050512123637.GA2966@orion.daedalusnetworks.priv> Message-ID: <20050513083059.W820@lexi.siliconlandmark.com> References: <20050512123637.GA2966@orion.daedalusnetworks.priv> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Information: Please contact the ISP for more information X-SL-MailScanner: Found to be clean X-SL-SpamCheck: not spam, SpamAssassin (score=-2.547, required 6, autolearn=not spam, AWL 0.05, BAYES_00 -2.60) X-MailScanner-From: andy@siliconlandmark.com cc: freebsd-current@freebsd.org Subject: Re: Trimming top's header to display width X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 May 2005 12:33:37 -0000 On Thu, 12 May 2005, Giorgos Keramidas wrote: > The following patch trims the header printed by top(1) to the > display_width. A lot of people have complained how top's header wraps > around when some columns are too wide, with the more recent thread > related to this being: > > http://lists.freebsd.org/pipermail/freebsd-current/2005-May/050014.html > > Does the diff below look like a good way to fix this? > > %%% > Index: contrib/top/display.c > =================================================================== > RCS file: /tmp/cvsroot/src/contrib/top/display.c,v > retrieving revision 1.1.1.1 > retrieving revision 1.2 > diff -u -r1.1.1.1 -r1.2 > --- contrib/top/display.c 12 May 2005 01:15:45 -0000 1.1.1.1 > +++ contrib/top/display.c 12 May 2005 11:06:21 -0000 1.2 > @@ -626,6 +626,33 @@ > static int header_length; > > /* > + * Trim a header string to the current display width and return a newly > + * allocated area with the trimmed header. > + */ > + > +char * > +trim_header(text) > + > +char *text; > + > +{ > + char *s; > + int width; > + > + s = NULL; Setting s to NULL is useless because malloc returns either NULL on failure or a pointer to the allocated memory segment. > + width = display_width; > + header_length = strlen(text); > + if (header_length >= width) { > + s = malloc((width + 1) * sizeof(char)); > + if (s == NULL) > + return (NULL); > + strncpy(s, text, width); > + s[width] = '\0'; > + } > + return (s); > +} > + > +/* > * *_header(text) - print the header for the process area > * > * Assumptions: cursor is on the previous line and lastline is consistent > @@ -636,7 +663,12 @@ > char *text; > > { > - header_length = strlen(text); > + char *s; > + > + s = trim_header(text); > + if (s != NULL) > + text = s; > + > if (header_status == ON) > { > putchar('\n'); > @@ -647,6 +679,7 @@ > { > header_status = OFF; > } > + free(s); > } > > /*ARGSUSED*/ > @@ -655,6 +688,12 @@ > char *text; /* ignored */ > > { > + char *s; > + > + s = trim_header(text); > + if (s != NULL) > + text = s; > + > if (header_status == ERASE) > { > putchar('\n'); > @@ -662,6 +701,7 @@ > clear_eol(header_length); > header_status = OFF; > } > + free(s); > } > > /* > %%% Other than that pet-peeve, the patch looks good to me... Andy /* Andre Guibert de Bruet * 6f43 6564 7020 656f 2e74 4220 7469 6a20 */ /* Code poet / Sysadmin * 636f 656b 2e79 5320 7379 6461 696d 2e6e */ /* GSM: +1 734 846 8758 * 5520 494e 2058 6c73 7565 6874 002e 0000 */ /* WWW: siliconlandmark.com * Tormenting bytes since 1980. */