git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Brian Gernhardt <benji@silverinsanity.com>
To: Git List <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH] grep: make show_line more portable
Date: Sun,  8 Mar 2009 21:15:26 -0400	[thread overview]
Message-ID: <1236561326-1231-1-git-send-email-benji@silverinsanity.com> (raw)

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

             reply	other threads:[~2009-03-09  1:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-09  1:15 Brian Gernhardt [this message]
2009-03-09  1:35 ` [PATCH] grep: make show_line more portable 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1236561326-1231-1-git-send-email-benji@silverinsanity.com \
    --to=benji@silverinsanity.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).