📄 diff.c
字号:
"-s --report-identical-files Report when two files are the same.","-x PAT --exclude=PAT Exclude files that match PAT.","-X FILE --exclude-from=FILE Exclude files that match any pattern in FILE.","-S FILE --starting-file=FILE Start with FILE when comparing directories.\n","--horizon-lines=NUM Keep NUM lines of the common prefix and suffix.","-d --minimal Try hard to find a smaller set of changes.","-H --speed-large-files Assume large files and many scattered small changes.\n","-v --version Output version info.","--help Output this help.",0};static voidusage (){ char const * const *p; printf ("Usage: %s [OPTION]... FILE1 FILE2\n\n", program_name); for (p = option_help; *p; p++) printf (" %s\n", *p); printf ("\nIf FILE1 or FILE2 is `-', read standard input.\n");}static intspecify_format (var, value) char **var; char *value;{ int err = *var ? strcmp (*var, value) : 0; *var = value; return err;}static voidspecify_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 Posix.1b file types */#ifdef S_TYPEISMQ if (S_TYPEISMQ (st)) return "message queue";#endif#ifdef S_TYPEISSEM if (S_TYPEISSEM (st)) return "semaphore";#endif#ifdef S_TYPEISSHM if (S_TYPEISSHM (st)) return "shared memory object";#endif /* other popular file types */ /* S_ISLNK is impossible with `fstat' and `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. */static intcompare_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; } bzero (inf, sizeof (inf)); /* 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++) { if (inf[i].desc != -1) { int stat_result; if (i && filename_cmp (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 = filename_lastdirchar (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; } else if ((same_files = inf[0].desc != -1 && inf[1].desc != -1 && 0 < same_file (&inf[0].stat, &inf[1].stat)) && 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; } 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. */ /* Open the files and record their descriptors. */ if (inf[0].desc == -2) if ((inf[0].desc = open (inf[0].name, O_RDONLY, 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, 0)) < 0) { perror_with_name (inf[1].name); failed = 1; }#if HAVE_SETMODE if (binary_I_O) for (i = 0; i <= 1; i++) if (0 <= inf[i].desc) setmode (inf[i].desc, O_BINARY);#endif /* 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 + -