sdiff.c
来自「WinMerge可以显示两个文件的不同之处」· C语言 代码 · 共 1,196 行 · 第 1/2 页
C
1,196 行
if (! interact_ok)
exiterr ();
if (! (WIFEXITED (wstatus) && WEXITSTATUS (wstatus) < 2))
fatal ("Subsidiary diff failed");
untrapsig (0);
checksigs ();
exit (WEXITSTATUS (wstatus));
}
}
return 0; /* Fool -Wall . . . */
}
static char const **diffargv;
static void
diffarg (a)
char const *a;
{
static unsigned diffargs, diffargsmax;
if (diffargs == diffargsmax)
{
if (! diffargsmax)
{
diffargv = (char const **) xmalloc (sizeof (char));
diffargsmax = 8;
}
diffargsmax *= 2;
diffargv = (char const **) realloc (diffargv,
diffargsmax * sizeof (char const *));
if (! diffargv)
fatal ("out of memory");
}
diffargv[diffargs++] = a;
}
static void
execdiff (differences_only, option, file1, file2)
int differences_only;
char const *option, *file1, *file2;
{
if (differences_only)
diffarg ("--suppress-common-lines");
diffarg (option);
diffarg ("--");
diffarg (file1);
diffarg (file2);
diffarg (0);
execvp (diffbin, (char **) diffargv);
write (STDERR_FILENO, diffbin, strlen (diffbin));
write (STDERR_FILENO, ": not found\n", 12);
_exit (2);
}
/* Signal handling */
#define NUM_SIGS (sizeof (sigs) / sizeof (*sigs))
static int const sigs[] = {
#ifdef SIGHUP
SIGHUP,
#endif
#ifdef SIGQUIT
SIGQUIT,
#endif
#ifdef SIGTERM
SIGTERM,
#endif
#ifdef SIGXCPU
SIGXCPU,
#endif
#ifdef SIGXFSZ
SIGXFSZ,
#endif
SIGINT,
SIGPIPE
};
/* Prefer `sigaction' if it is available, since `signal' can lose signals. */
#if HAVE_SIGACTION
static struct sigaction initial_action[NUM_SIGS];
#define initial_handler(i) (initial_action[i].sa_handler)
#else
static RETSIGTYPE (*initial_action[NUM_SIGS]) ();
#define initial_handler(i) (initial_action[i])
#endif
static int volatile ignore_SIGINT;
static int volatile signal_received;
static int sigs_trapped;
static RETSIGTYPE
catchsig (s)
int s;
{
#if ! HAVE_SIGACTION
signal (s, SIG_IGN);
#endif
if (! (s == SIGINT && ignore_SIGINT))
signal_received = s;
}
static void
trapsigs ()
{
int i;
#if HAVE_SIGACTION
struct sigaction catchaction;
bzero (&catchaction, sizeof (catchaction));
catchaction.sa_handler = catchsig;
#ifdef SA_INTERRUPT
/* Non-Posix BSD-style systems like SunOS 4.1.x need this
so that `read' calls are interrupted properly. */
catchaction.sa_flags = SA_INTERRUPT;
#endif
sigemptyset (&catchaction.sa_mask);
for (i = 0; i < NUM_SIGS; i++)
sigaddset (&catchaction.sa_mask, sigs[i]);
for (i = 0; i < NUM_SIGS; i++)
{
sigaction (sigs[i], 0, &initial_action[i]);
if (initial_handler (i) != SIG_IGN
&& sigaction (sigs[i], &catchaction, 0) != 0)
fatal ("signal error");
}
#else /* ! HAVE_SIGACTION */
for (i = 0; i < NUM_SIGS; i++)
{
initial_action[i] = signal (sigs[i], SIG_IGN);
if (initial_handler (i) != SIG_IGN
&& signal (sigs[i], catchsig) != SIG_IGN)
fatal ("signal error");
}
#endif /* ! HAVE_SIGACTION */
sigs_trapped = 1;
}
/* Untrap signal S, or all trapped signals if S is zero. */
static void
untrapsig (s)
int s;
{
int i;
if (sigs_trapped)
for (i = 0; i < NUM_SIGS; i++)
if ((!s || sigs[i] == s) && initial_handler (i) != SIG_IGN)
#if HAVE_SIGACTION
sigaction (sigs[i], &initial_action[i], 0);
#else
signal (sigs[i], initial_action[i]);
#endif
}
/* Exit if a signal has been received. */
static void
checksigs ()
{
int s = signal_received;
if (s)
{
cleanup ();
/* Yield an exit status indicating that a signal was received. */
untrapsig (s);
kill (getpid (), s);
/* That didn't work, so exit with error status. */
exit (2);
}
}
static void
give_help ()
{
fprintf (stderr,"l:\tuse the left version\n");
fprintf (stderr,"r:\tuse the right version\n");
fprintf (stderr,"e l:\tedit then use the left version\n");
fprintf (stderr,"e r:\tedit then use the right version\n");
fprintf (stderr,"e b:\tedit then use the left and right versions concatenated\n");
fprintf (stderr,"e:\tedit a new version\n");
fprintf (stderr,"s:\tsilently include common lines\n");
fprintf (stderr,"v:\tverbosely include common lines\n");
fprintf (stderr,"q:\tquit\n");
}
static int
skip_white ()
{
int c;
while (isspace (c = getchar ()) && c != '\n')
checksigs ();
if (ferror (stdin))
perror_fatal ("input error");
return c;
}
static void
flush_line ()
{
int c;
while ((c = getchar ()) != '\n' && c != EOF)
;
if (ferror (stdin))
perror_fatal ("input error");
}
/* interpret an edit command */
static int
edit (left, lenl, right, lenr, outfile)
struct line_filter *left;
int lenl;
struct line_filter *right;
int lenr;
FILE *outfile;
{
for (;;)
{
int cmd0, cmd1;
int gotcmd = 0;
cmd1 = 0; /* Pacify `gcc -W'. */
while (!gotcmd)
{
if (putchar ('%') != '%')
perror_fatal ("output error");
ck_fflush (stdout);
cmd0 = skip_white ();
switch (cmd0)
{
case 'l': case 'r': case 's': case 'v': case 'q':
if (skip_white () != '\n')
{
give_help ();
flush_line ();
continue;
}
gotcmd = 1;
break;
case 'e':
cmd1 = skip_white ();
switch (cmd1)
{
case 'l': case 'r': case 'b':
if (skip_white () != '\n')
{
give_help ();
flush_line ();
continue;
}
gotcmd = 1;
break;
case '\n':
gotcmd = 1;
break;
default:
give_help ();
flush_line ();
continue;
}
break;
case EOF:
if (feof (stdin))
{
gotcmd = 1;
cmd0 = 'q';
break;
}
/* falls through */
default:
flush_line ();
/* falls through */
case '\n':
give_help ();
continue;
}
}
switch (cmd0)
{
case 'l':
lf_copy (left, lenl, outfile);
lf_skip (right, lenr);
return 1;
case 'r':
lf_copy (right, lenr, outfile);
lf_skip (left, lenl);
return 1;
case 's':
suppress_common_flag = 1;
break;
case 'v':
suppress_common_flag = 0;
break;
case 'q':
return 0;
case 'e':
if (! tmpname && ! (tmpname = private_tempnam (0, "sdiff", 1, 0)))
perror_fatal ("temporary file name");
tmpmade = 1;
{
FILE *tmp = ck_fopen (tmpname, "w+");
if (cmd1 == 'l' || cmd1 == 'b')
lf_copy (left, lenl, tmp);
else
lf_skip (left, lenl);
if (cmd1 == 'r' || cmd1 == 'b')
lf_copy (right, lenr, tmp);
else
lf_skip (right, lenr);
ck_fflush (tmp);
{
pid_t pid;
int wstatus;
ignore_SIGINT = 1;
checksigs ();
pid = vfork ();
if (pid == 0)
{
char const *argv[3];
int i = 0;
argv[i++] = edbin;
argv[i++] = tmpname;
argv[i++] = 0;
execvp (edbin, (char **) argv);
write (STDERR_FILENO, edbin, strlen (edbin));
write (STDERR_FILENO, ": not found\n", 12);
_exit (1);
}
if (pid < 0)
perror_fatal ("fork failed");
while (waitpid (pid, &wstatus, 0) < 0)
if (errno == EINTR)
checksigs ();
else
perror_fatal ("wait failed");
ignore_SIGINT = 0;
if (! (WIFEXITED (wstatus) && WEXITSTATUS (wstatus) < 1))
fatal ("Subsidiary editor failed");
}
if (fseek (tmp, 0L, SEEK_SET) != 0)
perror_fatal ("fseek");
{
/* SDIFF_BUFSIZE is too big for a local var
in some compilers, so we allocate it dynamically. */
char *buf = xmalloc (SDIFF_BUFSIZE);
size_t size;
while ((size = ck_fread (buf, SDIFF_BUFSIZE, tmp)) != 0)
{
checksigs ();
ck_fwrite (buf, size, outfile);
}
ck_fclose (tmp);
free (buf);
}
return 1;
}
default:
give_help ();
break;
}
}
}
/* Alternately reveal bursts of diff output and handle user commands. */
static int
interact (diff, left, right, outfile)
struct line_filter *diff;
struct line_filter *left;
struct line_filter *right;
FILE *outfile;
{
for (;;)
{
char diff_help[256];
int snarfed = lf_snarf (diff, diff_help, sizeof (diff_help));
if (snarfed <= 0)
return snarfed;
checksigs ();
switch (diff_help[0])
{
case ' ':
puts (diff_help + 1);
break;
case 'i':
{
int lenl = atoi (diff_help + 1), lenr, lenmax;
char *p = strchr (diff_help, ',');
if (!p)
fatal (diff_help);
lenr = atoi (p + 1);
lenmax = max (lenl, lenr);
if (suppress_common_flag)
lf_skip (diff, lenmax);
else
lf_copy (diff, lenmax, stdout);
lf_copy (left, lenl, outfile);
lf_skip (right, lenr);
break;
}
case 'c':
{
int lenl = atoi (diff_help + 1), lenr;
char *p = strchr (diff_help, ',');
if (!p)
fatal (diff_help);
lenr = atoi (p + 1);
lf_copy (diff, max (lenl, lenr), stdout);
if (! edit (left, lenl, right, lenr, outfile))
return 0;
break;
}
default:
fatal (diff_help);
break;
}
}
}
/* temporary lossage: this is torn from gnu libc */
/* Return nonzero if DIR is an existing directory. */
static int
diraccess (dir)
char const *dir;
{
struct stat buf;
return stat (dir, &buf) == 0 && S_ISDIR (buf.st_mode);
}
/* Return nonzero if FILE exists. */
static int
exists (file)
char const *file;
{
struct stat buf;
return stat (file, &buf) == 0;
}
/* These are the characters used in temporary filenames. */
static char const letters[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
/* Generate a temporary filename.
If DIR_SEARCH is nonzero, DIR and PFX are used as
described for tempnam. If not, a temporary filename
in P_tmpdir with no special prefix is generated. If LENPTR
is not 0, *LENPTR is set the to length (including the
terminating '\0') of the resultant filename, which is returned.
This goes through a cyclic pattern of all possible filenames
consisting of five decimal digits of the current pid and three
of the characters in `letters'. Data for tempnam and tmpnam
is kept separate, but when tempnam is using P_tmpdir and no
prefix (i.e, it is identical to tmpnam), the same data is used.
Each potential filename is tested for an already-existing file of
the same name, and no name of an existing file will be returned.
When the cycle reaches its end (12345ZZZ), 0 is returned. */
static char *
private_tempnam (dir, pfx, dir_search, lenptr)
char const *dir;
char const *pfx;
int dir_search;
size_t *lenptr;
{
static char const tmpdir[] = PVT_tmpdir;
static struct
{
char buf[3];
char *s;
size_t i;
} infos[2], *info;
static char *buf;
static size_t bufsize = 1;
static pid_t oldpid = 0;
pid_t pid = getpid ();
register size_t len, plen;
if (dir_search)
{
register char const *d = getenv ("TMPDIR");
if (d && !diraccess (d))
d = 0;
if (!d && dir && diraccess (dir))
d = dir;
if (!d && diraccess (tmpdir))
d = tmpdir;
if (!d && diraccess ("/tmp"))
d = "/tmp";
if (!d)
{
errno = ENOENT;
return 0;
}
dir = d;
}
else
dir = tmpdir;
if (pfx && *pfx)
{
plen = strlen (pfx);
if (plen > 5)
plen = 5;
}
else
plen = 0;
if (dir != tmpdir && !stricmp (dir, tmpdir))
dir = tmpdir;
info = &infos[(plen == 0 && dir == tmpdir) ? 1 : 0];
if (pid != oldpid)
{
oldpid = pid;
info->buf[0] = info->buf[1] = info->buf[2] = '0';
info->s = &info->buf[0];
info->i = 0;
}
len = strlen (dir) + 1 + plen + 8;
if (bufsize <= len)
{
do
{
bufsize *= 2;
}
while (bufsize <= len);
if (buf)
free (buf);
buf = xmalloc (bufsize);
}
for (;;)
{
*info->s = letters[info->i];
sprintf (buf, "%s/%.*s%.5lu%.3s", dir, (int) plen, pfx,
(unsigned long) pid % 100000, info->buf);
if (!exists (buf))
break;
++info->i;
if (info->i > sizeof (letters) - 1)
{
info->i = 0;
if (info->s == &info->buf[2])
{
errno = EEXIST;
return 0;
}
++info->s;
}
}
if (lenptr)
*lenptr = len;
return buf;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?