git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] grep: make show_line more portable
@ 2009-03-09  1:15 Brian Gernhardt
  2009-03-09  1:35 ` Junio C Hamano
  2009-03-09  2:22 ` Jay Soffian
  0 siblings, 2 replies; 8+ messages in thread
From: Brian Gernhardt @ 2009-03-09  1:15 UTC (permalink / raw)
  To: Git List; +Cc: Junio C Hamano

On OS X the printf specifier "%.0s" outputs the entire string instead
of 0 characters as POSIX states.

In addition, for * width or precision printf expects an integer
argument.  On systems were regoff_t is 64-bit, unexpected results can
occur.

To fix these, use if statements to catch 0 precisions and casts to
convert regoff_t to int.

Signed-off-by: Brian Gernhardt <benji@silverinsanity.com>
---
 grep.c |   16 ++++++++++------
 1 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/grep.c b/grep.c
index cace1c8..ec68200 100644
--- a/grep.c
+++ b/grep.c
@@ -489,18 +489,22 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
 
 		*eol = '\0';
 		while (next_match(opt, bol, eol, ctx, &match, eflags)) {
-			printf("%.*s%s%.*s%s",
-			       match.rm_so, bol,
-			       opt->color_match,
-			       match.rm_eo - match.rm_so, bol + match.rm_so,
-			       GIT_COLOR_RESET);
+			if( match.rm_so > 0 )
+				printf( "%.*s", (int) match.rm_so, bol );
+			if( match.rm_eo > match.rm_so )
+				printf("%s%.*s%s",
+					   opt->color_match,
+					  (int) (match.rm_eo - match.rm_so), bol + match.rm_so,
+					   GIT_COLOR_RESET);
 			bol += match.rm_eo;
 			rest -= match.rm_eo;
 			eflags = REG_NOTBOL;
 		}
 		*eol = ch;
 	}
-	printf("%.*s\n", rest, bol);
+	if( rest > 0 )
+		printf("%.*s", rest, bol);
+	printf("\n");
 }
 
 static int grep_buffer_1(struct grep_opt *opt, const char *name,
-- 
1.6.2.222.g01cbd

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-03-09 19:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-09  1:15 [PATCH] grep: make show_line more portable Brian Gernhardt
2009-03-09  1:35 ` Junio C Hamano
2009-03-09  2:22 ` Jay Soffian
2009-03-09  2:23   ` Jay Soffian
2009-03-09  2:44   ` Brian Gernhardt
2009-03-09  3:52     ` Junio C Hamano
2009-03-09  9:50       ` Johannes Schindelin
2009-03-09 19:34     ` René Scharfe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).