git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Refactoring tracing code in "git.c" and "exec_cmd.c".

From
Christian Couder <chriscool@tuxfamily.org>
Date
Aug 27, 2006, 19:38 UTC
Message-ID
<200608272138.54834.chriscool@tuxfamily.org>
In-Reply-To
<7vmz9qybs5.fsf@assigned-by-dhcp.cox.net>
Le dimanche 27 août 2006 07:42, Junio C Hamano a écrit :
Show 18 quoted lines
> > +char *sq_quote_argv(const char** argv, int count)
> > +{
> > +	char *buf, *to;
> > +	int i;
> > +	size_t len;
> > +
> > +	/* Count argv if needed. */
> > +	if (count < 0) {
> > +		char **p = (char **)argv;
> > +		count = 0;
> > +		while (*p++) count++;
> > +	}
>
> Wouldn't this be easier to read?
>
> 	if (count < 0)
>         	for (count = 0; argv[count]; count++)
>                 	; /* just counting */
Yes, it looks better.
Show 14 quoted lines
> > +	/* Get destination buffer length. */
> > +	len = count ? count : 1;
>
> This confused me quite a bit.  Wouldn't it be simpler to special
> case the count==0 case and return xcalloc(1,1) here (this would
> allow you to lose "if (!count)" later as well)?
>
> > +	/* Copy into destination buffer. */
> > +	for (i = 0; i < count; ++i) {
> > +		if (i) *to++ = ' ';
>
> (style)
> 	if (i)
>         	*to++ = ' ';
Ok, I will take care of this.
Show 32 quoted lines
> > +		to += sq_quote_buf(to, len, argv[i]);
> > +	}
> > +
> > +	if (!count)
> > +		*buf = 0;
> > +
> > +	return buf;
> > +}
> >
> > +/* Return a newly allocated copy of "format" where the
> > + * first occurence of "old" has been replaced by "new". */
> > +static char *str_subst(const char *format, const char *old, const char
> > *new) +{
>
> I do not think there is anything wrong with this function
> per-se, but...
>
> > +void sq_quote_argv_printf(FILE* out, const char **argv, int count,
> > +			   const char *format, ...)
> > +{
> > +	/* Replace the string "ARGV" in format with the quoted arg values. */
> > +	char *argv_str = sq_quote_argv(argv, count);
> > +	char *new_format = str_subst(format, "ARGV", argv_str);
> > +
> > +	/* Print into "out" using the new format. */
> > +	va_list rest;
> > +	va_start(rest, format);
> > +	vfprintf(out, new_format, rest);
> > +	va_end(rest);
>
> this feels wrong.  What happens when the original argv had
> a per-cent in it?
You are right, I will rework this.

Thanks, Christian.

Previous: Junio C Hamano
Message 3 of 3 in “Refactoring tracing code in "git.c" and "exec_cmd.c".”
  1. Refactoring tracing code in "git.c" and "exec_cmd.c".Christian Couder, Aug 24, 2006
  2. Junio C HamanoAug 27, 2006
  3. Christian CouderAug 27, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.