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

📄 util.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Support routines for GNU DIFF.   Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.This file is part of GNU DIFF.GNU DIFF is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU DIFF; see the file COPYING.  If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  *//* $FreeBSD: src/contrib/diff/util.c,v 1.2.6.2 2000/09/20 02:24:32 jkh Exp $ */#include "diff.h"#ifndef PR_PROGRAM#define PR_PROGRAM "/bin/pr"#endif/* 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 const *format;  char const *arg1;  char const *arg2;  char const *arg3;  char const *arg4;};/* 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.   TEXT should normally be the file name.  */voidperror_with_name (text)     char const *text;{  int e = errno;  fprintf (stderr, "%s: ", program_name);  errno = e;  perror (text);}/* Use when a system call returns non-zero status and that is fatal.  */voidpfatal_with_name (text)     char const *text;{  int e = errno;  print_message_queue ();  fprintf (stderr, "%s: ", program_name);  errno = e;  perror (text);  exit (2);}/* Print an error message from the format-string FORMAT   with args ARG1 and ARG2.  */voiderror (format, arg, arg1)     char const *format, *arg, *arg1;{  fprintf (stderr, "%s: ", program_name);  fprintf (stderr, format, arg, arg1);  fprintf (stderr, "\n");}/* Print an error message containing the string TEXT, then exit.  */voidfatal (m)     char const *m;{  print_message_queue ();  error ("%s", m, 0);  exit (2);}/* Like printf, except if -l in effect then save the message and print later.   This is used for things like "binary files differ" and "Only in ...".  */voidmessage (format, arg1, arg2)     char const *format, *arg1, *arg2;{  message5 (format, arg1, arg2, 0, 0);}voidmessage5 (format, arg1, arg2, arg3, arg4)     char const *format, *arg1, *arg2, *arg3, *arg4;{  if (paginate_flag)    {      struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));      new->format = format;      new->arg1 = concat (arg1, "", "");      new->arg2 = concat (arg2, "", "");      new->arg3 = arg3 ? concat (arg3, "", "") : 0;      new->arg4 = arg4 ? concat (arg4, "", "") : 0;      new->next = 0;      *msg_chain_end = new;      msg_chain_end = &new->next;    }  else    {      if (sdiff_help_sdiff)	putchar (' ');      printf (format, arg1, arg2, arg3, arg4);    }}/* Output all the messages that were saved up by calls to `message'.  */voidprint_message_queue (){  struct msg *m;  for (m = msg_chain; m; m = m->next)    printf (m->format, m->arg1, m->arg2, m->arg3, m->arg4);}/* 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 int current_depth;voidsetup_output (name0, name1, depth)     char const *name0, *name1;     int depth;{  current_name0 = name0;  current_name1 = name1;  current_depth = depth;  outfile = 0;}#if HAVE_FORKstatic pid_t pr_pid;#endifvoidbegin_output (){  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.2 section 4.17.6.1.1 specifies this format.  But there is a     bug in the first printing (IEEE Std 1003.2-1992 p 251 l 3304):     it says that we must print only the last component of the pathnames.     This requirement is silly and does not match historical practice.  */  sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);  if (paginate_flag)    {      /* Make OUTFILE a pipe to a subsidiary `pr'.  */#if HAVE_FORK      int pipes[2];      if (pipe (pipes) != 0)	pfatal_with_name ("pipe");      fflush (stdout);      pr_pid = fork ();      if (pr_pid < 0)	pfatal_with_name ("vfork");      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]);	    }#ifdef __FreeBSD__	  execl (PR_PROGRAM, PR_PROGRAM, "-F", "-h", name, 0);#else	  execl (PR_PROGRAM, PR_PROGRAM, "-f", "-h", name, 0);#endif	  pfatal_with_name (PR_PROGRAM);	}      else	{	  close (pipes[0]);	  outfile = fdopen (pipes[1], "w");	  if (!outfile)	    pfatal_with_name ("fdopen");	}#else /* ! HAVE_FORK */      char *command = xmalloc (4 * strlen (name) + strlen (PR_PROGRAM) + 10);      char *p;      char const *a = name;      sprintf (command, "%s -f -h ", PR_PROGRAM);      p = command + strlen (command);      SYSTEM_QUOTE_ARG (p, a);      *p = 0;      outfile = popen (command, "w");      if (!outfile)	pfatal_with_name (command);      free (command);#endif /* ! HAVE_FORK */    }  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 (current_depth > 0)	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, 0);      break;    case OUTPUT_UNIFIED:      print_context_header (files, 1);      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 (){  if (outfile != 0 && outfile != stdout)    {      int wstatus;      if (ferror (outfile))	fatal ("write error");#if ! HAVE_FORK      wstatus = pclose (outfile);#else /* HAVE_FORK */      if (fclose (outfile) != 0)	pfatal_with_name ("write error");      if (waitpid (pr_pid, &wstatus, 0) < 0)	pfatal_with_name ("waitpid");#endif /* HAVE_FORK */      if (wstatus != 0)	fatal ("subsidiary pr failed");    }  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.  */intline_cmp (s1, s2)     char const *s1, *s2;{  register unsigned char const *t1 = (unsigned char const *) s1;  register unsigned char const *t2 = (unsigned char const *) s2;  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)	{	  /* Ignore horizontal white space if -b or -w is specified.  */	  if (ignore_all_space_flag)	    {	      /* For -w, just skip past any white space.  */	      while (ISSPACE (c1) && c1 != '\n') c1 = *t1++;	      while (ISSPACE (c2) && c2 != '\n') c2 = *t2++;	    }	  else if (ignore_space_change_flag)	    {	      /* 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'		      && (unsigned char const *) s1 + 1 < t1		      && ISSPACE(t1[-2]))		    {		      --t1;		      continue;		    }		  if (c1 == ' ' && c2 != '\n'		      && (unsigned char const *) s2 + 1 < t2		      && ISSPACE(t2[-2]))		    {		      --t2;		      continue;		    }		}	    }

⌨️ 快捷键说明

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