diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2012-05-14 23:07:13 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2012-05-14 23:08:59 -0700 |
commit | 3549f733b9cd2c7fd5dd1e4a273151e2da01a00b (patch) | |
tree | dae7ccaa474f677c43c47b7f92e9b5ff37deef50 | |
parent | de512b8345de9d501b21a5c1bc446e28d50ebf8c (diff) | |
download | diffutils-3549f733b9cd2c7fd5dd1e4a273151e2da01a00b.tar.gz |
main: port subcommands to mingw
Problem reported by Eli Zaretskii in
<http://lists.gnu.org/archive/html/bug-gnu-utils/2012-05/msg00013.html>.
Approach suggested by Bruno Haible as option (4) in
<http://lists.gnu.org/archive/html/bug-gnu-utils/2012-05/msg00036.html>.
* bootstrap.conf (gnulib_modules): Add system-quote.
* src/diff3.c, src/sdiff.c, src/util.c:
Include <system-quote.h>, not <sh-quote.h>.
* src/diff3.c (read_diff):
* src/sdiff.c (main, edit):
* src/util.c (begin_output):
Use system_quote_argv, for portability to Mingw.
* src/sdiff.c (NUM_SIGS, handler_index_of_SIGINT): Now enum
values, not macros; this is cleaner and avoids a GCC warning if
!HAVE_WORKING_VFORK.
* src/util.c (begin_output) [! HAVE_WORKING_FORK]: Do not use -f,
for consistency with the HAVE_WORKING_FORK code.
-rw-r--r-- | bootstrap.conf | 1 | ||||
-rw-r--r-- | src/diff3.c | 39 | ||||
-rw-r--r-- | src/sdiff.c | 41 | ||||
-rw-r--r-- | src/util.c | 19 |
4 files changed, 33 insertions, 67 deletions
diff --git a/bootstrap.conf b/bootstrap.conf index 55f0837..3954c4d 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -69,6 +69,7 @@ strftime strptime strtoumax sys_wait +system-quote unistd unlocked-io update-copyright diff --git a/src/diff3.c b/src/diff3.c index 3b01071..e234401 100644 --- a/src/diff3.c +++ b/src/diff3.c @@ -29,7 +29,7 @@ #include <file-type.h> #include <getopt.h> #include <progname.h> -#include <sh-quote.h> +#include <system-quote.h> #include <version-etc.h> #include <xalloc.h> #include <xfreopen.h> @@ -1161,13 +1161,15 @@ read_diff (char const *filea, int fd, wstatus, status; int werrno = 0; struct stat pipestat; - -#if HAVE_WORKING_FORK - char const *argv[9]; char const **ap; +#if HAVE_WORKING_FORK int fds[2]; pid_t pid; +#else + FILE *fpipe; + char *command; +#endif ap = argv; *ap++ = diff_program; @@ -1181,6 +1183,8 @@ read_diff (char const *filea, *ap++ = fileb; *ap = 0; +#if HAVE_WORKING_FORK + if (pipe (fds) != 0) perror_with_exit ("pipe"); @@ -1210,32 +1214,7 @@ read_diff (char const *filea, #else - FILE *fpipe; - char const args[] = " --horizon-lines=100 -- "; - char *command = xmalloc (shell_quote_length (diff_program) - + sizeof "-a" - + sizeof "--strip-trailing-cr" - + sizeof args - 1 - + shell_quote_length (filea) + 1 - + shell_quote_length (fileb) + 1); - char *p = command; - p = shell_quote_copy (p, diff_program); - if (text) - { - strcpy (p, " -a"); - p += 3; - } - if (strip_trailing_cr) - { - strcpy (p, " --strip-trailing-cr"); - p += 20; - } - strcpy (p, args); - p += sizeof args - 1; - p = shell_quote_copy (p, filea); - *p++ = ' '; - p = shell_quote_copy (p, fileb); - *p = 0; + command = system_quote_argv (SCI_SYSTEM, (char **) argv); errno = 0; fpipe = popen (command, "r"); if (!fpipe) diff --git a/src/sdiff.c b/src/sdiff.c index 63b2861..e82ec84 100644 --- a/src/sdiff.c +++ b/src/sdiff.c @@ -31,7 +31,7 @@ #include <file-type.h> #include <getopt.h> #include <progname.h> -#include <sh-quote.h> +#include <system-quote.h> #include <version-etc.h> #include <xalloc.h> @@ -66,7 +66,6 @@ static void perror_fatal (char const *) __attribute__((noreturn)); static void trapsigs (void); static void untrapsig (int); -#define NUM_SIGS (sizeof sigs / sizeof *sigs) static int const sigs[] = { #ifdef SIGHUP SIGHUP, @@ -87,8 +86,12 @@ static int const sigs[] = { SIGPIPE, #endif SIGINT -#define handler_index_of_SIGINT (NUM_SIGS - 1) }; +enum + { + NUM_SIGS = sizeof sigs / sizeof *sigs, + handler_index_of_SIGINT = NUM_SIGS - 1 + }; #if HAVE_SIGACTION /* Prefer 'sigaction' if available, since 'signal' can lose signals. */ @@ -607,19 +610,7 @@ main (int argc, char *argv[]) #if ! HAVE_WORKING_FORK { - size_t cmdsize = 1; - char *p, *command; - int i; - - for (i = 0; diffargv[i]; i++) - cmdsize += shell_quote_length (diffargv[i]) + 1; - command = p = xmalloc (cmdsize); - for (i = 0; diffargv[i]; i++) - { - p = shell_quote_copy (p, diffargv[i]); - *p++ = ' '; - } - p[-1] = 0; + char *command = system_quote_argv (SCI_SYSTEM, (char **) diffargv); errno = 0; diffout = popen (command, "r"); if (! diffout) @@ -1020,16 +1011,17 @@ edit (struct line_filter *left, char const *lname, lin lline, lin llen, { int wstatus; int werrno = 0; + char const *argv[3]; + ignore_SIGINT = true; checksigs (); + argv[0] = editor_program; + argv[1] = tmpname; + argv[2] = 0; { #if ! HAVE_WORKING_FORK - char *command = - xmalloc (shell_quote_length (editor_program) - + 1 + strlen (tmpname) + 1); - sprintf (shell_quote_copy (command, editor_program), - " %s", tmpname); + char *command = system_quote_argv (SCI_SYSTEM, (char **) argv); wstatus = system (command); if (wstatus == -1) werrno = errno; @@ -1040,13 +1032,6 @@ edit (struct line_filter *left, char const *lname, lin lline, lin llen, pid = fork (); if (pid == 0) { - char const *argv[3]; - int i = 0; - - argv[i++] = editor_program; - argv[i++] = tmpname; - argv[i] = 0; - execvp (editor_program, (char **) argv); _exit (errno == ENOENT ? 127 : 126); } @@ -21,7 +21,7 @@ #include "diff.h" #include <dirname.h> #include <error.h> -#include <sh-quote.h> +#include <system-quote.h> #include <xalloc.h> char const pr_program[] = PR_PROGRAM; @@ -187,9 +187,16 @@ begin_output (void) if (paginate) { + char const *argv[4]; + if (fflush (stdout) != 0) pfatal_with_name (_("write failed")); + argv[0] = pr_program; + argv[1] = "-h"; + argv[2] = name; + argv[3] = 0; + /* Make OUTFILE a pipe to a subsidiary 'pr'. */ { #if HAVE_WORKING_FORK @@ -212,7 +219,7 @@ begin_output (void) close (pipes[0]); } - execl (pr_program, pr_program, "-h", name, (char *) 0); + execv (pr_program, (char **) argv); _exit (errno == ENOENT ? 127 : 126); } else @@ -223,13 +230,7 @@ begin_output (void) pfatal_with_name ("fdopen"); } #else - char *command = xmalloc (sizeof pr_program - 1 + 7 - + shell_quote_length (name) + 1); - char *p; - sprintf (command, "%s -f -h ", pr_program); - p = command + sizeof pr_program - 1 + 7; - p = shell_quote_copy (p, name); - *p = 0; + char *command = system_quote_argv (SCI_SYSTEM, (char **) argv); errno = 0; outfile = popen (command, "w"); if (!outfile) |