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

📄 io.c

📁 制作2.6内核的CLFS时 使用的diffutils-2.8.7.tar.gz包
💻 C
📖 第 1 页 / 共 2 页
字号:
/* File I/O for GNU DIFF.   Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002,   2004 Free Software Foundation, Inc.   This file is part of GNU DIFF.   GNU DIFF 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.   GNU DIFF 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 "diff.h"#include <cmpbuf.h>#include <file-type.h>#include <setmode.h>#include <xalloc.h>/* Rotate an unsigned value to the left.  */#define ROL(v, n) ((v) << (n) | (v) >> (sizeof (v) * CHAR_BIT - (n)))/* Given a hash value and a new character, return a new hash value.  */#define HASH(h, c) ((c) + ROL (h, 7))/* The type of a hash value.  */typedef size_t hash_value;verify (hash_value_is_unsigned, ! 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,   but only while the classes are being computed.   Afterward, each class is represented by a number.  */struct equivclass{  lin next;		/* Next item in this bucket.  */  hash_value hash;	/* Hash of lines in this class.  */  char const *line;	/* A line that fits this class.  */  size_t length;	/* That line's length, not counting its newline.  */};/* Hash-table: array of buckets, each being a chain of equivalence classes.   buckets[-1] is reserved for incomplete lines.  */static lin *buckets;/* Number of buckets in the hash table array, not counting buckets[-1].  */static size_t nbuckets;/* Array in which the equivalence classes are allocated.   The bucket-chains go through the elements in this array.   The number of an equivalence class is its index in this array.  */static struct equivclass *equivs;/* Index of first free element in the array `equivs'.  */static lin equivs_index;/* Number of elements allocated in the array `equivs'.  */static lin equivs_alloc;/* Read a block of data into a file buffer, checking for EOF and error.  */voidfile_block_read (struct file_data *current, size_t size){  if (size && ! current->eof)    {      size_t s = block_read (current->desc,			     FILE_BUFFER (current) + current->buffered, size);      if (s == SIZE_MAX)	pfatal_with_name (current->name);      current->buffered += s;      current->eof = s < size;    }}/* Check for binary files and compare them for exact identity.  *//* Return 1 if BUF contains a non text character.   SIZE is the number of characters in BUF.  */#define binary_file_p(buf, size) (memchr (buf, 0, size) != 0)/* Get ready to read the current file.   Return nonzero if SKIP_TEST is zero,   and if it appears to be a binary file.  */static boolsip (struct file_data *current, bool skip_test){  /* If we have a nonexistent file at this stage, treat it as empty.  */  if (current->desc < 0)    {      /* Leave room for a sentinel.  */      current->bufsize = sizeof (word);      current->buffer = xmalloc (current->bufsize);    }  else    {      current->bufsize = buffer_lcm (sizeof (word),				     STAT_BLOCKSIZE (current->stat),				     PTRDIFF_MAX - 2 * sizeof (word));      current->buffer = xmalloc (current->bufsize);      if (! 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;	  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);	}    }  current->buffered = 0;  current->eof = false;  return false;}/* Slurp the rest of the current file completely into memory.  */static voidslurp (struct file_data *current){  size_t cc;  if (current->desc < 0)    {      /* The file is nonexistent.  */      return;    }  if (S_ISREG (current->stat.st_mode))    {      /* It's a regular file; slurp in the rest all at once.  */      /* Get the size out of the stat block.	 Allocate just enough room for appended newline plus word sentinel,	 plus word-alignment since we want the buffer word-aligned.  */      size_t file_size = current->stat.st_size;      cc = file_size + 2 * sizeof (word) - file_size % sizeof (word);      if (file_size != current->stat.st_size || cc < file_size	  || PTRDIFF_MAX <= cc)	xalloc_die ();      if (current->bufsize < cc)	{	  current->bufsize = cc;	  current->buffer = xrealloc (current->buffer, cc);	}      /* Try to read at least 1 more byte than the size indicates, to	 detect whether the file is growing.  This is a nicety for	 users who run 'diff' on files while they are changing.  */      if (current->buffered <= file_size)	{	  file_block_read (current, file_size + 1 - current->buffered);	  if (current->buffered <= file_size)	    return;	}    }  /* It's not a regular file, or it's a growing regular file; read it,     growing the buffer as needed.  */  file_block_read (current, current->bufsize - current->buffered);  if (current->buffered)    {      while (current->buffered == current->bufsize)	{	  if (PTRDIFF_MAX / 2 - sizeof (word) < current->bufsize)	    xalloc_die ();	  current->bufsize *= 2;	  current->buffer = xrealloc (current->buffer, current->bufsize);	  file_block_read (current, current->bufsize - current->buffered);	}      /* Allocate just enough room for appended newline plus word	 sentinel, plus word-alignment.  */      cc = current->buffered + 2 * sizeof (word);      current->bufsize = cc - cc % sizeof (word);      current->buffer = xrealloc (current->buffer, current->bufsize);    }}/* Split the file into lines, simultaneously computing the equivalence   class for each line.  */static voidfind_and_hash_each_line (struct file_data *current){  hash_value h;  char const *p = current->prefix_end;  unsigned char c;  lin i, *bucket;  size_t length;  /* Cache often-used quantities in local variables to help the compiler.  */  char const **linbuf = current->linbuf;  lin alloc_lines = current->alloc_lines;  lin line = 0;  lin linbuf_base = current->linbuf_base;  lin *cureqs = xmalloc (alloc_lines * sizeof *cureqs);  struct equivclass *eqs = equivs;  lin eqs_index = equivs_index;  lin eqs_alloc = equivs_alloc;  char const *suffix_begin = current->suffix_begin;  char const *bufend = FILE_BUFFER (current) + current->buffered;  bool diff_length_compare_anyway =    ignore_white_space != IGNORE_NO_WHITE_SPACE;  bool same_length_diff_contents_compare_anyway =    diff_length_compare_anyway | ignore_case;  while (p < suffix_begin)    {      char const *ip = p;      h = 0;      /* Hash this line until we find a newline.  */      if (ignore_case)	switch (ignore_white_space)	  {	  case IGNORE_ALL_SPACE:	    while ((c = *p++) != '\n')	      if (! isspace (c))		h = HASH (h, tolower (c));	    break;	  case IGNORE_SPACE_CHANGE:	    while ((c = *p++) != '\n')	      {		if (isspace (c))		  {		    do		      if ((c = *p++) == '\n')			goto hashing_done;		    while (isspace (c));		    h = HASH (h, ' ');		  }		/* C is now the first non-space.  */		h = HASH (h, tolower (c));	      }	    break;	  case IGNORE_TAB_EXPANSION:	    {	      size_t column = 0;	      while ((c = *p++) != '\n')		{		  size_t repetitions = 1;		  switch (c)		    {		    case '\b':		      column -= 0 < column;		      break;		    case '\t':		      c = ' ';		      repetitions = tabsize - column % tabsize;		      column = (column + repetitions < column				? 0				: column + repetitions);		      break;		    case '\r':		      column = 0;		      break;		    default:		      c = tolower (c);		      column++;		      break;		    }		  do		    h = HASH (h, c);		  while (--repetitions != 0);		}	    }	    break;	  default:	    while ((c = *p++) != '\n')	      h = HASH (h, tolower (c));	    break;	  }      else	switch (ignore_white_space)	  {	  case IGNORE_ALL_SPACE:	    while ((c = *p++) != '\n')	      if (! isspace (c))		h = HASH (h, c);	    break;	  case IGNORE_SPACE_CHANGE:	    while ((c = *p++) != '\n')	      {		if (isspace (c))		  {		    do		      if ((c = *p++) == '\n')			goto hashing_done;		    while (isspace (c));		    h = HASH (h, ' ');		  }		/* C is now the first non-space.  */		h = HASH (h, c);	      }	    break;	  case IGNORE_TAB_EXPANSION:	    {	      size_t column = 0;	      while ((c = *p++) != '\n')		{		  size_t repetitions = 1;		  switch (c)		    {		    case '\b':		      column -= 0 < column;		      break;		    case '\t':		      c = ' ';		      repetitions = tabsize - column % tabsize;		      column = (column + repetitions < column				? 0				: column + repetitions);		      break;		    case '\r':		      column = 0;		      break;		    default:		      column++;		      break;		    }		  do		    h = HASH (h, c);		  while (--repetitions != 0);		}	    }	    break;	  default:	    while ((c = *p++) != '\n')	      h = HASH (h, c);	    break;	  }   hashing_done:;      bucket = &buckets[h % nbuckets];      length = p - ip - 1;      if (p == bufend	  && current->missing_newline	  && ROBUST_OUTPUT_STYLE (output_style))	{	  /* This line is incomplete.  If this is significant,	     put the line into buckets[-1].  */	  if (ignore_white_space < IGNORE_SPACE_CHANGE)	    bucket = &buckets[-1];	  /* Omit the inserted newline when computing linbuf later.  */	  p--;	  bufend = suffix_begin = p;	}      for (i = *bucket;  ;  i = eqs[i].next)	if (!i)	  {	    /* Create a new equivalence class in this bucket.  */	    i = eqs_index++;	    if (i == eqs_alloc)	      {		if (PTRDIFF_MAX / (2 * sizeof *eqs) <= eqs_alloc)		  xalloc_die ();		eqs_alloc *= 2;		eqs = xrealloc (eqs, eqs_alloc * sizeof *eqs);	      }	    eqs[i].next = *bucket;	    eqs[i].hash = h;	    eqs[i].line = ip;	    eqs[i].length = length;	    *bucket = i;	    break;	  }	else if (eqs[i].hash == h)	  {	    char const *eqline = eqs[i].line;	    /* Reuse existing class if lines_differ reports the lines

⌨️ 快捷键说明

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