bug-diffutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-diffutils] [PATCH] diff: Encode file names with special charact


From: Jim Meyering
Subject: Re: [bug-diffutils] [PATCH] diff: Encode file names with special characters
Date: Mon, 10 Sep 2012 14:42:53 +0200

Andreas Gruenbacher wrote:

> On Mon, 2012-09-10 at 13:47 +0200, Jim Meyering wrote:
>> Andreas Gruenbacher wrote:
>> ...
>> > Note that the test case might not survive sending over email as it
>> > contains a special character.  Maybe the test case can be turned into
>> > plain text somehow and stay reasonably portable.
>> ...
>> > diff --git a/tests/filename-quoting b/tests/filename-quoting
>> > new file mode 100755
>> > index 0000000..d06ff60
>>
>> I guess the ^A (0x01) was what you meant, but I'd suggest also
>> replacing a couple of TABs in nearby code.  I'll be happy to
>> include this change [...] in your patch
>
> Great if the printf utility is portable enough to use here.

And since I noticed an xmalloc+sprintf combo while reviewing your
changes (your change merely modified an existing one), I wrote
this to replace that with a use of asprintf.  It is much more
maintainable that way:

>From 221383bcb1f9de839727b6ffd1b5a2b725314779 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Mon, 10 Sep 2012 12:18:59 +0200
Subject: [PATCH] maint: use xasprintf in place of xmalloc+sprintf

* bootstrap.conf (gnulib_modules): Add gnulib's xvasprintf module.
* src/util.c: Include "xvasprintf.h".
(begin_output): Use xasprintf in place of xmalloc+sprintf.
---
 bootstrap.conf | 1 +
 src/util.c     | 5 ++---
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 3954c4d..81b318e 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -81,6 +81,7 @@ xalloc
 xfreopen
 xreadlink
 xstrtoumax
+xvasprintf
 '

 # Additional xgettext options to use.  Use "\\\newline" to break lines.
diff --git a/src/util.c b/src/util.c
index 868b7e8..c02d156 100644
--- a/src/util.c
+++ b/src/util.c
@@ -23,6 +23,7 @@
 #include <error.h>
 #include <system-quote.h>
 #include <xalloc.h>
+#include "xvasprintf.h"

 char const pr_program[] = PR_PROGRAM;

@@ -262,14 +263,12 @@ begin_output (void)
   names[1] = c_escape (current_name1);

   /* Construct the header of this piece of diff.  */
-  name = xmalloc (strlen (names[0]) + strlen (names[1]) + strlen 
(switch_string) + 7);
-
   /* POSIX 1003.1-2001 specifies this format.  But there are some bugs in
      the standard: it says that we must print only the last component
      of the pathnames, and it requires two spaces after "diff" if
      there are no options.  These requirements are silly and do not
      match historical practice.  */
-  sprintf (name, "diff%s %s %s", switch_string, names[0], names[1]);
+  name = xasprintf ("diff%s %s %s", switch_string, names[0], names[1]);

   if (paginate)
     {
--
1.7.12.289.g0ce9864



reply via email to

[Prev in Thread] Current Thread [Next in Thread]