📄 diff.c
字号:
case 132:
case 133:
case 134:
specify_style (OUTPUT_IFDEF);
if (specify_format (&line_format[c - 132], optarg) != 0)
error ("conflicting line format", 0, 0);
break;
case 135:
specify_style (OUTPUT_IFDEF);
{
int i, err = 0;
for (i = 0; i < sizeof (line_format) / sizeof (*line_format); i++)
err |= specify_format (&line_format[i], optarg);
if (err)
error ("conflicting line format", 0, 0);
}
break;
case 136:
case 137:
case 138:
case 139:
specify_style (OUTPUT_IFDEF);
if (specify_format (&group_format[c - 136], optarg) != 0)
error ("conflicting group format", 0, 0);
break;
case 140:
if (ck_atoi (optarg, &horizon_lines) || horizon_lines < 0)
fatal ("horizon must be a nonnegative integer");
break;
default:
usage ();
}
prev = c;
}
if (optind != argc - 2)
usage ();
{
//
//* We maximize first the half line width, and then the gutter width,
//* according to the following constraints:
//* 1. Two half lines plus a gutter must fit in a line.
//* 2. If the half line width is nonzero:
//* a. The gutter width is at least GUTTER_WIDTH_MINIMUM.
//* b. If tabs are not expanded to spaces,
//* a half line plus a gutter is an integral number of tabs,
//* so that tabs in the right column line up.
int t = tab_expand_flag ? 1 : TAB_WIDTH;
int off = (width + t + GUTTER_WIDTH_MINIMUM) / (2*t) * t;
sdiff_half_width = max (0, min (off - GUTTER_WIDTH_MINIMUM, width - off)),
sdiff_column2_offset = sdiff_half_width ? off : width;
}
if (output_style != OUTPUT_CONTEXT && output_style != OUTPUT_UNIFIED)
context = 0;
else if (context == -1)
// Default amount of context for -c.
context = 3;
if (output_style == OUTPUT_IFDEF)
{
int i;
for (i = 0; i < sizeof (line_format) / sizeof (*line_format); i++)
if (!line_format[i])
line_format[i] = "%l\n";
if (!group_format[OLD])
group_format[OLD]
= group_format[UNCHANGED] ? group_format[UNCHANGED] : "%<";
if (!group_format[NEW])
group_format[NEW]
= group_format[UNCHANGED] ? group_format[UNCHANGED] : "%>";
if (!group_format[UNCHANGED])
group_format[UNCHANGED] = "%=";
if (!group_format[CHANGED])
group_format[CHANGED] = concat (group_format[OLD],
group_format[NEW], "");
}
no_diff_means_no_output =
(output_style == OUTPUT_IFDEF ?
(!*group_format[UNCHANGED]
|| (strcmp (group_format[UNCHANGED], "%=") == 0
&& !*line_format[UNCHANGED]))
: output_style == OUTPUT_SDIFF ? sdiff_skip_common_lines : 1);
switch_string = option_list (argv + 1, optind - 1);
val = compare_files (0, argv[optind], 0, argv[optind + 1], 0);
// Print any messages that were saved up for last.
print_message_queue ();
if (ferror (stdout) || fclose (stdout) != 0)
fatal ("write error");
exit (val);
return val;
}*/
/* Add the compiled form of regexp PATTERN to REGLIST. */
static void
add_regexp (reglist, pattern)
struct regexp_list **reglist;
char const *pattern;
{
struct regexp_list *r;
char const *m;
r = (struct regexp_list *) xmalloc (sizeof (*r));
bzero (r, sizeof (*r));
r->buf.fastmap = xmalloc (256);
m = re_compile_pattern (pattern, strlen (pattern), &r->buf);
if (m != 0)
error ("%s: %s", pattern, m);
/* Add to the start of the list, since it's easier than the end. */
r->next = *reglist;
*reglist = r;
}
static void
usage ()
{
fprintf (stderr, "Usage: %s [options] from-file to-file\n", program);
fprintf (stderr, "Options:\n\
[-abBcdefhHilnNpPqrstTuvwy] [-C lines] [-D name] [-F regexp]\n\
[-I regexp] [-L from-label [-L to-label]] [-S starting-file] [-U lines]\n\
[-W columns] [-x pattern] [-X pattern-file]\n");
fprintf (stderr, "\
[--brief] [--changed-group-format=format] [--context[=lines]] [--ed]\n\
[--exclude=pattern] [--exclude-from=pattern-file] [--expand-tabs]\n\
[--forward-ed] [--horizon-lines=lines] [--ifdef=name]\n\
[--ignore-all-space] [--ignore-blank-lines] [--ignore-case]\n");
fprintf (stderr, "\
[--ignore-matching-lines=regexp] [--ignore-space-change]\n\
[--initial-tab] [--label=from-label [--label=to-label]]\n\
[--left-column] [--minimal] [--new-file] [--new-group-format=format]\n\
[--new-line-format=format] [--old-group-format=format]\n");
fprintf (stderr, "\
[--old-line-format=format] [--paginate] [--rcs] [--recursive]\n\
[--report-identical-files] [--sdiff-merge-assist] [--show-c-function]\n\
[--show-function-line=regexp] [--side-by-side] [--speed-large-files]\n\
[--starting-file=starting-file] [--suppress-common-lines] [--text]\n");
fprintf (stderr, "\
[--unchanged-group-format=format] [--unchanged-line-format=format]\n\
[--unidirectional-new-file] [--unified[=lines]] [--version]\n\
[--width=columns]\n");
exit (2);
}
static int
specify_format (var, value)
char **var;
char *value;
{
int err = *var ? strcmp (*var, value) : 0;
*var = value;
return err;
}
static void
specify_style (style)
enum output_style style;
{
if (output_style != OUTPUT_NORMAL
&& output_style != style)
error ("conflicting specifications of output style", 0, 0);
output_style = style;
}
static char const *
filetype (st)
struct stat const *st;
{
/* See Posix.2 section 4.17.6.1.1 and Table 5-1 for these formats.
To keep diagnostics grammatical, the returned string must start
with a consonant. */
if (S_ISREG (st->st_mode))
{
if (st->st_size == 0)
return "regular empty file";
/* Posix.2 section 5.14.2 seems to suggest that we must read the file
and guess whether it's C, Fortran, etc., but this is somewhat useless
and doesn't reflect historical practice. We're allowed to guess
wrong, so we don't bother to read the file. */
return "regular file";
}
if (S_ISDIR (st->st_mode)) return "directory";
/* other Posix.1 file types */
#ifdef S_ISBLK
if (S_ISBLK (st->st_mode)) return "block special file";
#endif
#ifdef S_ISCHR
if (S_ISCHR (st->st_mode)) return "character special file";
#endif
#ifdef S_ISFIFO
if (S_ISFIFO (st->st_mode)) return "fifo";
#endif
/* other popular file types */
/* S_ISLNK is impossible with `stat'. */
#ifdef S_ISSOCK
if (S_ISSOCK (st->st_mode)) return "socket";
#endif
return "weird file";
}
/* Compare two files (or dirs) with specified names
DIR0/NAME0 and DIR1/NAME1, at level DEPTH in directory recursion.
(if DIR0 is 0, then the name is just NAME0, etc.)
This is self-contained; it opens the files and closes them.
Value is 0 if files are the same, 1 if different,
2 if there is a problem opening them. */
int
compare_files (dir0, name0, dir1, name1, depth)
char const *dir0, *dir1;
char const *name0, *name1;
int depth;
{
struct file_data inf[2];
register int i;
int val;
int same_files;
int failed = 0;
char *free0 = 0, *free1 = 0;
/* If this is directory comparison, perhaps we have a file
that exists only in one of the directories.
If so, just print a message to that effect. */
if (! ((name0 != 0 && name1 != 0)
|| (unidirectional_new_file_flag && name1 != 0)
|| entire_new_file_flag))
{
char const *name = name0 == 0 ? name1 : name0;
char const *dir = name0 == 0 ? dir1 : dir0;
message ("Only in %s: %s\n", dir, name);
/* Return 1 so that diff_dirs will return 1 ("some files differ"). */
return 1;
}
/* Mark any nonexistent file with -1 in the desc field. */
/* Mark unopened files (e.g. directories) with -2. */
inf[0].desc = name0 == 0 ? -1 : -2;
inf[1].desc = name1 == 0 ? -1 : -2;
/* Now record the full name of each file, including nonexistent ones. */
if (name0 == 0)
name0 = name1;
if (name1 == 0)
name1 = name0;
inf[0].name = dir0 == 0 ? name0 : (free0 = dir_file_pathname (dir0, name0));
inf[1].name = dir1 == 0 ? name1 : (free1 = dir_file_pathname (dir1, name1));
/* Stat the files. Record whether they are directories. */
for (i = 0; i <= 1; i++)
{
bzero (&inf[i].stat, sizeof (struct stat));
inf[i].dir_p = 0;
if (inf[i].desc != -1)
{
int stat_result;
if (i && stricmp (inf[i].name, inf[0].name) == 0)
{
inf[i].stat = inf[0].stat;
stat_result = 0;
}
else if (strcmp (inf[i].name, "-") == 0)
{
inf[i].desc = STDIN_FILENO;
stat_result = fstat (STDIN_FILENO, &inf[i].stat);
if (stat_result == 0 && S_ISREG (inf[i].stat.st_mode))
{
off_t pos = lseek (STDIN_FILENO, (off_t) 0, SEEK_CUR);
if (pos == -1)
stat_result = -1;
else
{
if (pos <= inf[i].stat.st_size)
inf[i].stat.st_size -= pos;
else
inf[i].stat.st_size = 0;
/* Posix.2 4.17.6.1.4 requires current time for stdin. */
time (&inf[i].stat.st_mtime);
}
}
}
else
stat_result = stat (inf[i].name, &inf[i].stat);
if (stat_result != 0)
{
perror_with_name (inf[i].name);
failed = 1;
}
else
{
inf[i].dir_p = S_ISDIR (inf[i].stat.st_mode) && inf[i].desc != 0;
if (inf[1 - i].desc == -1)
{
inf[1 - i].dir_p = inf[i].dir_p;
inf[1 - i].stat.st_mode = inf[i].stat.st_mode;
}
}
}
}
if (! failed && depth == 0 && inf[0].dir_p != inf[1].dir_p)
{
/* If one is a directory, and it was specified in the command line,
use the file in that dir with the other file's basename. */
int fnm_arg = inf[0].dir_p;
int dir_arg = 1 - fnm_arg;
char const *fnm = inf[fnm_arg].name;
char const *dir = inf[dir_arg].name;
char const *p = strrchr (fnm, '/');
char const *filename = inf[dir_arg].name
= dir_file_pathname (dir, p ? p + 1 : fnm);
if (strcmp (fnm, "-") == 0)
fatal ("can't compare - to a directory");
if (stat (filename, &inf[dir_arg].stat) != 0)
{
perror_with_name (filename);
failed = 1;
}
else
inf[dir_arg].dir_p = S_ISDIR (inf[dir_arg].stat.st_mode);
}
if (failed)
{
/* If either file should exist but does not, return 2. */
val = 2;
}
#if defined(__MSDOS__) || defined(__NT__) || defined(WIN32)
else if (same_files = 0) /* yes, only ONE equal sign intended! hmo11apr93 */
;
#else
else if ((same_files = inf[0].stat.st_ino == inf[1].stat.st_ino
&& inf[0].stat.st_dev == inf[1].stat.st_dev
&& inf[0].stat.st_size == inf[1].stat.st_size
&& inf[0].desc != -1
&& inf[1].desc != -1)
&& no_diff_means_no_output)
{
/* The two named files are actually the same physical file.
We know they are identical without actually reading them. */
val = 0;
}
#endif /*__MSDOS__||__NT__*/
else if (inf[0].dir_p & inf[1].dir_p)
{
if (output_style == OUTPUT_IFDEF)
fatal ("-D option not supported with directories");
/* If both are directories, compare the files in them. */
if (depth > 0 && !recursive)
{
/* But don't compare dir contents one level down
unless -r was specified. */
message ("Common subdirectories: %s and %s\n",
inf[0].name, inf[1].name);
val = 0;
}
else
{
val = diff_dirs (inf, compare_files, depth);
}
}
else if ((inf[0].dir_p | inf[1].dir_p)
|| (depth > 0
&& (! S_ISREG (inf[0].stat.st_mode)
|| ! S_ISREG (inf[1].stat.st_mode))))
{
/* Perhaps we have a subdirectory that exists only in one directory.
If so, just print a message to that effect. */
if (inf[0].desc == -1 || inf[1].desc == -1)
{
if ((inf[0].dir_p | inf[1].dir_p)
&& recursive
&& (entire_new_file_flag
|| (unidirectional_new_file_flag && inf[0].desc == -1)))
val = diff_dirs (inf, compare_files, depth);
else
{
char const *dir = (inf[0].desc == -1) ? dir1 : dir0;
/* See Posix.2 section 4.17.6.1.1 for this format. */
message ("Only in %s: %s\n", dir, name0);
val = 1;
}
}
else
{
/* We have two files that are not to be compared. */
/* See Posix.2 section 4.17.6.1.1 for this format. */
//message5 ("File %s is a %s while file %s is a %s\n",
// inf[0].name, filetype (&inf[0].stat),
// inf[1].name, filetype (&inf[1].stat));
/* This is a difference. */
val = 1;
}
}
else if ((no_details_flag & ~ignore_some_changes)
&& inf[0].stat.st_size != inf[1].stat.st_size
&& (inf[0].desc == -1 || S_ISREG (inf[0].stat.st_mode))
&& (inf[1].desc == -1 || S_ISREG (inf[1].stat.st_mode)))
{
message ("Files %s and %s differ\n", inf[0].name, inf[1].name);
val = 1;
}
else
{
/* Both exist and neither is a directory. */
int o_binary = always_text_flag ? O_BINARY : 0;
/* Open the files and record their descriptors. */
if (inf[0].desc == -2)
if ((inf[0].desc = open (inf[0].name, O_RDONLY|o_binary, 0)) < 0)
{
perror_with_name (inf[0].name);
failed = 1;
}
if (inf[1].desc == -2)
if (same_files)
inf[1].desc = inf[0].desc;
else if ((inf[1].desc = open (inf[1].name, O_RDONLY|o_binary, 0)) < 0)
{
perror_with_name (inf[1].name);
failed = 1;
}
/* Compare the files, if no error was found. */
val = failed ? 2 : diff_2_files (inf, depth);
/* Close the file descriptors. */
if (inf[0].desc >= 0 && close (inf[0].desc) != 0)
{
perror_with_name (inf[0].name);
val = 2;
}
if (inf[1].desc >= 0 && inf[0].desc != inf[1].desc
&& close (inf[1].desc) != 0)
{
perror_with_name (inf[1].name);
val = 2;
}
}
/* Now the comparison has been done, if no error prevented it,
and VAL is the value this function will return. */
if (val == 0 && !inf[0].dir_p)
{
if (print_file_same_flag)
message ("Files %s and %s are identical\n",
inf[0].name, inf[1].name);
}
else
fflush (stdout);
if (free0)
free (free0);
if (free1)
free (free1);
return val;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -