diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2006-01-05 07:08:23 +0000 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2006-01-05 07:08:23 +0000 |
commit | 91b8605708cf2d7c49679a46a80b4039167da2cd (patch) | |
tree | 6972aba10c298cc28c3511b03647ead2ddc42765 /src | |
parent | 174ab8396535806d77708990568f9817d11d5bb3 (diff) | |
download | diffutils-91b8605708cf2d7c49679a46a80b4039167da2cd.tar.gz |
* src/cmp.c: Don't include <setmode.h>.
(specify_ignore_initial): Reword to avoid gcc -W warnings.
(main): Use freopen instead of setmode, since freopen is in POSIX.
* src/context.c: Revert most 2004-09-01 changes. Then:
(TIMESPEC_NS): Remove. All uses replaced by
get_stat_mtime_ns.
Include stat-time.h, strftime.h.
(nstrtime): Remove decl.
* src/diff.c: Revert most 2004-09-01 changes. Then:
Don't include <posixver.h>, <quotesys.h>, <setmode.h>.
Include <sh-quote.h>, <stat-time.h>, <timespec.h>.
All uses of quotesys replaced by sh-quote.
(main, compare_files):
Use freopen instead of setmode, since freopen is in POSIX.
(main): Don't complain about "diff -NUM'.
(main, set_mtime_to_now):
Adjust to stat-time.h macros when accessing nanoseconds.
* src/diff3.c: Include sh-quote.h rather than quotesys. All uses
changed.
* src/dir.c (dir_read): excluded_filename renamed to
excluded_file_name.
* src/io.c: Don't include <setmode.h>.
(sip, read_files): Remove binary file stuff, leaving a FIXME behind.
A DOS expert needs to look at this.
* src/diff.c: Include sh-quote.h rather than quotesys.h.
All uses changed.
* src/system.h: Include verify.h.
(verify): Remove. All uses changed to verify.h version.
Include <intprops.h>.
(TYPE_SIGNED, TYPE_MINIMUM, TYPE_MAXIMUM): Remove. Now uses
intprops.h versions.
(O_BINARY): New defns, taken from coreutils.
* src/util.c: Include sh-quote.h rather than quotesys.h.
All uses changed.
Diffstat (limited to 'src')
-rw-r--r-- | src/cmp.c | 24 | ||||
-rw-r--r-- | src/context.c | 207 | ||||
-rw-r--r-- | src/diff.c | 516 | ||||
-rw-r--r-- | src/diff3.c | 16 | ||||
-rw-r--r-- | src/dir.c | 4 | ||||
-rw-r--r-- | src/io.c | 34 | ||||
-rw-r--r-- | src/sdiff.c | 14 | ||||
-rw-r--r-- | src/system.h | 47 | ||||
-rw-r--r-- | src/util.c | 8 |
9 files changed, 404 insertions, 466 deletions
@@ -1,7 +1,7 @@ /* cmp - compare two files byte by byte Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 2001, - 2002, 2004 Free Software Foundation, Inc. + 2002, 2004, 2006 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -32,7 +32,6 @@ #include <getopt.h> #include <hard-locale.h> #include <inttostr.h> -#include <setmode.h> #include <unlocked-io.h> #include <version-etc.h> #include <xalloc.h> @@ -126,15 +125,14 @@ static void specify_ignore_initial (int f, char **argptr, char delimiter) { uintmax_t val; - off_t o; char const *arg = *argptr; strtol_error e = xstrtoumax (arg, argptr, 0, &val, valid_suffixes); if (! (e == LONGINT_OK || (e == LONGINT_INVALID_SUFFIX_CHAR && **argptr == delimiter)) - || (o = val) < 0 || o != val || val == UINTMAX_MAX) + || TYPE_MAXIMUM (off_t) < val) try_help ("invalid --ignore-initial value `%s'", arg); - if (ignore_initial[f] < o) - ignore_initial[f] = o; + if (ignore_initial[f] < val) + ignore_initial[f] = val; } /* Specify the output format. */ @@ -284,9 +282,15 @@ main (int argc, char **argv) && file_name_cmp (file[0], file[1]) == 0) return EXIT_SUCCESS; - file_desc[f1] = (strcmp (file[f1], "-") == 0 - ? STDIN_FILENO - : open (file[f1], O_RDONLY, 0)); + if (strcmp (file[f1], "-") == 0) + { + file_desc[f1] = STDIN_FILENO; + if (O_BINARY && ! isatty (STDIN_FILENO)) + freopen (NULL, "rb", stdin); + } + else + file_desc[f1] = open (file[f1], O_RDONLY | O_BINARY, 0); + if (file_desc[f1] < 0 || fstat (file_desc[f1], stat_buf + f1) != 0) { if (file_desc[f1] < 0 && comparison_type == type_status) @@ -294,8 +298,6 @@ main (int argc, char **argv) else error (EXIT_TROUBLE, errno, "%s", file[f1]); } - - set_binary_mode (file_desc[f1], true); } /* If the files are links to the same inode and have the same file position, diff --git a/src/context.c b/src/context.c index 17164b3..7c127dd 100644 --- a/src/context.c +++ b/src/context.c @@ -1,7 +1,7 @@ /* Context-format output routines for GNU DIFF. Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 2001, - 2002, 2004 Free Software Foundation, Inc. + 2002, 2004, 2006 Free Software Foundation, Inc. This file is part of GNU DIFF. @@ -22,73 +22,70 @@ #include "diff.h" #include <inttostr.h> +#include <stat-time.h> +#include <strftime.h> -#ifdef ST_MTIM_NSEC -# define TIMESPEC_NS(timespec) ((timespec).ST_MTIM_NSEC) -#else -# define TIMESPEC_NS(timespec) 0 -#endif +static char const *find_function (char const * const *, lin); +static struct change *find_hunk (struct change *); +static void mark_ignorable (struct change *); +static void pr_context_hunk (struct change *); +static void pr_unidiff_hunk (struct change *); -size_t nstrftime (char *, size_t, char const *, struct tm const *, int, int); +/* Last place find_function started searching from. */ +static lin find_function_last_search; -static char const *find_function (char const * const *, lin, struct diffopt *); -static struct change *find_hunk (struct change *, struct diffopt const *opt); -static void mark_ignorable (struct change *, struct diffopt *); -static void pr_context_hunk (struct change *, struct diffopt *); -static void pr_unidiff_hunk (struct change *, struct diffopt *); +/* The value find_function returned when it started searching there. */ +static lin find_function_last_match; /* Print a label for a context diff, with a file name and date or a label. */ static void print_context_label (char const *mark, struct file_data *inf, - char const *label, - struct diffopt const *opt) + char const *label) { if (label) - fprintf (opt->out, "%s %s\n", mark, label); + fprintf (outfile, "%s %s\n", mark, label); else { char buf[MAX (INT_STRLEN_BOUND (int) + 32, INT_STRLEN_BOUND (time_t) + 11)]; struct tm const *tm = localtime (&inf->stat.st_mtime); - int nsec = TIMESPEC_NS (inf->stat.st_mtim); - if (! (tm && nstrftime (buf, sizeof buf, opt->time_format, tm, 0, nsec))) + int nsec = get_stat_mtime_ns (&inf->stat); + if (! (tm && nstrftime (buf, sizeof buf, time_format, tm, 0, nsec))) { long int sec = inf->stat.st_mtime; - verify (info_preserved, sizeof inf->stat.st_mtime <= sizeof sec); + verify (sizeof inf->stat.st_mtime <= sizeof sec); sprintf (buf, "%ld.%.9d", sec, nsec); } - fprintf (opt->out, "%s %s\t%s\n", mark, inf->name, buf); + fprintf (outfile, "%s %s\t%s\n", mark, inf->name, buf); } } /* Print a header for a context diff, with the file names and dates. */ void -print_context_header (struct file_data inf[], bool unidiff, - struct diffopt const *opt) +print_context_header (struct file_data inf[], bool unidiff) { if (unidiff) { - print_context_label ("---", &inf[0], opt->file_label[0], opt); - print_context_label ("+++", &inf[1], opt->file_label[1], opt); + print_context_label ("---", &inf[0], file_label[0]); + print_context_label ("+++", &inf[1], file_label[1]); } else { - print_context_label ("***", &inf[0], opt->file_label[0], opt); - print_context_label ("---", &inf[1], opt->file_label[1], opt); + print_context_label ("***", &inf[0], file_label[0]); + print_context_label ("---", &inf[1], file_label[1]); } } /* Print an edit script in context format. */ void -print_context_script (struct change *script, bool unidiff, - struct diffopt *opt) +print_context_script (struct change *script, bool unidiff) { - if (opt->ignore_blank_lines || opt->ignore_regexp) - mark_ignorable (script, opt); + if (ignore_blank_lines || ignore_regexp.fastmap) + mark_ignorable (script); else { struct change *e; @@ -96,13 +93,13 @@ print_context_script (struct change *script, bool unidiff, e->ignore = false; } - opt->find_function.last_search = - opt->file[0].prefix_lines; - opt->find_function.last_match = LIN_MAX; + find_function_last_search = - files[0].prefix_lines; + find_function_last_match = LIN_MAX; if (unidiff) - print_script (script, find_hunk, pr_unidiff_hunk, opt); + print_script (script, find_hunk, pr_unidiff_hunk); else - print_script (script, find_hunk, pr_context_hunk, opt); + print_script (script, find_hunk, pr_context_hunk); } /* Print a pair of line numbers with a comma, translated for file FILE. @@ -112,8 +109,7 @@ print_context_script (struct change *script, bool unidiff, We print the translated (real) line numbers. */ static void -print_context_number_range (FILE *out, struct file_data const *file, - lin a, lin b) +print_context_number_range (struct file_data const *file, lin a, lin b) { long int trans_a, trans_b; translate_range (file, a, b, &trans_a, &trans_b); @@ -128,9 +124,9 @@ print_context_number_range (FILE *out, struct file_data const *file, specification. */ if (trans_b <= trans_a) - fprintf (out, "%ld", trans_b); + fprintf (outfile, "%ld", trans_b); else - fprintf (out, "%ld,%ld", trans_a, trans_b); + fprintf (outfile, "%ld,%ld", trans_a, trans_b); } /* Print FUNCTION in a context header. */ @@ -147,13 +143,12 @@ print_context_function (FILE *out, char const *function) /* Print a portion of an edit script in context format. HUNK is the beginning of the portion to be printed. The end is marked by a `link' that has been nulled out. - OPT is the diff options. Prints out lines from both files, and precedes each line with the appropriate flag-character. */ static void -pr_context_hunk (struct change *hunk, struct diffopt *opt) +pr_context_hunk (struct change *hunk) { lin first0, last0, first1, last1, i; char const *prefix; @@ -162,32 +157,31 @@ pr_context_hunk (struct change *hunk, struct diffopt *opt) /* Determine range of line numbers involved in each file. */ - enum changes changes = analyze_hunk (hunk, &first0, &last0, &first1, &last1, - opt); + enum changes changes = analyze_hunk (hunk, &first0, &last0, &first1, &last1); if (! changes) return; /* Include a context's width before and after. */ - i = - opt->file[0].prefix_lines; - first0 = MAX (first0 - opt->context, i); - first1 = MAX (first1 - opt->context, i); - if (last0 < opt->file[0].valid_lines - opt->context) - last0 += opt->context; + i = - files[0].prefix_lines; + first0 = MAX (first0 - context, i); + first1 = MAX (first1 - context, i); + if (last0 < files[0].valid_lines - context) + last0 += context; else - last0 = opt->file[0].valid_lines - 1; - if (last1 < opt->file[1].valid_lines - opt->context) - last1 += opt->context; + last0 = files[0].valid_lines - 1; + if (last1 < files[1].valid_lines - context) + last1 += context; else - last1 = opt->file[1].valid_lines - 1; + last1 = files[1].valid_lines - 1; /* If desired, find the preceding function definition line in file 0. */ - function = 0; - if (opt->function_regexp) - function = find_function (opt->file[0].linbuf, first0, opt); + function = NULL; + if (function_regexp.fastmap) + function = find_function (files[0].linbuf, first0); - begin_output (opt); - out = opt->out; + begin_output (); + out = outfile; fprintf (out, "***************"); @@ -195,7 +189,7 @@ pr_context_hunk (struct change *hunk, struct diffopt *opt) print_context_function (out, function); fprintf (out, "\n*** "); - print_context_number_range (opt->out, &opt->file[0], first0, last0); + print_context_number_range (&files[0], first0, last0); fprintf (out, " ****\n"); if (changes & OLD) @@ -219,12 +213,12 @@ pr_context_hunk (struct change *hunk, struct diffopt *opt) Otherwise it is "deleted". */ prefix = (next->inserted > 0 ? "!" : "-"); - print_1_line (prefix, &opt->file[0].linbuf[i], opt); + print_1_line (prefix, &files[0].linbuf[i]); } } fprintf (out, "--- "); - print_context_number_range (opt->out, &opt->file[1], first1, last1); + print_context_number_range (&files[1], first1, last1); fprintf (out, " ----\n"); if (changes & NEW) @@ -248,7 +242,7 @@ pr_context_hunk (struct change *hunk, struct diffopt *opt) Otherwise it is "inserted". */ prefix = (next->deleted > 0 ? "!" : "+"); - print_1_line (prefix, &opt->file[1].linbuf[i], opt); + print_1_line (prefix, &files[1].linbuf[i]); } } } @@ -258,13 +252,10 @@ pr_context_hunk (struct change *hunk, struct diffopt *opt) If the numbers are equal, print just one number. Args A and B are internal line numbers. - We print the translated (real) line numbers. - - OPT is the diff options. */ + We print the translated (real) line numbers. */ static void -print_unidiff_number_range (FILE *out, struct file_data const *file, - lin a, lin b) +print_unidiff_number_range (struct file_data const *file, lin a, lin b) { long int trans_a, trans_b; translate_range (file, a, b, &trans_a, &trans_b); @@ -274,9 +265,9 @@ print_unidiff_number_range (FILE *out, struct file_data const *file, which is B. It would be more logical to print A, but 'patch' expects B in order to detect diffs against empty files. */ if (trans_b <= trans_a) - fprintf (out, trans_b < trans_a ? "%ld,0" : "%ld", trans_b); + fprintf (outfile, trans_b < trans_a ? "%ld,0" : "%ld", trans_b); else - fprintf (out, "%ld,%ld", trans_a, trans_b - trans_a + 1); + fprintf (outfile, "%ld,%ld", trans_a, trans_b - trans_a + 1); } /* Print a portion of an edit script in unidiff format. @@ -284,12 +275,10 @@ print_unidiff_number_range (FILE *out, struct file_data const *file, The end is marked by a `link' that has been nulled out. Prints out lines from both files, and precedes each - line with the appropriate flag-character. - - OPT is the diff options. */ + line with the appropriate flag-character. */ static void -pr_unidiff_hunk (struct change *hunk, struct diffopt *opt) +pr_unidiff_hunk (struct change *hunk) { lin first0, last0, first1, last1; lin i, j, k; @@ -299,35 +288,35 @@ pr_unidiff_hunk (struct change *hunk, struct diffopt *opt) /* Determine range of line numbers involved in each file. */ - if (! analyze_hunk (hunk, &first0, &last0, &first1, &last1, opt)) + if (! analyze_hunk (hunk, &first0, &last0, &first1, &last1)) return; /* Include a context's width before and after. */ - i = - opt->file[0].prefix_lines; - first0 = MAX (first0 - opt->context, i); - first1 = MAX (first1 - opt->context, i); - if (last0 < opt->file[0].valid_lines - opt->context) - last0 += opt->context; + i = - files[0].prefix_lines; + first0 = MAX (first0 - context, i); + first1 = MAX (first1 - context, i); + if (last0 < files[0].valid_lines - context) + last0 += context; else - last0 = opt->file[0].valid_lines - 1; - if (last1 < opt->file[1].valid_lines - opt->context) - last1 += opt->context; + last0 = files[0].valid_lines - 1; + if (last1 < files[1].valid_lines - context) + last1 += context; else - last1 = opt->file[1].valid_lines - 1; + last1 = files[1].valid_lines - 1; /* If desired, find the preceding function definition line in file 0. */ - function = 0; - if (opt->function_regexp) - function = find_function (opt->file[0].linbuf, first0, opt); + function = NULL; + if (function_regexp.fastmap) + function = find_function (files[0].linbuf, first0); - begin_output (opt); - out = opt->out; + begin_output (); + out = outfile; fprintf (out, "@@ -"); - print_unidiff_number_range (opt->out, &opt->file[0], first0, last0); + print_unidiff_number_range (&files[0], first0, last0); fprintf (out, " +"); - print_unidiff_number_range (opt->out, &opt->file[1], first1, last1); + print_unidiff_number_range (&files[1], first1, last1); fprintf (out, " @@"); if (function) @@ -346,8 +335,8 @@ pr_unidiff_hunk (struct change *hunk, struct diffopt *opt) if (!next || i < next->line0) { - putc (opt->initial_tab ? '\t' : ' ', out); - print_1_line (0, &opt->file[0].linbuf[i++], opt); + putc (initial_tab ? '\t' : ' ', out); + print_1_line (NULL, &files[0].linbuf[i++]); j++; } else @@ -358,9 +347,9 @@ pr_unidiff_hunk (struct change *hunk, struct diffopt *opt) while (k--) { putc ('-', out); - if (opt->initial_tab) + if (initial_tab) putc ('\t', out); - print_1_line (0, &opt->file[0].linbuf[i++], opt); + print_1_line (NULL, &files[0].linbuf[i++]); } /* Then output the inserted part. */ @@ -369,9 +358,9 @@ pr_unidiff_hunk (struct change *hunk, struct diffopt *opt) while (k--) { putc ('+', out); - if (opt->initial_tab) + if (initial_tab) putc ('\t', out); - print_1_line (0, &opt->file[1].linbuf[j++], opt); + print_1_line (NULL, &files[1].linbuf[j++]); } /* We're done with this hunk, so on to the next! */ @@ -386,7 +375,7 @@ pr_unidiff_hunk (struct change *hunk, struct diffopt *opt) to the `struct change' for the last change before those lines. */ static struct change * -find_hunk (struct change *start, struct diffopt const *opt) +find_hunk (struct change *start) { struct change *prev; lin top0, top1; @@ -396,8 +385,8 @@ find_hunk (struct change *start, struct diffopt const *opt) changes, but only CONTEXT if one is ignorable. Watch out for integer overflow, though. */ lin non_ignorable_threshold = - (LIN_MAX - 1) / 2 < opt->context ? LIN_MAX : 2 * opt->context + 1; - lin ignorable_threshold = opt->context; + (LIN_MAX - 1) / 2 < context ? LIN_MAX : 2 * context + 1; + lin ignorable_threshold = context; do { @@ -426,7 +415,7 @@ find_hunk (struct change *start, struct diffopt const *opt) are ignorable lines. */ static void -mark_ignorable (struct change *script, struct diffopt *opt) +mark_ignorable (struct change *script) { while (script) { @@ -438,7 +427,7 @@ mark_ignorable (struct change *script, struct diffopt *opt) /* Determine whether this change is ignorable. */ script->ignore = ! analyze_hunk (script, - &first0, &last0, &first1, &last1, opt); + &first0, &last0, &first1, &last1); /* Reconnect the chain as before. */ script->link = next; @@ -449,17 +438,15 @@ mark_ignorable (struct change *script, struct diffopt *opt) } /* Find the last function-header line in LINBUF prior to line number LINENUM. - This is a line containing a match for the regexp in `opt->function_regexp'. - OPT is the diff options. - Return the address of the text, or 0 if no function-header is found. */ + This is a line containing a match for the regexp in `function_regexp'. + Return the address of the text, or NULL if no function-header is found. */ static char const * -find_function (char const * const *linbuf, lin linenum, - struct diffopt *opt) +find_function (char const * const *linbuf, lin linenum) { lin i = linenum; - lin last = opt->find_function.last_search; - opt->find_function.last_search = i; + lin last = find_function_last_search; + find_function_last_search = i; while (last <= --i) { @@ -470,16 +457,16 @@ find_function (char const * const *linbuf, lin linenum, /* FIXME: re_search's size args should be size_t, not int. */ int len = MIN (linelen, INT_MAX); - if (0 <= re_search (opt->function_regexp, line, len, 0, len, 0)) + if (0 <= re_search (&function_regexp, line, len, 0, len, NULL)) { - opt->find_function.last_match = i; + find_function_last_match = i; return line; } } /* If we search back to where we started searching the previous time, find the line we found last time. */ - if (opt->find_function.last_match != LIN_MAX) - return linbuf[opt->find_function.last_match]; + if (find_function_last_match != LIN_MAX) + return linbuf[find_function_last_match]; - return 0; + return NULL; } @@ -1,7 +1,7 @@ /* diff - compare files line by line Copyright (C) 1988, 1989, 1992, 1993, 1994, 1996, 1998, 2001, 2002, - 2004 Free Software Foundation, Inc. + 2004, 2006 Free Software Foundation, Inc. This file is part of GNU DIFF. @@ -20,6 +20,7 @@ If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#define GDIFF_MAIN #include "diff.h" #include "paths.h" #include <c-stack.h> @@ -32,10 +33,10 @@ #include <fnmatch.h> #include <getopt.h> #include <hard-locale.h> -#include <posixver.h> #include <prepargs.h> -#include <quotesys.h> -#include <setmode.h> +#include <sh-quote.h> +#include <stat-time.h> +#include <timespec.h> #include <version-etc.h> #include <xalloc.h> @@ -52,23 +53,47 @@ struct regexp_list struct re_pattern_buffer *buf; }; -static int compare_files (struct comparison const *, char const *, char const *, struct diffopt *); -static void init_regexp_list (struct regexp_list *, struct re_pattern_buffer *); +static int compare_files (struct comparison const *, char const *, char const *); static void add_regexp (struct regexp_list *, char const *); -static struct re_pattern_buffer *summarize_regexp_list (struct regexp_list *); -static void specify_style (enum output_style, struct diffopt *); +static void summarize_regexp_list (struct regexp_list *); +static void specify_style (enum output_style); static void specify_value (char const **, char const *, char const *); static void try_help (char const *, char const *) __attribute__((noreturn)); -static void check_stdout (struct diffopt const *); +static void check_stdout (void); static void usage (void); -#if HAVE_SETMODE_DOS +/* If comparing directories, compare their common subdirectories + recursively. */ +static bool recursive; + +/* In context diffs, show previous lines that match these regexps. */ +static struct regexp_list function_regexp_list; + +/* Ignore changes affecting only lines that match these regexps. */ +static struct regexp_list ignore_regexp_list; + +#if O_BINARY /* Use binary I/O when reading and writing data (--binary). On POSIX hosts, this has no effect. */ static bool binary; #else enum { binary = true }; #endif + +/* When comparing directories, if a file appears only in one + directory, treat it as present but empty in the other (-N). + Then `patch' would create the file with appropriate contents. */ +static bool new_file; + +/* When comparing directories, if a file appears only in the second + directory of the two, treat it as present but empty in the other + (--unidirectional-new-file). + Then `patch' would create the file with appropriate contents. */ +static bool unidirectional_new_file; + +/* Report files compared that are the same (-s). + Normally nothing is output when that happens. */ +static bool report_identical_files; static char const shortopts[] = "0123456789abBcC:dD:eEfF:hHiI:lL:nNpPqrsS:tTuU:vwW:x:X:y"; @@ -181,6 +206,8 @@ static struct option const longopts[] = /* Program name. */ char *program_name; + + /* Return a string containing the command options with which diff was invoked. Spaces appear between what were separate ARGV-elements. There is a space at the beginning but none at the end. @@ -198,14 +225,14 @@ option_list (char **optionvec, int count) char *p; for (i = 0; i < count; i++) - size += 1 + quote_system_arg (NULL, optionvec[i]); + size += 1 + shell_quote_length (optionvec[i]); p = result = xmalloc (size); for (i = 0; i < count; i++) { *p++ = ' '; - p += quote_system_arg (p, optionvec[i]); + p = shell_quote_copy (p, optionvec[i]); } *p = '\0'; @@ -216,9 +243,9 @@ option_list (char **optionvec, int count) /* Return an option value suitable for add_exclude. */ static int -exclude_options (struct diffopt const *opt) +exclude_options (void) { - return EXCLUDE_WILDCARDS | (opt->ignore_file_name_case ? FNM_CASEFOLD : 0); + return EXCLUDE_WILDCARDS | (ignore_file_name_case ? FNM_CASEFOLD : 0); } int @@ -236,11 +263,6 @@ main (int argc, char **argv) char const *to_file = NULL; uintmax_t numval; char *numend; - struct regexp_list function_regexp_list; - struct regexp_list ignore_regexp_list; - struct re_pattern_buffer function_regexp; - struct re_pattern_buffer ignore_regexp; - struct diffopt opt; /* Do our initializations. */ exit_failure = 2; @@ -250,12 +272,10 @@ main (int argc, char **argv) bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); c_stack_action (0); + function_regexp_list.buf = &function_regexp; + ignore_regexp_list.buf = &ignore_regexp; re_set_syntax (RE_SYNTAX_GREP | RE_NO_POSIX_BACKTRACKING); - - memset (&opt, 0, sizeof opt); - init_regexp_list (&function_regexp_list, &function_regexp); - init_regexp_list (&ignore_regexp_list, &ignore_regexp); - opt.excluded = new_exclude (); + excluded = new_exclude (); /* Decode the options. */ @@ -284,16 +304,16 @@ main (int argc, char **argv) break; case 'a': - opt.text = true; + text = true; break; case 'b': - if (opt.ignore_white_space < IGNORE_SPACE_CHANGE) - opt.ignore_white_space = IGNORE_SPACE_CHANGE; + if (ignore_white_space < IGNORE_SPACE_CHANGE) + ignore_white_space = IGNORE_SPACE_CHANGE; break; case 'B': - opt.ignore_blank_lines = true; + ignore_blank_lines = true; break; case 'C': @@ -310,25 +330,25 @@ main (int argc, char **argv) else numval = 3; - specify_style (c == 'U' ? OUTPUT_UNIFIED : OUTPUT_CONTEXT, &opt); - if (opt.context < numval) - opt.context = numval; + specify_style (c == 'U' ? OUTPUT_UNIFIED : OUTPUT_CONTEXT); + if (context < numval) + context = numval; explicit_context = true; } break; case 'c': - specify_style (OUTPUT_CONTEXT, &opt); - if (opt.context < 3) - opt.context = 3; + specify_style (OUTPUT_CONTEXT); + if (context < 3) + context = 3; break; case 'd': - opt.minimal = true; + minimal = true; break; case 'D': - specify_style (OUTPUT_IFDEF, &opt); + specify_style (OUTPUT_IFDEF); { static char const C_ifdef_group_formats[] = "%%=%c#ifndef %s\n%%<#endif /* ! %s */\n%c#ifdef %s\n%%>#endif /* %s */\n%c#ifndef %s\n%%<#else /* %s */\n%%>#endif /* %s */\n"; @@ -340,27 +360,25 @@ main (int argc, char **argv) optarg, optarg, 0, optarg, optarg, 0, optarg, optarg, optarg); - for (i = 0; - i < sizeof opt.group_format / sizeof opt.group_format[0]; - i++) + for (i = 0; i < sizeof group_format / sizeof group_format[0]; i++) { - specify_value (&opt.group_format[i], b, "-D"); + specify_value (&group_format[i], b, "-D"); b += strlen (b) + 1; } } break; case 'e': - specify_style (OUTPUT_ED, &opt); + specify_style (OUTPUT_ED); break; case 'E': - if (opt.ignore_white_space < IGNORE_TAB_EXPANSION) - opt.ignore_white_space = IGNORE_TAB_EXPANSION; + if (ignore_white_space < IGNORE_TAB_EXPANSION) + ignore_white_space = IGNORE_TAB_EXPANSION; break; case 'f': - specify_style (OUTPUT_FORWARD_ED, &opt); + specify_style (OUTPUT_FORWARD_ED); break; case 'F': @@ -375,11 +393,11 @@ main (int argc, char **argv) break; case 'H': - opt.speed_large_files = true; + speed_large_files = true; break; case 'i': - opt.ignore_case = true; + ignore_case = true; break; case 'I': @@ -389,7 +407,7 @@ main (int argc, char **argv) case 'l': if (!pr_program[0]) try_help ("pagination not supported on this host", NULL); - opt.paginate = true; + paginate = true; #ifdef SIGCHLD /* Pagination requires forking and waiting, and System V fork+wait does not work if SIGCHLD is ignored. */ @@ -398,20 +416,20 @@ main (int argc, char **argv) break; case 'L': - if (!opt.file_label[0]) - opt.file_label[0] = optarg; - else if (!opt.file_label[1]) - opt.file_label[1] = optarg; + if (!file_label[0]) + file_label[0] = optarg; + else if (!file_label[1]) + file_label[1] = optarg; else - fatal ("too many file label options", &opt); + fatal ("too many file label options"); break; case 'n': - specify_style (OUTPUT_RCS, &opt); + specify_style (OUTPUT_RCS); break; case 'N': - opt.new_file = true; + new_file = true; break; case 'p': @@ -420,62 +438,62 @@ main (int argc, char **argv) break; case 'P': - opt.unidirectional_new_file = true; + unidirectional_new_file = true; break; case 'q': - opt.brief = true; + brief = true; break; case 'r': - opt.recursive = true; + recursive = true; break; case 's': - opt.report_identical_files = true; + report_identical_files = true; break; case 'S': - specify_value (&opt.starting_file, optarg, "-S"); + specify_value (&starting_file, optarg, "-S"); break; case 't': - opt.expand_tabs = true; + expand_tabs = true; break; case 'T': - opt.initial_tab = true; + initial_tab = true; break; case 'u': - specify_style (OUTPUT_UNIFIED, &opt); - if (opt.context < 3) - opt.context = 3; + specify_style (OUTPUT_UNIFIED); + if (context < 3) + context = 3; break; case 'v': version_etc (stdout, "diff", PACKAGE_NAME, PACKAGE_VERSION, "Paul Eggert", "Mike Haertel", "David Hayes", "Richard Stallman", "Len Tower", (char *) NULL); - check_stdout (&opt); + check_stdout (); return EXIT_SUCCESS; case 'w': - opt.ignore_white_space = IGNORE_ALL_SPACE; + ignore_white_space = IGNORE_ALL_SPACE; break; case 'x': - add_exclude (opt.excluded, optarg, exclude_options (&opt)); + add_exclude (excluded, optarg, exclude_options ()); break; case 'X': - if (add_exclude_file (add_exclude, opt.excluded, optarg, - exclude_options (&opt), '\n')) - pfatal_with_name (optarg, &opt); + if (add_exclude_file (add_exclude, excluded, optarg, + exclude_options (), '\n')) + pfatal_with_name (optarg); break; case 'y': - specify_style (OUTPUT_SDIFF, &opt); + specify_style (OUTPUT_SDIFF); break; case 'W': @@ -485,15 +503,16 @@ main (int argc, char **argv) if (width != numval) { if (width) - fatal ("conflicting width options", &opt); + fatal ("conflicting width options"); width = numval; } break; case BINARY_OPTION: -#if HAVE_SETMODE_DOS +#if O_BINARY binary = true; - set_binary_mode (STDOUT_FILENO, true); + if (! isatty (STDOUT_FILENO)) + freopen (NULL, "wb", stdout); #endif break; @@ -503,18 +522,18 @@ main (int argc, char **argv) case HELP_OPTION: usage (); - check_stdout (&opt); + check_stdout (); return EXIT_SUCCESS; case HORIZON_LINES_OPTION: numval = strtoumax (optarg, &numend, 10); if (*numend) try_help ("invalid horizon length `%s'", optarg); - opt.horizon_lines = MAX (opt.horizon_lines, MIN (numval, LIN_MAX)); + horizon_lines = MAX (horizon_lines, MIN (numval, LIN_MAX)); break; case IGNORE_FILE_NAME_CASE_OPTION: - opt.ignore_file_name_case = true; + ignore_file_name_case = true; break; case INHIBIT_HUNK_MERGE_OPTION: @@ -523,47 +542,45 @@ main (int argc, char **argv) break; case LEFT_COLUMN_OPTION: - opt.left_column = true; + left_column = true; break; case LINE_FORMAT_OPTION: - specify_style (OUTPUT_IFDEF, &opt); - for (i = 0; - i < sizeof opt.line_format / sizeof opt.line_format[0]; - i++) - specify_value (&opt.line_format[i], optarg, "--line-format"); + specify_style (OUTPUT_IFDEF); + for (i = 0; i < sizeof line_format / sizeof line_format[0]; i++) + specify_value (&line_format[i], optarg, "--line-format"); break; case NO_IGNORE_FILE_NAME_CASE_OPTION: - opt.ignore_file_name_case = false; + ignore_file_name_case = false; break; case NORMAL_OPTION: - specify_style (OUTPUT_NORMAL, &opt); + specify_style (OUTPUT_NORMAL); break; case SDIFF_MERGE_ASSIST_OPTION: - specify_style (OUTPUT_SDIFF, &opt); - opt.sdiff_merge_assist = true; + specify_style (OUTPUT_SDIFF); + sdiff_merge_assist = true; break; case STRIP_TRAILING_CR_OPTION: - opt.strip_trailing_cr = true; + strip_trailing_cr = true; break; case SUPPRESS_COMMON_LINES_OPTION: - opt.suppress_common_lines = true; + suppress_common_lines = true; break; case TABSIZE_OPTION: numval = strtoumax (optarg, &numend, 10); if (! (0 < numval && numval <= SIZE_MAX) || *numend) try_help ("invalid tabsize `%s'", optarg); - if (opt.tabsize != numval) + if (tabsize != numval) { - if (opt.tabsize) - fatal ("conflicting tabsize options", &opt); - opt.tabsize = numval; + if (tabsize) + fatal ("conflicting tabsize options"); + tabsize = numval; } break; @@ -574,18 +591,18 @@ main (int argc, char **argv) case UNCHANGED_LINE_FORMAT_OPTION: case OLD_LINE_FORMAT_OPTION: case NEW_LINE_FORMAT_OPTION: - specify_style (OUTPUT_IFDEF, &opt); + specify_style (OUTPUT_IFDEF); c -= UNCHANGED_LINE_FORMAT_OPTION; - specify_value (&opt.line_format[c], optarg, line_format_option[c]); + specify_value (&line_format[c], optarg, line_format_option[c]); break; case UNCHANGED_GROUP_FORMAT_OPTION: case OLD_GROUP_FORMAT_OPTION: case NEW_GROUP_FORMAT_OPTION: case CHANGED_GROUP_FORMAT_OPTION: - specify_style (OUTPUT_IFDEF, &opt); + specify_style (OUTPUT_IFDEF); c -= UNCHANGED_GROUP_FORMAT_OPTION; - specify_value (&opt.group_format[c], optarg, group_format_option[c]); + specify_value (&group_format[c], optarg, group_format_option[c]); break; default: @@ -594,65 +611,42 @@ main (int argc, char **argv) prev = c; } - if (opt.output_style == OUTPUT_UNSPECIFIED) + if (output_style == OUTPUT_UNSPECIFIED) { if (show_c_function) { - specify_style (OUTPUT_CONTEXT, &opt); + specify_style (OUTPUT_CONTEXT); if (ocontext < 0) - opt.context = 3; + context = 3; } else - specify_style (OUTPUT_NORMAL, &opt); + specify_style (OUTPUT_NORMAL); } - if (opt.output_style != OUTPUT_CONTEXT || hard_locale (LC_TIME)) + if (output_style != OUTPUT_CONTEXT || hard_locale (LC_TIME)) { -#ifdef ST_MTIM_NSEC - opt.time_format = "%Y-%m-%d %H:%M:%S.%N %z"; +#if (defined STAT_TIMESPEC || defined STAT_TIMESPEC_NS \ + || defined HAVE_STRUCT_STAT_ST_SPARE1) + time_format = "%Y-%m-%d %H:%M:%S.%N %z"; #else - opt.time_format = "%Y-%m-%d %H:%M:%S %z"; + time_format = "%Y-%m-%d %H:%M:%S %z"; #endif } else { /* See POSIX 1003.1-2001 for this format. */ - opt.time_format = "%a %b %e %T %Y"; + time_format = "%a %b %e %T %Y"; } - if (0 <= ocontext) - { - bool modern_usage = 200112 <= posix2_version (); - - if ((opt.output_style == OUTPUT_CONTEXT - || opt.output_style == OUTPUT_UNIFIED) - && (opt.context < ocontext - || (ocontext < opt.context && ! explicit_context))) - { - if (modern_usage) - { - error (0, 0, - _("`-%ld' option is obsolete; use `-%c %ld'"), - (long int) ocontext, - opt.output_style == OUTPUT_CONTEXT ? 'C' : 'U', - (long int) ocontext); - try_help (NULL, NULL); - } - opt.context = ocontext; - } - else - { - if (modern_usage) - { - error (0, 0, _("`-%ld' option is obsolete; omit it"), - (long int) ocontext); - try_help (NULL, NULL); - } - } - } + if (0 <= ocontext + && (output_style == OUTPUT_CONTEXT + || output_style == OUTPUT_UNIFIED) + && (context < ocontext + || (ocontext < context && ! explicit_context))) + context = ocontext; - if (! opt.tabsize) - opt.tabsize = 8; + if (! tabsize) + tabsize = 8; if (! width) width = 130; @@ -667,63 +661,61 @@ main (int argc, char **argv) a half line plus a gutter is an integral number of tabs, so that tabs in the right column line up. */ - intmax_t t = opt.expand_tabs ? 1 : opt.tabsize; + intmax_t t = expand_tabs ? 1 : tabsize; intmax_t w = width; intmax_t off = (w + t + GUTTER_WIDTH_MINIMUM) / (2 * t) * t; - opt.sdiff_half_width = MAX (0, MIN (off - GUTTER_WIDTH_MINIMUM, w - off)), - opt.sdiff_column2_offset = opt.sdiff_half_width ? off : w; + sdiff_half_width = MAX (0, MIN (off - GUTTER_WIDTH_MINIMUM, w - off)), + sdiff_column2_offset = sdiff_half_width ? off : w; } /* Make the horizon at least as large as the context, so that shift_boundaries has more freedom to shift the first and last hunks. */ - if (opt.horizon_lines < opt.context) - opt.horizon_lines = opt.context; + if (horizon_lines < context) + horizon_lines = context; - if (function_regexp_list.regexps) - opt.function_regexp = summarize_regexp_list (&function_regexp_list); - if (ignore_regexp_list.regexps) - opt.ignore_regexp = summarize_regexp_list (&ignore_regexp_list); + summarize_regexp_list (&function_regexp_list); + summarize_regexp_list (&ignore_regexp_list); - if (opt.output_style == OUTPUT_IFDEF) + if (output_style == OUTPUT_IFDEF) { - for (i = 0; i < sizeof opt.line_format / sizeof opt.line_format[0]; i++) - if (!opt.line_format[i]) - opt.line_format[i] = "%l\n"; - if (!opt.group_format[OLD]) - opt.group_format[OLD] - = opt.group_format[CHANGED] ? opt.group_format[CHANGED] : "%<"; - if (!opt.group_format[NEW]) - opt.group_format[NEW] - = opt.group_format[CHANGED] ? opt.group_format[CHANGED] : "%>"; - if (!opt.group_format[UNCHANGED]) - opt.group_format[UNCHANGED] = "%="; - if (!opt.group_format[CHANGED]) - opt.group_format[CHANGED] = concat (opt.group_format[OLD], - opt.group_format[NEW], ""); + for (i = 0; i < sizeof line_format / sizeof line_format[0]; i++) + if (!line_format[i]) + line_format[i] = "%l\n"; + if (!group_format[OLD]) + group_format[OLD] + = group_format[CHANGED] ? group_format[CHANGED] : "%<"; + if (!group_format[NEW]) + group_format[NEW] + = group_format[CHANGED] ? group_format[CHANGED] : "%>"; + if (!group_format[UNCHANGED]) + group_format[UNCHANGED] = "%="; + if (!group_format[CHANGED]) + group_format[CHANGED] = concat (group_format[OLD], + group_format[NEW], ""); } - opt.no_diff_means_no_output = - (opt.output_style == OUTPUT_IFDEF ? - (!*opt.group_format[UNCHANGED] - || (strcmp (opt.group_format[UNCHANGED], "%=") == 0 - && !*opt.line_format[UNCHANGED])) - : (opt.output_style != OUTPUT_SDIFF) | opt.suppress_common_lines); + no_diff_means_no_output = + (output_style == OUTPUT_IFDEF ? + (!*group_format[UNCHANGED] + || (strcmp (group_format[UNCHANGED], "%=") == 0 + && !*line_format[UNCHANGED])) + : (output_style != OUTPUT_SDIFF) | suppress_common_lines); - opt.files_can_be_treated_as_binary = - (opt.brief & binary - & ~ (opt.ignore_blank_lines | opt.ignore_case | opt.strip_trailing_cr - | (ignore_regexp_list.regexps || opt.ignore_white_space))); + files_can_be_treated_as_binary = + (brief & binary + & ~ (ignore_blank_lines | ignore_case | strip_trailing_cr + | (ignore_regexp_list.regexps || ignore_white_space))); - opt.switch_string = option_list (argv + 1, optind - 1); + switch_string = option_list (argv + 1, optind - 1); if (from_file) { if (to_file) - fatal ("--from-file and --to-file both specified", &opt); + fatal ("--from-file and --to-file both specified"); else for (; optind < argc; optind++) { - int status = compare_files (NULL, from_file, argv[optind], &opt); + int status = compare_files (NULL, from_file, argv[optind]); if (exit_status < status) exit_status = status; } @@ -733,7 +725,7 @@ main (int argc, char **argv) if (to_file) for (; optind < argc; optind++) { - int status = compare_files (NULL, argv[optind], to_file, &opt); + int status = compare_files (NULL, argv[optind], to_file); if (exit_status < status) exit_status = status; } @@ -747,31 +739,18 @@ main (int argc, char **argv) try_help ("extra operand `%s'", argv[optind + 2]); } - exit_status = compare_files (NULL, argv[optind], argv[optind + 1], - &opt); + exit_status = compare_files (NULL, argv[optind], argv[optind + 1]); } } /* Print any messages that were saved up for last. */ - print_message_queue (&opt); + print_message_queue (); - check_stdout (&opt); + check_stdout (); exit (exit_status); return exit_status; } -/* Initialize REGLIST for BUF. */ - -static void -init_regexp_list (struct regexp_list *reglist, struct re_pattern_buffer *buf) -{ - reglist->regexps = NULL; - reglist->len = 0; - reglist->size = 0; - reglist->multiple_regexps = false; - reglist->buf = buf; -} - /* Append to REGLIST the regexp PATTERN. */ static void @@ -811,23 +790,25 @@ add_regexp (struct regexp_list *reglist, char const *pattern) } /* Ensure that REGLIST represents the disjunction of its regexps. - This is done here, rather than earlier, to avoid O(N^2) behavior. - Return the compiled regular expression summarizing the disjunction. */ + This is done here, rather than earlier, to avoid O(N^2) behavior. */ -static struct re_pattern_buffer * +static void summarize_regexp_list (struct regexp_list *reglist) { - reglist->buf->fastmap = xmalloc (1 << CHAR_BIT); - if (reglist->multiple_regexps) + if (reglist->regexps) { - /* Compile the disjunction of the regexps. - (If just one regexp was specified, it is already compiled.) */ - char const *m = re_compile_pattern (reglist->regexps, reglist->len, - reglist->buf); - if (m) - error (EXIT_TROUBLE, 0, "%s: %s", reglist->regexps, m); + /* At least one regexp was specified. Allocate a fastmap for it. */ + reglist->buf->fastmap = xmalloc (1 << CHAR_BIT); + if (reglist->multiple_regexps) + { + /* Compile the disjunction of the regexps. + (If just one regexp was specified, it is already compiled.) */ + char const *m = re_compile_pattern (reglist->regexps, reglist->len, + reglist->buf); + if (m) + error (EXIT_TROUBLE, 0, "%s: %s", reglist->regexps, m); + } } - return reglist->buf; } static void @@ -841,12 +822,12 @@ try_help (char const *reason_msgid, char const *operand) } static void -check_stdout (struct diffopt const *opt) +check_stdout (void) { if (ferror (stdout)) - fatal ("write failed", opt); + fatal ("write failed"); else if (fclose (stdout) != 0) - pfatal_with_name (_("standard output"), opt); + pfatal_with_name (_("standard output")); } static char const * const option_help_msgid[] = { @@ -861,7 +842,7 @@ static char const * const option_help_msgid[] = { N_("-B --ignore-blank-lines Ignore changes whose lines are all blank."), N_("-I RE --ignore-matching-lines=RE Ignore changes whose lines all match RE."), N_("--strip-trailing-cr Strip trailing carriage return on input."), -#if HAVE_SETMODE_DOS +#if O_BINARY N_("--binary Read and write data in binary mode."), #endif N_("-a --text Treat all files as text."), @@ -977,13 +958,13 @@ specify_value (char const **var, char const *value, char const *option) /* Set the output style to STYLE, diagnosing conflicts. */ static void -specify_style (enum output_style style, struct diffopt *opt) +specify_style (enum output_style style) { - if (opt->output_style != style) + if (output_style != style) { - if (opt->output_style != OUTPUT_UNSPECIFIED) + if (output_style != OUTPUT_UNSPECIFIED) try_help ("conflicting output style options", NULL); - opt->output_style = style; + output_style = style; } } @@ -992,28 +973,18 @@ specify_style (enum output_style style, struct diffopt *opt) static void set_mtime_to_now (struct stat *st) { -#ifdef ST_MTIM_NSEC - -# if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME - if (clock_gettime (CLOCK_REALTIME, &st->st_mtim) == 0) - return; -# endif - -# if HAVE_GETTIMEOFDAY - { - struct timeval timeval; - if (gettimeofday (&timeval, 0) == 0) - { - st->st_mtime = timeval.tv_sec; - st->st_mtim.ST_MTIM_NSEC = timeval.tv_usec * 1000; - return; - } - } +#ifdef STAT_TIMESPEC + gettime (&STAT_TIMESPEC (st, st_mtim)); +#else + struct timespec t; + gettime (&t); + st->st_mtime = t.tv_sec; +# if defined STAT_TIMESPEC_NS + STAT_TIMESPEC_NS (st, st_mtim) = t.tv_nsec; +# elif defined HAVE_STRUCT_STAT_ST_SPARE1 + st->st_spare1 = t.tv_nsec / 1000; # endif - -#endif /* ST_MTIM_NSEC */ - - time (&st->st_mtime); +#endif } /* Compare two files (or dirs) with parent comparison PARENT @@ -1027,8 +998,7 @@ set_mtime_to_now (struct stat *st) static int compare_files (struct comparison const *parent, char const *name0, - char const *name1, - struct diffopt *opt) + char const *name1) { struct comparison cmp; #define DIR_P(f) (S_ISDIR (cmp.file[f].stat.st_mode) != 0) @@ -1043,14 +1013,14 @@ compare_files (struct comparison const *parent, If so, just print a message to that effect. */ if (! ((name0 && name1) - || (opt->unidirectional_new_file && name1) - || opt->new_file)) + || (unidirectional_new_file && name1) + || new_file)) { char const *name = name0 ? name0 : name1; char const *dir = parent->file[!name0].name; /* See POSIX 1003.1-2001 for this format. */ - message (opt, "Only in %s: %s\n", dir, name); + message ("Only in %s: %s\n", dir, name); /* Return EXIT_FAILURE so that diff_dirs will return EXIT_FAILURE ("some files differ"). */ @@ -1106,6 +1076,8 @@ compare_files (struct comparison const *parent, else if (strcmp (cmp.file[f].name, "-") == 0) { cmp.file[f].desc = STDIN_FILENO; + if (binary && ! isatty (STDIN_FILENO)) + freopen (NULL, "rb", stdin); if (fstat (STDIN_FILENO, &cmp.file[f].stat) != 0) cmp.file[f].desc = ERRNO_ENCODE (errno); else @@ -1136,7 +1108,7 @@ compare_files (struct comparison const *parent, top-level files that do not exist but their counterparts do exist. */ for (f = 0; f < 2; f++) - if ((opt->new_file || (f == 0 && opt->unidirectional_new_file)) + if ((new_file || (f == 0 && unidirectional_new_file)) && (cmp.file[f].desc == UNOPENED ? (S_ISREG (cmp.file[f].stat.st_mode) && ! (cmp.file[f].stat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) @@ -1177,7 +1149,7 @@ compare_files (struct comparison const *parent, = dir_file_pathname (dir, base_name (fnm)); if (strcmp (fnm, "-") == 0) - fatal ("cannot compare `-' to a directory", opt); + fatal ("cannot compare `-' to a directory"); if (stat (filename, &cmp.file[dir_arg].stat) != 0) { @@ -1201,28 +1173,28 @@ compare_files (struct comparison const *parent, && 0 < same_file (&cmp.file[0].stat, &cmp.file[1].stat) && same_file_attributes (&cmp.file[0].stat, &cmp.file[1].stat))) - && opt->no_diff_means_no_output) + && no_diff_means_no_output) { /* The two named files are actually the same physical file. We know they are identical without actually reading them. */ } else if (DIR_P (0) & DIR_P (1)) { - if (opt->output_style == OUTPUT_IFDEF) - fatal ("-D option not supported with directories", opt); + if (output_style == OUTPUT_IFDEF) + fatal ("-D option not supported with directories"); /* If both are directories, compare the files in them. */ - if (parent && !opt->recursive) + if (parent && !recursive) { /* But don't compare dir contents one level down unless -r was specified. See POSIX 1003.1-2001 for this format. */ - message (opt, "Common subdirectories: %s and %s\n", + message ("Common subdirectories: %s and %s\n", cmp.file[0].name, cmp.file[1].name); } else - status = diff_dirs (&cmp, compare_files, opt); + status = diff_dirs (&cmp, compare_files); } else if ((DIR_P (0) | DIR_P (1)) || (parent @@ -1234,18 +1206,18 @@ compare_files (struct comparison const *parent, /* We have a subdirectory that exists only in one directory. */ if ((DIR_P (0) | DIR_P (1)) - && opt->recursive - && (opt->new_file - || (opt->unidirectional_new_file + && recursive + && (new_file + || (unidirectional_new_file && cmp.file[0].desc == NONEXISTENT))) - status = diff_dirs (&cmp, compare_files, opt); + status = diff_dirs (&cmp, compare_files); else { char const *dir = parent->file[cmp.file[0].desc == NONEXISTENT].name; /* See POSIX 1003.1-2001 for this format. */ - message (opt, "Only in %s: %s\n", dir, name0); + message ("Only in %s: %s\n", dir, name0); status = EXIT_FAILURE; } @@ -1255,22 +1227,24 @@ compare_files (struct comparison const *parent, /* We have two files that are not to be compared. */ /* See POSIX 1003.1-2001 for this format. */ - message5 (opt, "File %s is a %s while file %s is a %s\n", - file_label (0, opt), file_type (&cmp.file[0].stat), - file_label (1, opt), file_type (&cmp.file[1].stat)); + message5 ("File %s is a %s while file %s is a %s\n", + file_label[0] ? file_label[0] : cmp.file[0].name, + file_type (&cmp.file[0].stat), + file_label[1] ? file_label[1] : cmp.file[1].name, + file_type (&cmp.file[1].stat)); /* This is a difference. */ status = EXIT_FAILURE; } } - else if (opt->files_can_be_treated_as_binary + else if (files_can_be_treated_as_binary && S_ISREG (cmp.file[0].stat.st_mode) && S_ISREG (cmp.file[1].stat.st_mode) && cmp.file[0].stat.st_size != cmp.file[1].stat.st_size) { - message (opt, "Files %s and %s differ\n", - file_label (0, opt), - file_label (1, opt)); + message ("Files %s and %s differ\n", + file_label[0] ? file_label[0] : cmp.file[0].name, + file_label[1] ? file_label[1] : cmp.file[1].name); status = EXIT_FAILURE; } else @@ -1279,8 +1253,10 @@ compare_files (struct comparison const *parent, /* Open the files and record their descriptors. */ + int oflags = O_RDONLY | (binary ? O_BINARY : 0); + if (cmp.file[0].desc == UNOPENED) - if ((cmp.file[0].desc = open (cmp.file[0].name, O_RDONLY, 0)) < 0) + if ((cmp.file[0].desc = open (cmp.file[0].name, oflags, 0)) < 0) { perror_with_name (cmp.file[0].name); status = EXIT_TROUBLE; @@ -1289,25 +1265,17 @@ compare_files (struct comparison const *parent, { if (same_files) cmp.file[1].desc = cmp.file[0].desc; - else if ((cmp.file[1].desc = open (cmp.file[1].name, O_RDONLY, 0)) - < 0) + else if ((cmp.file[1].desc = open (cmp.file[1].name, oflags, 0)) < 0) { perror_with_name (cmp.file[1].name); status = EXIT_TROUBLE; } } -#if HAVE_SETMODE_DOS - if (binary) - for (f = 0; f < 2; f++) - if (0 <= cmp.file[f].desc) - set_binary_mode (cmp.file[f].desc, true); -#endif - /* Compare the files, if no error was found. */ if (status == EXIT_SUCCESS) - status = diff_2_files (&cmp, opt); + status = diff_2_files (&cmp); /* Close the file descriptors. */ @@ -1329,17 +1297,17 @@ compare_files (struct comparison const *parent, if (status == EXIT_SUCCESS) { - if (opt->report_identical_files && !DIR_P (0)) - message (opt, "Files %s and %s are identical\n", - file_label (0, opt), - file_label (1, opt)); + if (report_identical_files && !DIR_P (0)) + message ("Files %s and %s are identical\n", + file_label[0] ? file_label[0] : cmp.file[0].name, + file_label[1] ? file_label[1] : cmp.file[1].name); } else { /* Flush stdout so that the user sees differences immediately. This can hurt performance, unfortunately. */ if (fflush (stdout) != 0) - pfatal_with_name (_("standard output"), opt); + pfatal_with_name (_("standard output")); } if (free0) diff --git a/src/diff3.c b/src/diff3.c index 261eeab..531c949 100644 --- a/src/diff3.c +++ b/src/diff3.c @@ -1,7 +1,7 @@ /* diff3 - compare three files line by line Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1998, 2001, - 2002, 2004 Free Software Foundation, Inc. + 2002, 2004, 2006 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,7 +31,7 @@ #include <file-type.h> #include <getopt.h> #include <inttostr.h> -#include <quotesys.h> +#include <sh-quote.h> #include <version-etc.h> #include <xalloc.h> @@ -1192,14 +1192,14 @@ read_diff (char const *filea, FILE *fpipe; char const args[] = " --horizon-lines=100 -- "; - char *command = xmalloc (quote_system_arg (0, diff_program) + char *command = xmalloc (shell_quote_length (diff_program) + sizeof "-a" + sizeof "--strip-trailing-cr" + sizeof args - 1 - + quote_system_arg (0, filea) + 1 - + quote_system_arg (0, fileb) + 1); + + shell_quote_length (filea) + 1 + + shell_quote_length (fileb) + 1); char *p = command; - p += quote_system_arg (p, diff_program); + p = shell_quote_copy (p, diff_program); if (text) { strcpy (p, " -a"); @@ -1212,9 +1212,9 @@ read_diff (char const *filea, } strcpy (p, args); p += sizeof args - 1; - p += quote_system_arg (p, filea); + p = shell_quote_copy (p, filea); *p++ = ' '; - p += quote_system_arg (p, fileb); + p = shell_quote_copy (p, fileb); *p = 0; errno = 0; fpipe = popen (command, "r"); @@ -1,7 +1,7 @@ /* Read, sort and compare two directories. Used for GNU DIFF. Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002, - 2004 Free Software Foundation, Inc. + 2004, 2006 Free Software Foundation, Inc. This file is part of GNU DIFF. @@ -99,7 +99,7 @@ dir_read (struct file_data const *dir, struct dirdata *dirdata) && (d_name[1] == 0 || (d_name[1] == '.' && d_name[2] == 0))) continue; - if (excluded_filename (excluded, d_name)) + if (excluded_file_name (excluded, d_name)) continue; while (data_alloc < data_used + d_size) @@ -1,7 +1,7 @@ /* File I/O for GNU DIFF. Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002, - 2004 Free Software Foundation, Inc. + 2004, 2006 Free Software Foundation, Inc. This file is part of GNU DIFF. @@ -23,7 +23,6 @@ #include "diff.h" #include <cmpbuf.h> #include <file-type.h> -#include <setmode.h> #include <xalloc.h> /* Rotate an unsigned value to the left. */ @@ -34,7 +33,7 @@ /* The type of a hash value. */ typedef size_t hash_value; -verify (hash_value_is_unsigned, ! TYPE_SIGNED (hash_value)); +verify (! TYPE_SIGNED (hash_value)); /* Lines are put into equivalence classes of lines that match in lines_differ. Each equivalence class is represented by one of these structures, @@ -114,26 +113,11 @@ sip (struct file_data *current, bool skip_test) { /* Check first part of file to see if it's a binary file. */ - bool was_binary = set_binary_mode (current->desc, true); - off_t buffered; - file_block_read (current, current->bufsize); - buffered = current->buffered; + /* FIXME: if O_BINARY, this should revert to text mode + if the file is not binary. */ - if (! was_binary) - { - /* Revert to text mode and seek back to the beginning to - reread the file. Use relative seek, since file - descriptors like stdin might not start at offset - zero. */ - - if (lseek (current->desc, - buffered, SEEK_CUR) == -1) - pfatal_with_name (current->name); - set_binary_mode (current->desc, false); - current->buffered = 0; - current->eof = false; - } - - return binary_file_p (current->buffer, buffered); + file_block_read (current, current->bufsize); + return binary_file_p (current->buffer, current->buffered); } } @@ -796,8 +780,7 @@ static unsigned char const prime_offset[] = /* Verify that this host's size_t is not too wide for the above table. */ -verify (enough_prime_offsets, - sizeof (size_t) * CHAR_BIT <= sizeof prime_offset); +verify (sizeof (size_t) * CHAR_BIT <= sizeof prime_offset); /* Given a vector of two file_data objects, read the file associated with each one, and build the table of equivalence classes. @@ -821,8 +804,7 @@ read_files (struct file_data filevec[], bool pretend_binary) } if (appears_binary) { - set_binary_mode (filevec[0].desc, true); - set_binary_mode (filevec[1].desc, true); + /* FIXME: If O_BINARY, this should set both files to binary mode. */ return true; } diff --git a/src/sdiff.c b/src/sdiff.c index 2b0a2ae..f0d39a2 100644 --- a/src/sdiff.c +++ b/src/sdiff.c @@ -1,7 +1,7 @@ /* sdiff - side-by-side merge of file differences - Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 2001, 2002, 2004 - Free Software Foundation, Inc. + Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 2001, 2002, 2004, + 2006 Free Software Foundation, Inc. This file is part of GNU DIFF. @@ -33,7 +33,7 @@ #include <exitfail.h> #include <file-type.h> #include <getopt.h> -#include <quotesys.h> +#include <sh-quote.h> #include <version-etc.h> #include <xalloc.h> @@ -614,11 +614,11 @@ main (int argc, char *argv[]) int i; for (i = 0; diffargv[i]; i++) - cmdsize += quote_system_arg (0, diffargv[i]) + 1; + cmdsize += shell_quote_length (diffargv[i]) + 1; command = p = xmalloc (cmdsize); for (i = 0; diffargv[i]; i++) { - p += quote_system_arg (p, diffargv[i]); + p = shell_quote_copy (diffargv[i]); *p++ = ' '; } p[-1] = 0; @@ -1056,9 +1056,9 @@ edit (struct line_filter *left, char const *lname, lin lline, lin llen, { #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK) char *command = - xmalloc (quote_system_arg (0, editor_program) + xmalloc (shell_quote_length (editor_program) + 1 + strlen (tmpname) + 1); - sprintf (command + quote_system_arg (command, editor_program), + sprintf (shell_quote_copy (command, editor_program), " %s", tmpname); wstatus = system (command); if (wstatus == -1) diff --git a/src/system.h b/src/system.h index e480cc5..bf0da79 100644 --- a/src/system.h +++ b/src/system.h @@ -1,7 +1,7 @@ /* System dependent declarations. Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002, - 2004 Free Software Foundation, Inc. + 2004, 2006 Free Software Foundation, Inc. This file is part of GNU DIFF. @@ -37,26 +37,7 @@ # define volatile #endif -/* Verify a requirement at compile-time (unlike assert, which is runtime). */ -#define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; } - - -/* Determine whether an integer type is signed, and its bounds. - This code assumes two's (or one's!) complement with no holes. */ - -/* The extra casts work around common compiler bugs, - e.g. Cray C 5.0.3.0 when t == time_t. */ -#ifndef TYPE_SIGNED -# define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) -#endif -#ifndef TYPE_MINIMUM -# define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \ - ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \ - : (t) 0)) -#endif -#ifndef TYPE_MAXIMUM -# define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t))) -#endif +#include <verify.h> #include <sys/types.h> #include <sys/stat.h> @@ -119,6 +100,22 @@ # define S_IWUSR 0200 #endif +/* For systems that distinguish between text and binary I/O. + O_BINARY is usually declared in fcntl.h */ +#if !defined O_BINARY && defined _O_BINARY + /* For MSC-compatible compilers. */ +# define O_BINARY _O_BINARY +#endif + +#ifdef __BEOS__ + /* BeOS 5 has O_BINARY, but it has no effect. */ +# undef O_BINARY +#endif + +#ifndef O_BINARY +# define O_BINARY 0 +#endif + #if HAVE_SYS_WAIT_H # include <sys/wait.h> #endif @@ -240,6 +237,8 @@ int strcasecmp (char const *, char const *); # define vfork fork #endif +#include <intprops.h> + /* Type used for fast comparison of several bytes at a time. */ #ifndef word @@ -251,9 +250,9 @@ int strcasecmp (char const *, char const *); typedef ptrdiff_t lin; #define LIN_MAX PTRDIFF_MAX -verify (lin_is_signed, TYPE_SIGNED (lin)); -verify (lin_is_wide_enough, sizeof (ptrdiff_t) <= sizeof (lin)); -verify (lin_is_printable_as_long_int, sizeof (lin) <= sizeof (long int)); +verify (TYPE_SIGNED (lin)); +verify (sizeof (ptrdiff_t) <= sizeof (lin)); +verify (sizeof (lin) <= sizeof (long int)); /* This section contains POSIX-compliant defaults for macros that are meant to be overridden by hand in config.h as needed. */ @@ -1,7 +1,7 @@ /* Support routines for GNU DIFF. Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002, - 2004 Free Software Foundation, Inc. + 2004, 2006 Free Software Foundation, Inc. This file is part of GNU DIFF. @@ -23,7 +23,7 @@ #include "diff.h" #include <dirname.h> #include <error.h> -#include <quotesys.h> +#include <sh-quote.h> #include <xalloc.h> char const pr_program[] = PR_PROGRAM; @@ -226,11 +226,11 @@ begin_output (void) } #else char *command = xmalloc (sizeof pr_program - 1 + 7 - + quote_system_arg ((char *) 0, name) + 1); + + shell_quote_length (name) + 1); char *p; sprintf (command, "%s -f -h ", pr_program); p = command + sizeof pr_program - 1 + 7; - p += quote_system_arg (p, name); + p = shell_quote_copy (name); *p = 0; errno = 0; outfile = popen (command, "w"); |