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

📄 util.c

📁 制作2.6内核的CLFS时 使用的diffutils-2.8.7.tar.gz包
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Support routines 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 <dirname.h>#include <error.h>#include <quotesys.h>#include <xalloc.h>char const pr_program[] = PR_PROGRAM;/* Queue up one-line messages to be printed at the end,   when -l is specified.  Each message is recorded with a `struct msg'.  */struct msg{  struct msg *next;  char args[1]; /* Format + 4 args, each '\0' terminated, concatenated.  */};/* Head of the chain of queues messages.  */static struct msg *msg_chain;/* Tail of the chain of queues messages.  */static struct msg **msg_chain_end = &msg_chain;/* Use when a system call returns non-zero status.   NAME should normally be the file name.  */voidperror_with_name (char const *name){  error (0, errno, "%s", name);}/* Use when a system call returns non-zero status and that is fatal.  */voidpfatal_with_name (char const *name){  int e = errno;  print_message_queue ();  error (EXIT_TROUBLE, e, "%s", name);  abort ();}/* Print an error message containing MSGID, then exit.  */voidfatal (char const *msgid){  print_message_queue ();  error (EXIT_TROUBLE, 0, "%s", _(msgid));  abort ();}/* Like printf, except if -l in effect then save the message and print later.   This is used for things like "Only in ...".  */voidmessage (char const *format_msgid, char const *arg1, char const *arg2){  message5 (format_msgid, arg1, arg2, 0, 0);}voidmessage5 (char const *format_msgid, char const *arg1, char const *arg2,	  char const *arg3, char const *arg4){  if (paginate)    {      char *p;      char const *arg[5];      int i;      size_t size[5];      size_t total_size = offsetof (struct msg, args);      struct msg *new;      arg[0] = format_msgid;      arg[1] = arg1;      arg[2] = arg2;      arg[3] = arg3 ? arg3 : "";      arg[4] = arg4 ? arg4 : "";      for (i = 0;  i < 5;  i++)	total_size += size[i] = strlen (arg[i]) + 1;      new = xmalloc (total_size);      for (i = 0, p = new->args;  i < 5;  p += size[i++])	memcpy (p, arg[i], size[i]);      *msg_chain_end = new;      new->next = 0;      msg_chain_end = &new->next;    }  else    {      if (sdiff_merge_assist)	putchar (' ');      printf (_(format_msgid), arg1, arg2, arg3, arg4);    }}/* Output all the messages that were saved up by calls to `message'.  */voidprint_message_queue (void){  char const *arg[5];  int i;  struct msg *m = msg_chain;  while (m)    {      struct msg *next = m->next;      arg[0] = m->args;      for (i = 0;  i < 4;  i++)	arg[i + 1] = arg[i] + strlen (arg[i]) + 1;      printf (_(arg[0]), arg[1], arg[2], arg[3], arg[4]);      free (m);      m = next;    }}/* Call before outputting the results of comparing files NAME0 and NAME1   to set up OUTFILE, the stdio stream for the output to go to.   Usually, OUTFILE is just stdout.  But when -l was specified   we fork off a `pr' and make OUTFILE a pipe to it.   `pr' then outputs to our stdout.  */static char const *current_name0;static char const *current_name1;static bool currently_recursive;voidsetup_output (char const *name0, char const *name1, bool recursive){  current_name0 = name0;  current_name1 = name1;  currently_recursive = recursive;  outfile = 0;}#if HAVE_WORKING_FORK || HAVE_WORKING_VFORKstatic pid_t pr_pid;#endifvoidbegin_output (void){  char *name;  if (outfile != 0)    return;  /* Construct the header of this piece of diff.  */  name = xmalloc (strlen (current_name0) + strlen (current_name1)		  + strlen (switch_string) + 7);  /* POSIX 1003.1-2001 specifies this format.  But there are some bugs in     the standard: it says that we must print only the last component     of the pathnames, and it requires two spaces after "diff" if     there are no options.  These requirements are silly and do not     match historical practice.  */  sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);  if (paginate)    {      if (fflush (stdout) != 0)	pfatal_with_name (_("write failed"));      /* Make OUTFILE a pipe to a subsidiary `pr'.  */      {#if HAVE_WORKING_FORK || HAVE_WORKING_VFORK	int pipes[2];	if (pipe (pipes) != 0)	  pfatal_with_name ("pipe");	pr_pid = vfork ();	if (pr_pid < 0)	  pfatal_with_name ("fork");	if (pr_pid == 0)	  {	    close (pipes[1]);	    if (pipes[0] != STDIN_FILENO)	      {		if (dup2 (pipes[0], STDIN_FILENO) < 0)		  pfatal_with_name ("dup2");		close (pipes[0]);	      }	    execl (pr_program, pr_program, "-h", name, (char *) 0);	    _exit (errno == ENOENT ? 127 : 126);	  }	else	  {	    close (pipes[0]);	    outfile = fdopen (pipes[1], "w");	    if (!outfile)	      pfatal_with_name ("fdopen");	  }#else	char *command = xmalloc (sizeof pr_program - 1 + 7				 + quote_system_arg ((char *) 0, 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 = 0;	errno = 0;	outfile = popen (command, "w");	if (!outfile)	  pfatal_with_name (command);	free (command);#endif      }    }  else    {      /* If -l was not specified, output the diff straight to `stdout'.  */      outfile = stdout;      /* If handling multiple files (because scanning a directory),	 print which files the following output is about.  */      if (currently_recursive)	printf ("%s\n", name);    }  free (name);  /* A special header is needed at the beginning of context output.  */  switch (output_style)    {    case OUTPUT_CONTEXT:      print_context_header (files, false);      break;    case OUTPUT_UNIFIED:      print_context_header (files, true);      break;    default:      break;    }}/* Call after the end of output of diffs for one file.   Close OUTFILE and get rid of the `pr' subfork.  */voidfinish_output (void){  if (outfile != 0 && outfile != stdout)    {      int status;      int wstatus;      int werrno = 0;      if (ferror (outfile))	fatal ("write failed");#if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK)      wstatus = pclose (outfile);      if (wstatus == -1)	werrno = errno;#else      if (fclose (outfile) != 0)	pfatal_with_name (_("write failed"));      if (waitpid (pr_pid, &wstatus, 0) < 0)	pfatal_with_name ("waitpid");#endif      status = (! werrno && WIFEXITED (wstatus)		? WEXITSTATUS (wstatus)		: INT_MAX);      if (status)	error (EXIT_TROUBLE, werrno,	       _(status == 126		 ? "subsidiary program `%s' could not be invoked"		 : status == 127		 ? "subsidiary program `%s' not found"		 : status == INT_MAX		 ? "subsidiary program `%s' failed"		 : "subsidiary program `%s' failed (exit status %d)"),	       pr_program, status);    }  outfile = 0;}/* Compare two lines (typically one from each input file)   according to the command line options.   For efficiency, this is invoked only when the lines do not match exactly   but an option like -i might cause us to ignore the difference.   Return nonzero if the lines differ.  */boollines_differ (char const *s1, char const *s2){  register char const *t1 = s1;  register char const *t2 = s2;  size_t column = 0;  while (1)    {      register unsigned char c1 = *t1++;      register unsigned char c2 = *t2++;      /* Test for exact char equality first, since it's a common case.  */      if (c1 != c2)	{	  switch (ignore_white_space)	    {	    case IGNORE_ALL_SPACE:	      /* For -w, just skip past any white space.  */	      while (isspace (c1) && c1 != '\n') c1 = *t1++;	      while (isspace (c2) && c2 != '\n') c2 = *t2++;	      break;	    case IGNORE_SPACE_CHANGE:	      /* For -b, advance past any sequence of white space in		 line 1 and consider it just one space, or nothing at		 all if it is at the end of the line.  */	      if (isspace (c1))		{		  while (c1 != '\n')		    {		      c1 = *t1++;		      if (! isspace (c1))			{			  --t1;			  c1 = ' ';			  break;			}		    }		}	      /* Likewise for line 2.  */	      if (isspace (c2))		{		  while (c2 != '\n')		    {		      c2 = *t2++;		      if (! isspace (c2))			{			  --t2;			  c2 = ' ';			  break;			}		    }		}	      if (c1 != c2)		{		  /* If we went too far when doing the simple test		     for equality, go back to the first non-white-space		     character in both sides and try again.  */		  if (c2 == ' ' && c1 != '\n'		      && s1 + 1 < t1		      && isspace ((unsigned char) t1[-2]))		    {		      --t1;		      continue;		    }

⌨️ 快捷键说明

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