⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmp.c

📁 制作2.6内核的CLFS时 使用的diffutils-2.8.7.tar.gz包
💻 C
📖 第 1 页 / 共 2 页
字号:
/* cmp - compare two files byte by byte   Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 2001,   2002, 2004 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   the Free Software Foundation; either version 2, or (at your option)   any later version.   This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with this program; see the file COPYING.   If not, write to the Free Software Foundation,   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */#include "system.h"#include "paths.h"#include <stdio.h>#include <c-stack.h>#include <cmpbuf.h>#include <error.h>#include <exit.h>#include <exitfail.h>#include <file-type.h>#include <getopt.h>#include <hard-locale.h>#include <inttostr.h>#include <setmode.h>#include <unlocked-io.h>#include <version-etc.h>#include <xalloc.h>#include <xstrtol.h>#if defined LC_MESSAGES && ENABLE_NLS# define hard_locale_LC_MESSAGES hard_locale (LC_MESSAGES)#else# define hard_locale_LC_MESSAGES 0#endifstatic int cmp (void);static off_t file_position (int);static size_t block_compare (word const *, word const *);static size_t block_compare_and_count (word const *, word const *, off_t *);static void sprintc (char *, unsigned char);/* Name under which this program was invoked.  */char *program_name;/* Filenames of the compared files.  */static char const *file[2];/* File descriptors of the files.  */static int file_desc[2];/* Status of the files.  */static struct stat stat_buf[2];/* Read buffers for the files.  */static word *buffer[2];/* Optimal block size for the files.  */static size_t buf_size;/* Initial prefix to ignore for each file.  */static off_t ignore_initial[2];/* Number of bytes to compare.  */static uintmax_t bytes = UINTMAX_MAX;/* Output format.  */static enum comparison_type  {    type_first_diff,	/* Print the first difference.  */    type_all_diffs,	/* Print all differences.  */    type_status		/* Exit status only.  */  } comparison_type;/* If nonzero, print values of bytes quoted like cat -t does. */static bool opt_print_bytes;/* Values for long options that do not have single-letter equivalents.  */enum{  HELP_OPTION = CHAR_MAX + 1};static struct option const long_options[] ={  {"print-bytes", 0, 0, 'b'},  {"print-chars", 0, 0, 'c'}, /* obsolescent as of diffutils 2.7.3 */  {"ignore-initial", 1, 0, 'i'},  {"verbose", 0, 0, 'l'},  {"bytes", 1, 0, 'n'},  {"silent", 0, 0, 's'},  {"quiet", 0, 0, 's'},  {"version", 0, 0, 'v'},  {"help", 0, 0, HELP_OPTION},  {0, 0, 0, 0}};static void try_help (char const *, char const *) __attribute__((noreturn));static voidtry_help (char const *reason_msgid, char const *operand){  if (reason_msgid)    error (0, 0, _(reason_msgid), operand);  error (EXIT_TROUBLE, 0,	 _("Try `%s --help' for more information."), program_name);  abort ();}static char const valid_suffixes[] = "kKMGTPEZY0";/* Update ignore_initial[F] according to the result of parsing an   *operand ARGPTR of --ignore-initial, updating *ARGPTR to point   *after the operand.  If DELIMITER is nonzero, the operand may be   *followed by DELIMITER; otherwise it must be null-terminated.  */static voidspecify_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)    try_help ("invalid --ignore-initial value `%s'", arg);  if (ignore_initial[f] < o)    ignore_initial[f] = o;}/* Specify the output format.  */static voidspecify_comparison_type (enum comparison_type t){  if (comparison_type && comparison_type != t)    try_help ("options -l and -s are incompatible", 0);  comparison_type = t;}static voidcheck_stdout (void){  if (ferror (stdout))    error (EXIT_TROUBLE, 0, "%s", _("write failed"));  else if (fclose (stdout) != 0)    error (EXIT_TROUBLE, errno, "%s", _("standard output"));}static char const * const option_help_msgid[] = {  N_("-b  --print-bytes  Print differing bytes."),  N_("-i SKIP  --ignore-initial=SKIP  Skip the first SKIP bytes of input."),  N_("-i SKIP1:SKIP2  --ignore-initial=SKIP1:SKIP2"),  N_("  Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2."),  N_("-l  --verbose  Output byte numbers and values of all differing bytes."),  N_("-n LIMIT  --bytes=LIMIT  Compare at most LIMIT bytes."),  N_("-s  --quiet  --silent  Output nothing; yield exit status only."),  N_("-v  --version  Output version info."),  N_("--help  Output this help."),  0};static voidusage (void){  char const * const *p;  printf (_("Usage: %s [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]\n"),	  program_name);  printf ("%s\n\n", _("Compare two files byte by byte."));  for (p = option_help_msgid;  *p;  p++)    printf ("  %s\n", _(*p));  printf ("\n%s\n%s\n\n%s\n%s\n\n%s\n",	  _("SKIP1 and SKIP2 are the number of bytes to skip in each file."),	  _("SKIP values may be followed by the following multiplicative suffixes:\n\kB 1000, K 1024, MB 1,000,000, M 1,048,576,\n\GB 1,000,000,000, G 1,073,741,824, and so on for T, P, E, Z, Y."),	  _("If a FILE is `-' or missing, read standard input."),	  _("Exit status is 0 if inputs are the same, 1 if different, 2 if trouble."),	  _("Report bugs to <bug-gnu-utils@gnu.org>."));}intmain (int argc, char **argv){  int c, f, exit_status;  size_t words_per_buffer;  exit_failure = EXIT_TROUBLE;  initialize_main (&argc, &argv);  program_name = argv[0];  setlocale (LC_ALL, "");  bindtextdomain (PACKAGE, LOCALEDIR);  textdomain (PACKAGE);  c_stack_action (0);  /* Parse command line options.  */  while ((c = getopt_long (argc, argv, "bci:ln:sv", long_options, 0))	 != -1)    switch (c)      {      case 'b':      case 'c': /* 'c' is obsolescent as of diffutils 2.7.3 */	opt_print_bytes = true;	break;      case 'i':	specify_ignore_initial (0, &optarg, ':');	if (*optarg++ == ':')	  specify_ignore_initial (1, &optarg, 0);	else if (ignore_initial[1] < ignore_initial[0])	  ignore_initial[1] = ignore_initial[0];	break;      case 'l':	specify_comparison_type (type_all_diffs);	break;      case 'n':	{	  uintmax_t n;	  if (xstrtoumax (optarg, 0, 0, &n, valid_suffixes) != LONGINT_OK)	    try_help ("invalid --bytes value `%s'", optarg);	  if (n < bytes)	    bytes = n;	}	break;      case 's':	specify_comparison_type (type_status);	break;      case 'v':	/* TRANSLATORS: Please translate the second "o" in "Torbjorn	   Granlund" to an o-with-umlaut (U+00F6, LATIN SMALL LETTER O	   WITH DIAERESIS) if possible.  */	version_etc (stdout, "cmp", PACKAGE_NAME, PACKAGE_VERSION,		     _("Torbjorn Granlund"), "David MacKenzie", (char *) 0);	check_stdout ();	return EXIT_SUCCESS;      case HELP_OPTION:	usage ();	check_stdout ();	return EXIT_SUCCESS;      default:	try_help (0, 0);      }  if (optind == argc)    try_help ("missing operand after `%s'", argv[argc - 1]);  file[0] = argv[optind++];  file[1] = optind < argc ? argv[optind++] : "-";  for (f = 0; f < 2 && optind < argc; f++)    {      char *arg = argv[optind++];      specify_ignore_initial (f, &arg, 0);    }  if (optind < argc)    try_help ("extra operand `%s'", argv[optind]);  for (f = 0; f < 2; f++)    {      /* If file[1] is "-", treat it first; this avoids a misdiagnostic if	 stdin is closed and opening file[0] yields file descriptor 0.  */      int f1 = f ^ (strcmp (file[1], "-") == 0);      /* Two files with the same name and offset are identical.	 But wait until we open the file once, for proper diagnostics.  */      if (f && ignore_initial[0] == ignore_initial[1]	  && 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 (file_desc[f1] < 0 || fstat (file_desc[f1], stat_buf + f1) != 0)	{	  if (file_desc[f1] < 0 && comparison_type == type_status)	    exit (EXIT_TROUBLE);	  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,     they are identical.  */  if (0 < same_file (&stat_buf[0], &stat_buf[1])      && same_file_attributes (&stat_buf[0], &stat_buf[1])      && file_position (0) == file_position (1))    return EXIT_SUCCESS;  /* If output is redirected to the null device, we may assume `-s'.  */  if (comparison_type != type_status)    {      struct stat outstat, nullstat;      if (fstat (STDOUT_FILENO, &outstat) == 0	  && stat (NULL_DEVICE, &nullstat) == 0	  && 0 < same_file (&outstat, &nullstat))	comparison_type = type_status;    }  /* If only a return code is needed,     and if both input descriptors are associated with plain files,     conclude that the files differ if they have different sizes     and if more bytes will be compared than are in the smaller file.  */  if (comparison_type == type_status      && S_ISREG (stat_buf[0].st_mode)      && S_ISREG (stat_buf[1].st_mode))    {      off_t s0 = stat_buf[0].st_size - file_position (0);      off_t s1 = stat_buf[1].st_size - file_position (1);      if (s0 < 0)	s0 = 0;      if (s1 < 0)	s1 = 0;      if (s0 != s1 && MIN (s0, s1) < bytes)	exit (EXIT_FAILURE);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -