mirror of
https://github.com/sheumann/hush.git
synced 2024-12-21 23:29:34 +00:00
diff: shrink
function old new delta check 678 1607 +929 files_differ - 175 +175 do_diff 436 433 -3 asciifile 94 90 -4 print_only 23 16 -7 diff_main 868 842 -26 prepare 339 301 -38 print_status 316 178 -138 diffreg 2993 1818 -1175 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/7 up/down: 1104/-1391) Total: -287 bytes
This commit is contained in:
parent
c693840fe8
commit
dc1cbf839d
166
editors/diff.c
166
editors/diff.c
@ -14,7 +14,10 @@
|
|||||||
|
|
||||||
#include "libbb.h"
|
#include "libbb.h"
|
||||||
|
|
||||||
#define FSIZE_MAX 32768
|
// #define FSIZE_MAX 32768
|
||||||
|
|
||||||
|
/* NOINLINEs added to prevent gcc from merging too much into diffreg()
|
||||||
|
* (it bites more than it can (efficiently) chew). */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Output flags
|
* Output flags
|
||||||
@ -37,12 +40,11 @@
|
|||||||
* D_SKIPPED1 - skipped path1 as it is a special file
|
* D_SKIPPED1 - skipped path1 as it is a special file
|
||||||
* D_SKIPPED2 - skipped path2 as it is a special file
|
* D_SKIPPED2 - skipped path2 as it is a special file
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define D_SAME 0
|
#define D_SAME 0
|
||||||
#define D_DIFFER (1 << 0)
|
#define D_DIFFER (1 << 0)
|
||||||
#define D_BINARY (1 << 1)
|
#define D_BINARY (1 << 1)
|
||||||
#define D_COMMON (1 << 2)
|
#define D_COMMON (1 << 2)
|
||||||
#define D_ONLY (1<<3)
|
/*#define D_ONLY (1 << 3) - unused */
|
||||||
#define D_MISMATCH1 (1 << 4)
|
#define D_MISMATCH1 (1 << 4)
|
||||||
#define D_MISMATCH2 (1 << 5)
|
#define D_MISMATCH2 (1 << 5)
|
||||||
#define D_ERROR (1 << 6)
|
#define D_ERROR (1 << 6)
|
||||||
@ -65,7 +67,6 @@
|
|||||||
#define FLAG_U (1 << 12)
|
#define FLAG_U (1 << 12)
|
||||||
#define FLAG_w (1 << 13)
|
#define FLAG_w (1 << 13)
|
||||||
|
|
||||||
#define g_read_buf bb_common_bufsiz1
|
|
||||||
|
|
||||||
struct cand {
|
struct cand {
|
||||||
int x;
|
int x;
|
||||||
@ -90,13 +91,16 @@ struct context_vec {
|
|||||||
int d; /* end line in new file */
|
int d; /* end line in new file */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define g_read_buf bb_common_bufsiz1
|
||||||
|
|
||||||
struct globals {
|
struct globals {
|
||||||
USE_FEATURE_DIFF_DIR(char **dl;)
|
USE_FEATURE_DIFF_DIR(char **dl;)
|
||||||
USE_FEATURE_DIFF_DIR(int dl_count;)
|
USE_FEATURE_DIFF_DIR(int dl_count;)
|
||||||
|
int status;
|
||||||
/* This is the default number of lines of context. */
|
/* This is the default number of lines of context. */
|
||||||
int context;
|
int context;
|
||||||
size_t max_context;
|
size_t max_context;
|
||||||
int status;
|
|
||||||
char *start;
|
char *start;
|
||||||
const char *label1;
|
const char *label1;
|
||||||
const char *label2;
|
const char *label2;
|
||||||
@ -157,23 +161,25 @@ struct globals {
|
|||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
static void print_only(const char *path, size_t dirlen, const char *entry)
|
/*static void print_only(const char *path, size_t dirlen, const char *entry)*/
|
||||||
|
static void print_only(const char *path, const char *entry)
|
||||||
{
|
{
|
||||||
if (dirlen > 1)
|
printf("Only in %s: %s\n", path, entry);
|
||||||
dirlen--;
|
|
||||||
printf("Only in %.*s: %s\n", (int) dirlen, path, entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_status(int val, char *path1, char *path2, char *entry)
|
|
||||||
|
/*static void print_status(int val, char *path1, char *path2, char *entry)*/
|
||||||
|
static void print_status(int val, char *_path1, char *_path2)
|
||||||
{
|
{
|
||||||
const char *const _entry = entry ? entry : "";
|
/*const char *const _entry = entry ? entry : "";*/
|
||||||
char * const _path1 = entry ? concat_path_file(path1, _entry) : path1;
|
/*char *const _path1 = entry ? concat_path_file(path1, _entry) : path1;*/
|
||||||
char * const _path2 = entry ? concat_path_file(path2, _entry) : path2;
|
/*char *const _path2 = entry ? concat_path_file(path2, _entry) : path2;*/
|
||||||
|
|
||||||
switch (val) {
|
switch (val) {
|
||||||
case D_ONLY:
|
/* case D_ONLY:
|
||||||
print_only(path1, strlen(path1), entry);
|
print_only(path1, entry);
|
||||||
break;
|
break;
|
||||||
|
*/
|
||||||
case D_COMMON:
|
case D_COMMON:
|
||||||
printf("Common subdirectories: %s and %s\n", _path1, _path2);
|
printf("Common subdirectories: %s and %s\n", _path1, _path2);
|
||||||
break;
|
break;
|
||||||
@ -205,18 +211,23 @@ static void print_status(int val, char *path1, char *path2, char *entry)
|
|||||||
_path2);
|
_path2);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
if (entry) {
|
if (entry) {
|
||||||
free(_path1);
|
free(_path1);
|
||||||
free(_path2);
|
free(_path2);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Read line, return its nonzero hash. Return 0 if EOF.
|
||||||
|
*
|
||||||
|
* Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
|
||||||
|
*/
|
||||||
static ALWAYS_INLINE int fiddle_sum(int sum, int t)
|
static ALWAYS_INLINE int fiddle_sum(int sum, int t)
|
||||||
{
|
{
|
||||||
return sum * 127 + t;
|
return sum * 127 + t;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
* Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
|
|
||||||
*/
|
|
||||||
static int readhash(FILE *fp)
|
static int readhash(FILE *fp)
|
||||||
{
|
{
|
||||||
int i, t, space;
|
int i, t, space;
|
||||||
@ -224,32 +235,34 @@ static int readhash(FILE *fp)
|
|||||||
|
|
||||||
sum = 1;
|
sum = 1;
|
||||||
space = 0;
|
space = 0;
|
||||||
|
i = 0;
|
||||||
if (!(option_mask32 & (FLAG_b | FLAG_w))) {
|
if (!(option_mask32 & (FLAG_b | FLAG_w))) {
|
||||||
for (i = 0; (t = getc(fp)) != '\n'; i++) {
|
while ((t = getc(fp)) != '\n') {
|
||||||
if (t == EOF) {
|
if (t == EOF) {
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
sum = fiddle_sum(sum, t);
|
sum = fiddle_sum(sum, t);
|
||||||
|
i = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (i = 0;;) {
|
while (1) {
|
||||||
switch (t = getc(fp)) {
|
switch (t = getc(fp)) {
|
||||||
case '\t':
|
case '\t':
|
||||||
case '\r':
|
case '\r':
|
||||||
case '\v':
|
case '\v':
|
||||||
case '\f':
|
case '\f':
|
||||||
case ' ':
|
case ' ':
|
||||||
space++;
|
space = 1;
|
||||||
continue;
|
continue;
|
||||||
default:
|
default:
|
||||||
if (space && !(option_mask32 & FLAG_w)) {
|
if (space && !(option_mask32 & FLAG_w)) {
|
||||||
i++;
|
i = 1;
|
||||||
space = 0;
|
space = 0;
|
||||||
}
|
}
|
||||||
sum = fiddle_sum(sum, t);
|
sum = fiddle_sum(sum, t);
|
||||||
i++;
|
i = 1;
|
||||||
continue;
|
continue;
|
||||||
case EOF:
|
case EOF:
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
@ -273,7 +286,7 @@ static int readhash(FILE *fp)
|
|||||||
* Check to see if the given files differ.
|
* Check to see if the given files differ.
|
||||||
* Returns 0 if they are the same, 1 if different, and -1 on error.
|
* Returns 0 if they are the same, 1 if different, and -1 on error.
|
||||||
*/
|
*/
|
||||||
static int files_differ(FILE *f1, FILE *f2, int flags)
|
static NOINLINE int files_differ(FILE *f1, FILE *f2, int flags)
|
||||||
{
|
{
|
||||||
size_t i, j;
|
size_t i, j;
|
||||||
|
|
||||||
@ -288,7 +301,7 @@ static int files_differ(FILE *f1, FILE *f2, int flags)
|
|||||||
if (i != j)
|
if (i != j)
|
||||||
return 1;
|
return 1;
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
return (ferror(f1) || ferror(f2));
|
return (ferror(f1) || ferror(f2)) ? -1 : 0;
|
||||||
if (memcmp(g_read_buf,
|
if (memcmp(g_read_buf,
|
||||||
g_read_buf + COMMON_BUFSIZE/2, i) != 0)
|
g_read_buf + COMMON_BUFSIZE/2, i) != 0)
|
||||||
return 1;
|
return 1;
|
||||||
@ -310,7 +323,7 @@ static void prepare(int i, FILE *fp /*, off_t filesize*/)
|
|||||||
|
|
||||||
p = xmalloc((sz + 3) * sizeof(p[0]));
|
p = xmalloc((sz + 3) * sizeof(p[0]));
|
||||||
j = 0;
|
j = 0;
|
||||||
while ((h = readhash(fp))) {
|
while ((h = readhash(fp)) != 0) { /* while not EOF */
|
||||||
if (j == sz) {
|
if (j == sz) {
|
||||||
sz = sz * 3 / 2;
|
sz = sz * 3 / 2;
|
||||||
p = xrealloc(p, (sz + 3) * sizeof(p[0]));
|
p = xrealloc(p, (sz + 3) * sizeof(p[0]));
|
||||||
@ -433,13 +446,13 @@ static int stone(int *a, int n, int *b, int *c)
|
|||||||
int i, k, y, j, l;
|
int i, k, y, j, l;
|
||||||
int oldc, tc, oldl;
|
int oldc, tc, oldl;
|
||||||
unsigned int numtries;
|
unsigned int numtries;
|
||||||
|
|
||||||
#if ENABLE_FEATURE_DIFF_MINIMAL
|
#if ENABLE_FEATURE_DIFF_MINIMAL
|
||||||
const unsigned int bound =
|
const unsigned int bound =
|
||||||
(option_mask32 & FLAG_d) ? UINT_MAX : MAX(256, isqrt(n));
|
(option_mask32 & FLAG_d) ? UINT_MAX : MAX(256, isqrt(n));
|
||||||
#else
|
#else
|
||||||
const unsigned int bound = MAX(256, isqrt(n));
|
const unsigned int bound = MAX(256, isqrt(n));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
k = 0;
|
k = 0;
|
||||||
c[0] = newcand(0, 0, 0);
|
c[0] = newcand(0, 0, 0);
|
||||||
for (i = 1; i <= n; i++) {
|
for (i = 1; i <= n; i++) {
|
||||||
@ -516,7 +529,7 @@ static int skipline(FILE * f)
|
|||||||
* to confounding by hashing (which result in "jackpot")
|
* to confounding by hashing (which result in "jackpot")
|
||||||
* 2. collect random access indexes to the two files
|
* 2. collect random access indexes to the two files
|
||||||
*/
|
*/
|
||||||
static void check(FILE * f1, FILE * f2)
|
static NOINLINE void check(FILE *f1, FILE *f2)
|
||||||
{
|
{
|
||||||
int i, j, jackpot, c, d;
|
int i, j, jackpot, c, d;
|
||||||
long ctold, ctnew;
|
long ctold, ctnew;
|
||||||
@ -536,8 +549,7 @@ static void check(FILE * f1, FILE * f2)
|
|||||||
ixnew[j] = ctnew += skipline(f2);
|
ixnew[j] = ctnew += skipline(f2);
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
if ((option_mask32 & FLAG_b) || (option_mask32 & FLAG_w)
|
if (option_mask32 & (FLAG_b | FLAG_w | FLAG_i)) {
|
||||||
|| (option_mask32 & FLAG_i)) {
|
|
||||||
while (1) {
|
while (1) {
|
||||||
c = getc(f1);
|
c = getc(f1);
|
||||||
d = getc(f2);
|
d = getc(f2);
|
||||||
@ -545,8 +557,9 @@ static void check(FILE * f1, FILE * f2)
|
|||||||
* GNU diff ignores a missing newline
|
* GNU diff ignores a missing newline
|
||||||
* in one file if bflag || wflag.
|
* in one file if bflag || wflag.
|
||||||
*/
|
*/
|
||||||
if (((option_mask32 & FLAG_b) || (option_mask32 & FLAG_w)) &&
|
if ((option_mask32 & (FLAG_b | FLAG_w))
|
||||||
((c == EOF && d == '\n') || (c == '\n' && d == EOF))) {
|
&& ((c == EOF && d == '\n') || (c == '\n' && d == EOF))
|
||||||
|
) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ctold++;
|
ctold++;
|
||||||
@ -556,12 +569,14 @@ static void check(FILE * f1, FILE * f2)
|
|||||||
if (c == '\n')
|
if (c == '\n')
|
||||||
break;
|
break;
|
||||||
ctold++;
|
ctold++;
|
||||||
} while (isspace(c = getc(f1)));
|
c = getc(f1);
|
||||||
|
} while (isspace(c));
|
||||||
do {
|
do {
|
||||||
if (d == '\n')
|
if (d == '\n')
|
||||||
break;
|
break;
|
||||||
ctnew++;
|
ctnew++;
|
||||||
} while (isspace(d = getc(f2)));
|
d = getc(f2);
|
||||||
|
} while (isspace(d));
|
||||||
} else if (option_mask32 & FLAG_w) {
|
} else if (option_mask32 & FLAG_w) {
|
||||||
while (isspace(c) && c != '\n') {
|
while (isspace(c) && c != '\n') {
|
||||||
c = getc(f1);
|
c = getc(f1);
|
||||||
@ -594,6 +609,7 @@ static void check(FILE * f1, FILE * f2)
|
|||||||
J[i] = 0;
|
J[i] = 0;
|
||||||
if (c != '\n' && c != EOF)
|
if (c != '\n' && c != EOF)
|
||||||
ctold += skipline(f1);
|
ctold += skipline(f1);
|
||||||
|
// BUG? Should be "if (d != '\n' && d != EOF)" ?
|
||||||
if (d != '\n' && c != EOF)
|
if (d != '\n' && c != EOF)
|
||||||
ctnew += skipline(f2);
|
ctnew += skipline(f2);
|
||||||
break;
|
break;
|
||||||
@ -628,9 +644,11 @@ static void sort(struct line *a, int n)
|
|||||||
aim = &ai[m];
|
aim = &ai[m];
|
||||||
if (aim < ai)
|
if (aim < ai)
|
||||||
break; /* wraparound */
|
break; /* wraparound */
|
||||||
if (aim->value > ai[0].value ||
|
if (aim->value > ai[0].value
|
||||||
(aim->value == ai[0].value && aim->serial > ai[0].serial))
|
|| (aim->value == ai[0].value && aim->serial > ai[0].serial)
|
||||||
|
) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
w.value = ai[0].value;
|
w.value = ai[0].value;
|
||||||
ai[0].value = aim->value;
|
ai[0].value = aim->value;
|
||||||
aim->value = w.value;
|
aim->value = w.value;
|
||||||
@ -688,27 +706,27 @@ static void fetch(long *f, int a, int b, FILE * lb, int ch)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if ENABLE_FEATURE_DIFF_BINARY
|
||||||
static int asciifile(FILE *f)
|
static int asciifile(FILE *f)
|
||||||
{
|
{
|
||||||
#if ENABLE_FEATURE_DIFF_BINARY
|
|
||||||
int i, cnt;
|
int i, cnt;
|
||||||
#endif
|
|
||||||
|
|
||||||
if ((option_mask32 & FLAG_a) || f == NULL)
|
if (option_mask32 & FLAG_a)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
#if ENABLE_FEATURE_DIFF_BINARY
|
|
||||||
rewind(f);
|
rewind(f);
|
||||||
cnt = fread(g_read_buf, 1, COMMON_BUFSIZE, f);
|
cnt = fread(g_read_buf, 1, COMMON_BUFSIZE, f);
|
||||||
for (i = 0; i < cnt; i++) {
|
for (i = 0; i < cnt; i++) {
|
||||||
if (!isprint(g_read_buf[i])
|
if (!isprint(g_read_buf[i])
|
||||||
&& !isspace(g_read_buf[i])) {
|
&& !isspace(g_read_buf[i])
|
||||||
|
) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
#define asciifile(f) 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* dump accumulated "unified" diff changes */
|
/* dump accumulated "unified" diff changes */
|
||||||
@ -756,6 +774,7 @@ static void dump_unified_vec(FILE * f1, FILE * f2)
|
|||||||
#if 0
|
#if 0
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'c':
|
case 'c':
|
||||||
|
// fetch() seeks!
|
||||||
fetch(ixold, lowa, a - 1, f1, ' ');
|
fetch(ixold, lowa, a - 1, f1, ' ');
|
||||||
fetch(ixold, a, b, f1, '-');
|
fetch(ixold, a, b, f1, '-');
|
||||||
fetch(ixnew, c, d, f2, '+');
|
fetch(ixnew, c, d, f2, '+');
|
||||||
@ -808,8 +827,8 @@ static void print_header(const char *file1, const char *file2)
|
|||||||
* lines appended (beginning at b). If c is greater than d then there are
|
* lines appended (beginning at b). If c is greater than d then there are
|
||||||
* lines missing from the to file.
|
* lines missing from the to file.
|
||||||
*/
|
*/
|
||||||
static void change(char *file1, FILE * f1, char *file2, FILE * f2, int a,
|
static void change(char *file1, FILE *f1, char *file2, FILE *f2,
|
||||||
int b, int c, int d)
|
int a, int b, int c, int d)
|
||||||
{
|
{
|
||||||
if ((a > b && c > d) || (option_mask32 & FLAG_q)) {
|
if ((a > b && c > d) || (option_mask32 & FLAG_q)) {
|
||||||
anychange = 1;
|
anychange = 1;
|
||||||
@ -833,12 +852,14 @@ static void change(char *file1, FILE * f1, char *file2, FILE * f2, int a,
|
|||||||
* Print the context/unidiff header first time through.
|
* Print the context/unidiff header first time through.
|
||||||
*/
|
*/
|
||||||
print_header(file1, file2);
|
print_header(file1, file2);
|
||||||
} else if (a > context_vec_ptr->b + (2 * context) + 1 &&
|
} else if (a > context_vec_ptr->b + (2 * context) + 1
|
||||||
c > context_vec_ptr->d + (2 * context) + 1) {
|
&& c > context_vec_ptr->d + (2 * context) + 1
|
||||||
|
) {
|
||||||
/*
|
/*
|
||||||
* If this change is more than 'context' lines from the
|
* If this change is more than 'context' lines from the
|
||||||
* previous change, dump the record and reset it.
|
* previous change, dump the record and reset it.
|
||||||
*/
|
*/
|
||||||
|
// dump_unified_vec() seeks!
|
||||||
dump_unified_vec(f1, f2);
|
dump_unified_vec(f1, f2);
|
||||||
}
|
}
|
||||||
context_vec_ptr++;
|
context_vec_ptr++;
|
||||||
@ -870,12 +891,15 @@ static void output(char *file1, FILE * f1, char *file2, FILE * f2)
|
|||||||
i1++;
|
i1++;
|
||||||
j01 = J[i1 + 1] - 1;
|
j01 = J[i1 + 1] - 1;
|
||||||
J[i1] = j01;
|
J[i1] = j01;
|
||||||
|
// change() seeks!
|
||||||
change(file1, f1, file2, f2, i0, i1, j00, j01);
|
change(file1, f1, file2, f2, i0, i1, j00, j01);
|
||||||
}
|
}
|
||||||
if (m == 0) {
|
if (m == 0) {
|
||||||
|
// change() seeks!
|
||||||
change(file1, f1, file2, f2, 1, 0, 1, len[1]);
|
change(file1, f1, file2, f2, 1, 0, 1, len[1]);
|
||||||
}
|
}
|
||||||
if (anychange != 0 && !(option_mask32 & FLAG_q)) {
|
if (anychange != 0 && !(option_mask32 & FLAG_q)) {
|
||||||
|
// dump_unified_vec() seeks!
|
||||||
dump_unified_vec(f1, f2);
|
dump_unified_vec(f1, f2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -887,12 +911,12 @@ static void output(char *file1, FILE * f1, char *file2, FILE * f2)
|
|||||||
*
|
*
|
||||||
* The major goal is to generate the match vector J.
|
* The major goal is to generate the match vector J.
|
||||||
* J[i] is the index of the line in file1 corresponding
|
* J[i] is the index of the line in file1 corresponding
|
||||||
* to line i file0. J[i] = 0 if there is no
|
* to line i in file0. J[i] = 0 if there is no
|
||||||
* such line in file1.
|
* such line in file1.
|
||||||
*
|
*
|
||||||
* Lines are hashed so as to work in core. All potential
|
* Lines are hashed so as to work in core. All potential
|
||||||
* matches are located by sorting the lines of each file
|
* matches are located by sorting the lines of each file
|
||||||
* on the hash (called ``value''). In particular, this
|
* on the hash (called "value"). In particular, this
|
||||||
* collects the equivalence classes in file1 together.
|
* collects the equivalence classes in file1 together.
|
||||||
* Subroutine equiv replaces the value of each line in
|
* Subroutine equiv replaces the value of each line in
|
||||||
* file0 by the index of the first element of its
|
* file0 by the index of the first element of its
|
||||||
@ -908,7 +932,7 @@ static void output(char *file1, FILE * f1, char *file2, FILE * f2)
|
|||||||
* The cleverness lies in routine stone. This marches
|
* The cleverness lies in routine stone. This marches
|
||||||
* through the lines of file0, developing a vector klist
|
* through the lines of file0, developing a vector klist
|
||||||
* of "k-candidates". At step i a k-candidate is a matched
|
* of "k-candidates". At step i a k-candidate is a matched
|
||||||
* pair of lines x,y (x in file0 y in file1) such that
|
* pair of lines x,y (x in file0, y in file1) such that
|
||||||
* there is a common subsequence of length k
|
* there is a common subsequence of length k
|
||||||
* between the first i lines of file0 and the first y
|
* between the first i lines of file0 and the first y
|
||||||
* lines of file1, but there is no such subsequence for
|
* lines of file1, but there is no such subsequence for
|
||||||
@ -942,11 +966,10 @@ static void output(char *file1, FILE * f1, char *file2, FILE * f2)
|
|||||||
* 3*(number of k-candidates installed), typically about
|
* 3*(number of k-candidates installed), typically about
|
||||||
* 6n words for files of length n.
|
* 6n words for files of length n.
|
||||||
*/
|
*/
|
||||||
static unsigned diffreg(char *ofile1, char *ofile2, int flags)
|
static unsigned diffreg(char *file1, char *file2, int flags)
|
||||||
{
|
{
|
||||||
char *file1 = ofile1;
|
FILE *f1;
|
||||||
char *file2 = ofile2;
|
FILE *f2;
|
||||||
FILE *f1 = stdin, *f2 = stdin;
|
|
||||||
unsigned rval;
|
unsigned rval;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@ -956,19 +979,19 @@ static unsigned diffreg(char *ofile1, char *ofile2, int flags)
|
|||||||
if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
|
if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
|
||||||
return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
|
return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
|
||||||
|
|
||||||
rval = D_SAME;
|
|
||||||
|
|
||||||
if (LONE_DASH(file1) && LONE_DASH(file2))
|
if (LONE_DASH(file1) && LONE_DASH(file2))
|
||||||
goto closem;
|
return D_SAME;
|
||||||
|
|
||||||
|
rval = D_SAME;
|
||||||
|
|
||||||
if (flags & D_EMPTY1)
|
if (flags & D_EMPTY1)
|
||||||
f1 = xfopen(bb_dev_null, "r");
|
f1 = xfopen(bb_dev_null, "r");
|
||||||
else if (NOT_LONE_DASH(file1))
|
else
|
||||||
f1 = xfopen(file1, "r");
|
f1 = xfopen_stdin(file1);
|
||||||
if (flags & D_EMPTY2)
|
if (flags & D_EMPTY2)
|
||||||
f2 = xfopen(bb_dev_null, "r");
|
f2 = xfopen(bb_dev_null, "r");
|
||||||
else if (NOT_LONE_DASH(file2))
|
else
|
||||||
f2 = xfopen(file2, "r");
|
f2 = xfopen_stdin(file2);
|
||||||
|
|
||||||
/* We can't diff non-seekable stream - we use rewind(), fseek().
|
/* We can't diff non-seekable stream - we use rewind(), fseek().
|
||||||
* This can be fixed (volunteers?).
|
* This can be fixed (volunteers?).
|
||||||
@ -977,6 +1000,7 @@ static unsigned diffreg(char *ofile1, char *ofile2, int flags)
|
|||||||
* Check in main won't catch "diffing fifos buried in subdirectories"
|
* Check in main won't catch "diffing fifos buried in subdirectories"
|
||||||
* failure scenario - not very likely in real life... */
|
* failure scenario - not very likely in real life... */
|
||||||
|
|
||||||
|
/* Quick check whether they are different */
|
||||||
i = files_differ(f1, f2, flags);
|
i = files_differ(f1, f2, flags);
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
goto closem;
|
goto closem;
|
||||||
@ -992,6 +1016,7 @@ static unsigned diffreg(char *ofile1, char *ofile2, int flags)
|
|||||||
goto closem;
|
goto closem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rewind inside!
|
||||||
prepare(0, f1 /*, stb1.st_size*/);
|
prepare(0, f1 /*, stb1.st_size*/);
|
||||||
prepare(1, f2 /*, stb2.st_size*/);
|
prepare(1, f2 /*, stb2.st_size*/);
|
||||||
prune();
|
prune();
|
||||||
@ -1021,7 +1046,9 @@ static unsigned diffreg(char *ofile1, char *ofile2, int flags)
|
|||||||
|
|
||||||
ixold = xrealloc(ixold, (len[0] + 2) * sizeof(long));
|
ixold = xrealloc(ixold, (len[0] + 2) * sizeof(long));
|
||||||
ixnew = xrealloc(ixnew, (len[1] + 2) * sizeof(long));
|
ixnew = xrealloc(ixnew, (len[1] + 2) * sizeof(long));
|
||||||
|
// Rewind inside!
|
||||||
check(f1, f2);
|
check(f1, f2);
|
||||||
|
// Rewind inside!
|
||||||
output(file1, f1, file2, f2);
|
output(file1, f1, file2, f2);
|
||||||
|
|
||||||
closem:
|
closem:
|
||||||
@ -1032,10 +1059,6 @@ static unsigned diffreg(char *ofile1, char *ofile2, int flags)
|
|||||||
}
|
}
|
||||||
fclose_if_not_stdin(f1);
|
fclose_if_not_stdin(f1);
|
||||||
fclose_if_not_stdin(f2);
|
fclose_if_not_stdin(f2);
|
||||||
if (file1 != ofile1)
|
|
||||||
free(file1);
|
|
||||||
if (file2 != ofile2)
|
|
||||||
free(file2);
|
|
||||||
return rval;
|
return rval;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1086,7 +1109,7 @@ static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
|
|||||||
else
|
else
|
||||||
val = diffreg(fullpath1, fullpath2, flags);
|
val = diffreg(fullpath1, fullpath2, flags);
|
||||||
|
|
||||||
print_status(val, fullpath1, fullpath2, NULL);
|
print_status(val, fullpath1, fullpath2 /*, NULL*/);
|
||||||
ret:
|
ret:
|
||||||
free(fullpath1);
|
free(fullpath1);
|
||||||
free(fullpath2);
|
free(fullpath2);
|
||||||
@ -1097,7 +1120,8 @@ static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
|
|||||||
#if ENABLE_FEATURE_DIFF_DIR
|
#if ENABLE_FEATURE_DIFF_DIR
|
||||||
/* This function adds a filename to dl, the directory listing. */
|
/* This function adds a filename to dl, the directory listing. */
|
||||||
static int add_to_dirlist(const char *filename,
|
static int add_to_dirlist(const char *filename,
|
||||||
struct stat ATTRIBUTE_UNUSED * sb, void *userdata,
|
struct stat ATTRIBUTE_UNUSED *sb,
|
||||||
|
void *userdata,
|
||||||
int depth ATTRIBUTE_UNUSED)
|
int depth ATTRIBUTE_UNUSED)
|
||||||
{
|
{
|
||||||
/* +2: with space for eventual trailing NULL */
|
/* +2: with space for eventual trailing NULL */
|
||||||
@ -1160,7 +1184,6 @@ static void diffdir(char *p1, char *p2)
|
|||||||
*dp2 = '\0';
|
*dp2 = '\0';
|
||||||
|
|
||||||
/* Get directory listings for p1 and p2. */
|
/* Get directory listings for p1 and p2. */
|
||||||
|
|
||||||
dirlist1 = get_dir(p1);
|
dirlist1 = get_dir(p1);
|
||||||
dirlist2 = get_dir(p2);
|
dirlist2 = get_dir(p2);
|
||||||
|
|
||||||
@ -1190,13 +1213,13 @@ static void diffdir(char *p1, char *p2)
|
|||||||
if (option_mask32 & FLAG_N)
|
if (option_mask32 & FLAG_N)
|
||||||
do_diff(p1, dp1, p2, NULL);
|
do_diff(p1, dp1, p2, NULL);
|
||||||
else
|
else
|
||||||
print_only(p1, strlen(p1) + 1, dp1);
|
print_only(p1, dp1);
|
||||||
dirlist1++;
|
dirlist1++;
|
||||||
} else {
|
} else {
|
||||||
if (option_mask32 & FLAG_N)
|
if (option_mask32 & FLAG_N)
|
||||||
do_diff(p1, NULL, p2, dp2);
|
do_diff(p1, NULL, p2, dp2);
|
||||||
else
|
else
|
||||||
print_only(p2, strlen(p2) + 1, dp2);
|
print_only(p2, dp2);
|
||||||
dirlist2++;
|
dirlist2++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1237,7 +1260,6 @@ int diff_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
* Do sanity checks, fill in stb1 and stb2 and call the appropriate
|
* Do sanity checks, fill in stb1 and stb2 and call the appropriate
|
||||||
* driver routine. Both drivers use the contents of stb1 and stb2.
|
* driver routine. Both drivers use the contents of stb1 and stb2.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
f1 = argv[0];
|
f1 = argv[0];
|
||||||
f2 = argv[1];
|
f2 = argv[1];
|
||||||
if (LONE_DASH(f1)) {
|
if (LONE_DASH(f1)) {
|
||||||
@ -1272,7 +1294,7 @@ int diff_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
* This can be fixed (volunteers?) */
|
* This can be fixed (volunteers?) */
|
||||||
if (!S_ISREG(stb1.st_mode) || !S_ISREG(stb2.st_mode))
|
if (!S_ISREG(stb1.st_mode) || !S_ISREG(stb2.st_mode))
|
||||||
bb_error_msg_and_die("can't diff non-seekable stream");
|
bb_error_msg_and_die("can't diff non-seekable stream");
|
||||||
print_status(diffreg(f1, f2, 0), f1, f2, NULL);
|
print_status(diffreg(f1, f2, 0), f1, f2 /*, NULL*/);
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user