annotate toys/pending/diff.c @ 1539:3e85af1f7e22 draft

First batch of sed tests. Only good for TEST_HOST=1 at the moment because the test infrastructure itself depends on sed, so if an unfinished sed is in the $PATH it goes boing. But hey, corner cases! I have... more.
author Rob Landley <rob@landley.net>
date Wed, 29 Oct 2014 18:44:33 -0500
parents e22e9eee9346
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
1 /* diff.c - compare files line by line
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
2 *
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
3 * Copyright 2014 Sandeep Sharma <sandeep.jack2756@gmail.com>
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Ashwini Kumar <ak.ashwini1981@gmail.com>
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
5 *
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
6 * See: http://cm.bell-labs.com/cm/cs/cstr/41.pdf
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
7
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
8 USE_DIFF(NEWTOY(diff, "<2>2B(ignore-blank-lines)d(minimal)b(ignore-space-change)ut(expand-tabs)w(ignore-all-space)i(ignore-case)T(initial-tab)s(report-identical-files)q(brief)a(text)L(label)*S(starting-file):N(new-file)r(recursive)U(unified)#<0=3", TOYFLAG_USR|TOYFLAG_BIN))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
9
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
10 config DIFF
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
11 bool "diff"
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
12 default n
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
13 help
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
14 usage: diff [-abBdiNqrTstw] [-L LABEL] [-S FILE] [-U LINES] FILE1 FILE2
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
15
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
16 -a Treat all files as text
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
17 -b Ignore changes in the amount of whitespace
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
18 -B Ignore changes whose lines are all blank
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
19 -d Try hard to find a smaller set of changes
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
20 -i Ignore case differences
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
21 -L Use LABEL instead of the filename in the unified header
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
22 -N Treat absent files as empty
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
23 -q Output only whether files differ
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
24 -r Recurse
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
25 -S Start with FILE when comparing directories
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
26 -T Make tabs line up by prefixing a tab when necessary
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
27 -s Report when two files are the same
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
28 -t Expand tabs to spaces in output
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
29 -U Output LINES lines of context
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
30 -w Ignore all whitespace
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
31 */
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
32
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
33 #define FOR_diff
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
34 #include "toys.h"
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
35
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
36 GLOBALS(
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
37 long ct;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
38 char *start;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
39 struct arg_list *L_list;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
40
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
41 int dir_num, size, is_binary, status, change, len[2];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
42 int *offset[2];
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
43 )
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
44
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
45 #define MIN(x,y) ((x) < (y) ? (x) : (y))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
46 #define MAX(x,y) ((x) > (y) ? (x) : (y))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
47 #define IS_STDIN(s) ((s)[0] == '-' && !(s)[1])
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
48
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
49 struct v_vector {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
50 unsigned serial:31;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
51 unsigned last:1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
52 union {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
53 unsigned hash;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
54 unsigned p;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
55 };
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
56 };
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
57
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
58 struct diff {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
59 long a, b, c, d, prev, suff;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
60 };
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
61
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
62 static struct dir {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
63 char **list;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
64 int nr_elm;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
65 } dir[2];
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
66
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
67 struct candidate {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
68 int a, b;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
69 struct candidate *prev, *next;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
70 };
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
71
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
72 static struct file {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
73 FILE *fp;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
74 int len;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
75 } file[2];
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
76
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
77 enum {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
78 SAME,
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
79 DIFFER,
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
80 };
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
81
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
82 enum {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
83 empty = 1 << 9,
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
84 eol = 1 << 10,
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
85 eof = 1 << 11,
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
86 space = 1 << 12
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
87 };
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
88
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
89 static int comp(const void *a, const void* b)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
90 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
91 int i = ((struct v_vector *)a)->hash -
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
92 ((struct v_vector *)b)->hash;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
93
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
94 if (!i) i = ((struct v_vector *)a)->serial -
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
95 ((struct v_vector *)b)->serial;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
96 return i;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
97 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
98
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
99 static int search (struct candidate **K, int r, int k, int j)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
100 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
101 int low = r, upper = k, mid;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
102
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
103 mid = (low + upper) / 2;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
104 while (low <= mid) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
105 if (((struct candidate*)(K[mid]))->b < j &&
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
106 ((struct candidate*)(K[mid + 1]))->b > j)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
107 return mid;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
108
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
109 if (((struct candidate*)(K[mid]))->b < j) low = mid + 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
110 else if (((struct candidate*)(K[mid]))->b > j) upper = mid - 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
111 else return -1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
112
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
113 mid = (low + upper) / 2;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
114 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
115 return -1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
116 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
117
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
118 static struct candidate * new_candidate (int i, int j, struct candidate* prev)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
119 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
120 struct candidate *c = xzalloc(sizeof(struct candidate));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
121
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
122 c->a = i;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
123 c->b = j;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
124 c->prev = prev;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
125 return c;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
126 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
127
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
128
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
129 static void free_candidates(struct candidate *c)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
130 {
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
131 struct candidate *t = c;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
132
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
133 while ((t = c)) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
134 c = c->next;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
135 free(t);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
136 }
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
137 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
138 /*
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
139 * 1. Search K[r: k] for an element K[s] such that K[s]-> b < j and K[s + 1]->b > j
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
140 * 2. if found do
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
141 * 2.a. If K[s + 1]->b > j do K[r] = c; r = s+1 and c = candidate(i, j, K[s]) //we have a candidate
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
142 * 2.b. if s = k (fence reached move it further) do K[k + 2] = K[k + 1], k++
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
143 * 3. if E[p].last true break i.e we have reached at the end of an equiv class
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
144 * else p = p + 1 //keep traversing the equiv class.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
145 * 4. K[r] = c //Save the sucessfully filled k-candidate.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
146 */
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
147 static void do_merge(struct candidate **K, int *k, int i,
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
148 struct v_vector *E, int p)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
149 {
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
150 int r = 0, s, j;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
151 struct candidate *pr = 0, *c = K[0];
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
152
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
153 while (1) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
154 j = E[p].serial;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
155 s = search(K, r, *k, j);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
156 if (s >= 0 && (((struct candidate*)(K[s]))->b < j &&
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
157 ((struct candidate*)(K[s + 1]))->b > j)) {
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
158
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
159 if (((struct candidate*)(K[s + 1]))->b > j) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
160 pr = K[s];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
161 if (r && K[r]) c->next = K[r];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
162 K[r] = c;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
163 r = s + 1;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
164 c = new_candidate(i , j, pr);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
165 }
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
166 if (s == *k) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
167 K[*k + 2] = K[*k + 1];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
168 *k = *k + 1;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
169 break;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
170 }
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
171 }
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
172 if (E[p].last) break;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
173 else p = p + 1;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
174 }
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
175 K[r] = c;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
176 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
177
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
178 static FILE* read_stdin()
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
179 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
180 char tmp_name[] = "/tmp/diffXXXXXX";
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
181 int rd, wr, tmpfd = mkstemp(tmp_name);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
182
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
183 if (tmpfd == -1) perror_exit("mkstemp");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
184 unlink(tmp_name);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
185
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
186 while (1) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
187 rd = xread(STDIN_FILENO, toybuf, sizeof(toybuf));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
188
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
189 if (!rd) break;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
190 if (rd < 0) perror_exit("read error");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
191 wr = writeall(tmpfd, toybuf, rd);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
192 if (wr < 0) perror_exit("write");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
193 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
194 return fdopen(tmpfd, "r");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
195 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
196
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
197 static int read_tok(FILE *fp, off_t *off, int tok)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
198 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
199 int t = 0, is_space;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
200
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
201 tok |= empty;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
202 while (!(tok & eol)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
203
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
204 t = fgetc(fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
205 if (off && t != EOF) *off += 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
206 is_space = isspace(t) || (t == EOF);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
207 tok |= (t & (eof + eol)); //set tok eof+eol when t is eof
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
208
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
209 if (t == '\n') tok |= eol;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
210 if (toys.optflags & FLAG_i)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
211 if (t >= 'A' && t <= 'Z') t = tolower(t);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
212
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
213 if (toys.optflags & FLAG_w && is_space) continue;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
214
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
215 if (toys.optflags & FLAG_b) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
216 if (tok & space) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
217 if (is_space) continue;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
218 tok &= ~space;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
219 } else if (is_space) t = space + ' ';
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
220 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
221 tok &= ~(empty + 0xff); //remove empty and char too.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
222 tok |= t; //add most recent char
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
223 break;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
224 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
225
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
226 return tok;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
227 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
228
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
229 int bcomp(const void *a, const void *b)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
230 {
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
231 struct v_vector *l = (struct v_vector*)a,
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
232 *r = (struct v_vector*)b;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
233 int ret = l->hash - r->hash;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
234
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
235 if (!ret) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
236 if ((r -1)->last) return 0;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
237 else return -1;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
238 }
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
239 return ret;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
240 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
241 /* file[0] corresponds file 1 and file[1] correspond file 2.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
242 * 1. calc hashes for both the files and store them in vector(v[0], v[1])
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
243 * 2. sort file[1] with hash as primary and serial as sec. key
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
244 * 3. Form the equivalance class of file[1] stored in e vector. It lists all the equivalence
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
245 * classes of lines in file[1], with e.last = true on the last element of each class.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
246 * The elements are ordered by serial within classes.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
247 * 4. Form the p vector stored in p_vector. p_vector[i], if non-zero, now points in e vector
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
248 * to the begining of the equiv class of lines in file[1] equivalent to line
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
249 * i in file[0].
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
250 * 5. Form the k-candidates as discribed in do_merge.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
251 * 6. Create a vector J[i] = j, such that i'th line in file[0] is j'th line of
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
252 * file[1], i.e J comprises LCS
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
253 */
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
254 static int * create_j_vector()
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
255 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
256 int tok, i, j, size = 100, k;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
257 off_t off;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
258 long hash;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
259 int *p_vector, *J;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
260 struct v_vector *v[2], *e;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
261 struct candidate **kcand, *pr;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
262
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
263 for (i = 0; i < 2; i++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
264 tok = off = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
265 hash = 5831;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
266 v[i] = xzalloc(size * sizeof(struct v_vector));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
267 TT.offset[i] = xzalloc(size * sizeof(int));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
268 file[i].len = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
269 fseek(file[i].fp, 0, SEEK_SET);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
270
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
271 while (1) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
272 tok = read_tok(file[i].fp, &off, tok);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
273 if (!(tok & empty)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
274 hash = ((hash << 5) + hash) + (tok & 0xff);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
275 continue;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
276 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
277
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
278 if (size == ++file[i].len) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
279 size = size * 11 / 10;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
280 v[i] = xrealloc(v[i], size*sizeof(struct v_vector));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
281 TT.offset[i] = xrealloc(TT.offset[i], size*sizeof(int));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
282 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
283
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
284 v[i][file[i].len].hash = hash & INT_MAX;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
285 TT.offset[i][file[i].len] = off;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
286 if ((tok & eof)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
287 TT.offset[i][file[i].len] = ++off;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
288 break;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
289 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
290 hash = 5831; //next line
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
291 tok = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
292 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
293 if (TT.offset[i][file[i].len] - TT.offset[i][file[i].len - 1] == 1)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
294 file[i].len--;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
295 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
296
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
297 for (i = 0; i <= file[1].len; i++) v[1][i].serial = i;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
298 qsort(v[1] + 1, file[1].len, sizeof(struct v_vector), comp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
299
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
300 e = v[1];
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
301 e[0].serial = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
302 e[0].last = 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
303 for ( i = 1; i <= file[1].len; i++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
304 if ((i == file[1].len) || (v[1][i].hash != v[1][i+1].hash)) e[i].last = 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
305 else e[i].last = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
306 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
307
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
308 p_vector = xzalloc((file[0].len + 2) * sizeof(int));
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
309 for (i = 1; i <= file[0].len; i++) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
310 void *r = bsearch(&v[0][i], (e + 1), file[1].len, sizeof(e[0]), bcomp);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
311 if (r) p_vector[i] = (struct v_vector*)r - e;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
312 }
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
313
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
314 for (i = 1; i <= file[0].len; i++)
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
315 e[i].p = p_vector[i];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
316 free(p_vector);
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
317
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
318 size = 100;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
319 kcand = xzalloc(size * sizeof(struct candidate*));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
320
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
321 kcand[0] = new_candidate(0 , 0, NULL);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
322 kcand[1] = new_candidate(file[0].len+1, file[1].len+1, NULL); //the fence
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
323
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
324 k = 0; //last successfully filled k candidate.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
325 for (i = 1; i <= file[0].len; i++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
326
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
327 if (!e[i].p) continue;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
328 if ((size - 2) == k) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
329 size = size * 11 / 10;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
330 kcand = xrealloc(kcand, (size * sizeof(struct candidate*)));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
331 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
332 do_merge(kcand, &k, i, e, e[i].p);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
333 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
334 free(v[0]); //no need for v_vector now.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
335 free(v[1]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
336
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
337 J = xzalloc((file[0].len + 2) * sizeof(int));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
338
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
339 for (pr = kcand[k]; pr; pr = pr->prev)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
340 J[pr->a] = pr->b;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
341 J[file[0].len + 1] = file[1].len+1; //mark boundary
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
342
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
343 for (i = k + 1; i >= 0; i--) free_candidates(kcand[i]);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
344 free(kcand);
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
345
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
346 for (i = 1; i <= file[0].len; i++) { // jackpot?
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
347 if (!J[i]) continue;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
348
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
349 fseek(file[0].fp, TT.offset[0][i - 1], SEEK_SET);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
350 fseek(file[1].fp, TT.offset[1][J[i] - 1], SEEK_SET);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
351
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
352 for (j = J[i]; i <= file[0].len && J[i] == j; i++, j++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
353 int tok0 = 0, tok1 = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
354
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
355 do {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
356 tok0 = read_tok(file[0].fp, NULL, tok0);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
357 tok1 = read_tok(file[1].fp, NULL, tok1);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
358 if (((tok0 ^ tok1) & empty) || ((tok0 & 0xff) != (tok1 & 0xff)))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
359 J[i] = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
360 } while (!(tok0 & tok1 & empty));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
361 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
362 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
363 return J;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
364 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
365
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
366 static int *diff(char **files)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
367 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
368 size_t i ,j;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
369 int s, t;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
370 char *bufi, *bufj;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
371
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
372 TT.is_binary = 0; //loop calls to diff
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
373 TT.status = SAME;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
374
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
375 for (i = 0; i < 2; i++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
376 if (IS_STDIN(files[i])) file[i].fp = read_stdin();
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
377 else file[i].fp = fopen(files[i], "r");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
378
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
379 if (!file[i].fp){
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
380 perror_msg("%s",files[i]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
381 TT.status = 2;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
382 return NULL; //return SAME
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
383 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
384 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
385
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
386 s = sizeof(toybuf)/2;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
387 bufi = toybuf;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
388 bufj = (toybuf + s);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
389
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
390 fseek(file[0].fp, 0, SEEK_SET);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
391 fseek(file[1].fp, 0, SEEK_SET);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
392
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
393 if (toys.optflags & FLAG_a) return create_j_vector();
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
394
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
395 while (1) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
396 i = fread(bufi, 1, s, file[0].fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
397 j = fread(bufj, 1, s, file[1].fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
398
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
399 if (i != j) TT.status = DIFFER;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
400
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
401 for (t = 0; t < i && !TT.is_binary; t++)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
402 if (!bufi[t]) TT.is_binary = 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
403 for (t = 0; t < j && !TT.is_binary; t++)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
404 if (!bufj[t]) TT.is_binary = 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
405
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
406 i = MIN(i, j);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
407 for (t = 0; t < i; t++)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
408 if (bufi[t] != bufj[t]) TT.status = DIFFER;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
409
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
410 if (!i || !j) break;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
411 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
412 if (TT.is_binary || (TT.status == SAME)) return NULL;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
413 return create_j_vector();
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
414 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
415
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
416 static void print_diff(int a, int b, char c, int *off_set, FILE *fp)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
417 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
418 int i, j, cc, cl;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
419
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
420 for (i = a; i <= b; i++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
421 fseek(fp, off_set[i - 1], SEEK_SET);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
422 putchar(c);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
423 if (toys.optflags & FLAG_T) putchar('\t');
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
424 for (j = 0, cl = 0; j < (off_set[i] - off_set[i - 1]); j++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
425 cc = fgetc(fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
426 if (cc == EOF) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
427 printf("\n\\ No newline at end of file\n");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
428 return;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
429 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
430 if ((cc == '\t') && (toys.optflags & FLAG_t))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
431 do putchar(' '); while (++cl & 7);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
432 else {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
433 putchar(cc); //xputc has calls to fflush, it hurts performance badly.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
434 cl++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
435 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
436 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
437 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
438 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
439
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
440 static char *concat_file_path(char *path, char *default_path)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
441 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
442 char *final_path;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
443
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
444 if ('/' == path[strlen(path) - 1]) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
445 while (*default_path == '/') ++default_path;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
446 final_path = xmprintf("%s%s", path, default_path);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
447 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
448 else if (*default_path != '/')
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
449 final_path = xmprintf("%s/%s", path, default_path);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
450 else final_path = xmprintf("%s%s", path, default_path);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
451 return final_path;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
452 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
453
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
454 static int skip(struct dirtree *node)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
455 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
456 int len = strlen(toys.optargs[TT.dir_num]), ret = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
457 char *tmp = NULL, *ptr, *f_path = dirtree_path(node, NULL);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
458 struct stat st;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
459
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
460 ptr = f_path;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
461 ptr += len;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
462 if (ptr[0]) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
463 tmp = concat_file_path(toys.optargs[1 - TT.dir_num], ptr);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
464 if (tmp && !stat(tmp, &st)) ret = 0; //it is there on other side
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
465 else ret = 1; //not present on other side.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
466 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
467 free(f_path);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
468 if (tmp) free(tmp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
469 return ret; //add otherwise
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
470 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
471
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
472 static void add_to_list(struct dirtree *node)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
473 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
474 char *full_path;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
475
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
476 dir[TT.dir_num].list = xrealloc(dir[TT.dir_num].list,
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
477 (TT.size + 1)*sizeof(char*));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
478 TT.size++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
479 full_path = dirtree_path(node, NULL);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
480 dir[TT.dir_num].list[TT.size - 1] = full_path;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
481 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
482
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
483 static int list_dir (struct dirtree *node)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
484 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
485 int ret = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
486
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
487 if (node->parent && !dirtree_notdotdot(node)) return 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
488
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
489 if (S_ISDIR(node->st.st_mode) && !node->parent) { //add root dirs.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
490 add_to_list(node);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
491 return (DIRTREE_RECURSE|DIRTREE_SYMFOLLOW);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
492 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
493
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
494 if (S_ISDIR(node->st.st_mode) && (toys.optflags & FLAG_r)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
495 if (!(toys.optflags & FLAG_N)) ret = skip(node);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
496 if (!ret) return (DIRTREE_RECURSE|DIRTREE_SYMFOLLOW);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
497 else {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
498 add_to_list(node); //only at one side.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
499 return 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
500 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
501 } else {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
502 add_to_list(node);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
503 return S_ISDIR(node->st.st_mode) ? 0 : (DIRTREE_RECURSE|DIRTREE_SYMFOLLOW);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
504 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
505 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
506
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
507 static int cmp(const void *p1, const void *p2)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
508 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
509 return strcmp(* (char * const *)p1, * (char * const *)p2);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
510 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
511
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
512 static void do_diff(char **files)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
513 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
514
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
515 long i = 1, size = 1, x = 0, change = 0, ignore_white,
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
516 start1, end1, start2, end2;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
517 struct diff *d;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
518 struct arg_list *llist = TT.L_list;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
519 int *J;
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
520
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
521 TT.offset[0] = TT.offset[1] = NULL;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
522 J = diff(files);
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
523
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
524 if (!J) return; //No need to compare, have to status only
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
525
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
526 d = xzalloc(size *sizeof(struct diff));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
527 do {
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
528 ignore_white = 0;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
529 for (d[x].a = i; d[x].a <= file[0].len; d[x].a++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
530 if (J[d[x].a] != (J[d[x].a - 1] + 1)) break;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
531 else continue;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
532 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
533 d[x].c = (J[d[x].a - 1] + 1);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
534
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
535 for (d[x].b = (d[x].a - 1); d[x].b <= file[0].len; d[x].b++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
536 if (J[d[x].b + 1]) break;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
537 else continue;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
538 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
539 d[x].d = (J[d[x].b + 1] - 1);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
540
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
541 if ((toys.optflags & FLAG_B)) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
542 if (d[x].a <= d[x].b) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
543 if ((TT.offset[0][d[x].b] - TT.offset[0][d[x].a - 1])
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
544 == (d[x].b - d[x].a + 1))
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
545 ignore_white = 1;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
546 } else if (d[x].c <= d[x].d){
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
547 if ((TT.offset[1][d[x].d] - TT.offset[1][d[x].c - 1])
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
548 == (d[x].d - d[x].c + 1))
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
549 ignore_white = 1;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
550 }
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
551 }
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
552
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
553 if ((d[x].a <= d[x].b || d[x].c <= d[x].d) && !ignore_white)
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
554 change = 1; //is we have diff ?
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
555
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
556 if (!ignore_white) d = xrealloc(d, (x + 2) *sizeof(struct diff));
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
557 i = d[x].b + 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
558 if (i > file[0].len) break;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
559 J[d[x].b] = d[x].d;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
560 if (!ignore_white) x++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
561 } while (i <= file[0].len);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
562
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
563 i = x+1;
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
564 TT.status = change; //update status, may change bcoz of -w etc.
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
565
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
566 if (!(toys.optflags & FLAG_q) && change) { //start of !FLAG_q
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
567
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
568 xprintf("--- %s\n", (toys.optflags & FLAG_L) ? llist->arg : files[0]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
569 if (((toys.optflags & FLAG_L) && !llist->next) || !(toys.optflags & FLAG_L))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
570 xprintf("+++ %s\n", files[1]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
571 else {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
572 while (llist->next) llist = llist->next;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
573 xprintf("+++ %s\n", llist->arg);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
574 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
575
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
576 struct diff *t, *ptr1 = d, *ptr2 = d;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
577 while (i) {
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
578 long a,b;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
579
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
580 if (TT.ct > file[0].len) TT.ct = file[0].len; //trim context to file len.
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
581 if (ptr1->b < ptr1->a && ptr1->d < ptr1->c) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
582 i--;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
583 continue;
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
584 }
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
585 //Handle the context stuff
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
586 a = ptr1->a;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
587 b = ptr1->b;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
588
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
589 b = MIN(file[0].len, b);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
590 if (i == x + 1) ptr1->suff = MAX(1,a - TT.ct);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
591 else {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
592 if ((ptr1 - 1)->prev >= (ptr1->a - TT.ct))
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
593 ptr1->suff = (ptr1 - 1)->prev + 1;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
594 else ptr1->suff = ptr1->a - TT.ct;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
595 }
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
596 calc_ct:
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
597 if (i > 1) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
598 if ((ptr2->b + TT.ct) >= (ptr2 + 1)->a) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
599 ptr2++;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
600 i--;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
601 goto calc_ct;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
602 } else ptr2->prev = ptr2->b + TT.ct;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
603 } else ptr2->prev = ptr2->b;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
604 start1 = (ptr2->prev - ptr1->suff + 1);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
605 end1 = (start1 == 1) ? -1 : start1;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
606 start2 = MAX(1, ptr1->c - (ptr1->a - ptr1->suff));
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
607 end2 = ptr2->prev - ptr2->b + ptr2->d;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
608
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
609 printf("@@ -%ld", start1 ? ptr1->suff: (ptr1->suff -1));
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
610 if (end1 != -1) printf(",%ld ", ptr2->prev-ptr1->suff + 1);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
611 else putchar(' ');
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
612
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
613 printf("+%ld", (end2 - start2 + 1) ? start2: (start2 -1));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
614 if ((end2 - start2 +1) != 1) printf(",%ld ", (end2 - start2 +1));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
615 else putchar(' ');
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
616 printf("@@\n");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
617
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
618 for (t = ptr1; t <= ptr2; t++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
619 if (t== ptr1) print_diff(t->suff, t->a-1, ' ', TT.offset[0], file[0].fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
620 print_diff(t->a, t->b, '-', TT.offset[0], file[0].fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
621 print_diff(t->c, t->d, '+', TT.offset[1], file[1].fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
622 if (t == ptr2)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
623 print_diff(t->b+1, (t)->prev, ' ', TT.offset[0], file[0].fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
624 else print_diff(t->b+1, (t+1)->a-1, ' ', TT.offset[0], file[0].fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
625 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
626 ptr2++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
627 ptr1 = ptr2;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
628 i--;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
629 } //end of while
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
630 } //End of !FLAG_q
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
631 free(d);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
632 free(J);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
633 free(TT.offset[0]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
634 free(TT.offset[1]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
635 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
636
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
637 static void show_status(char **files)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
638 {
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
639 switch (TT.status) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
640 case SAME:
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
641 if (toys.optflags & FLAG_s)
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
642 printf("Files %s and %s are identical\n",files[0], files[1]);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
643 break;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
644 case DIFFER:
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
645 if ((toys.optflags & FLAG_q) || TT.is_binary)
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
646 printf("Files %s and %s differ\n",files[0], files[1]);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
647 break;
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
648 }
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
649 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
650
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
651 static void create_empty_entry(int l , int r, int j)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
652 {
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
653 struct stat st[2];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
654 char *f[2], *path[2];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
655 int i;
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
656
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
657 if (j > 0 && (toys.optflags & FLAG_N)) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
658 path[0] = concat_file_path(dir[0].list[0], dir[1].list[r] + TT.len[1]);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
659 f[0] = "/dev/null";
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
660 path[1] = f[1] = dir[1].list[r];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
661 stat(f[1], &st[0]);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
662 st[1] = st[0];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
663 }
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
664 else if (j < 0 && (toys.optflags & FLAG_N)) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
665 path[1] = concat_file_path(dir[1].list[0], dir[0].list[l] + TT.len[0]);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
666 f[1] = "/dev/null";
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
667 path[0] = f[0] = dir[0].list[l];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
668 stat(f[0], &st[0]);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
669 st[1] = st[0];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
670 }
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
671
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
672 if (!j) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
673 for (i = 0; i < 2; i++) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
674 path[i] = f[i] = dir[i].list[!i ? l: r];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
675 stat(f[i], &st[i]);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
676 }
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
677 }
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
678
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
679 if (S_ISDIR(st[0].st_mode) && S_ISDIR(st[1].st_mode))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
680 printf("Common subdirectories: %s and %s\n", path[0], path[1]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
681 else if (!S_ISREG(st[0].st_mode) && !S_ISDIR(st[0].st_mode))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
682 printf("File %s is not a regular file or directory "
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
683 "and was skipped\n", path[0]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
684 else if (!S_ISREG(st[1].st_mode) && !S_ISDIR(st[1].st_mode))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
685 printf("File %s is not a regular file or directory "
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
686 "and was skipped\n", path[1]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
687 else if (S_ISDIR(st[0].st_mode) != S_ISDIR(st[1].st_mode)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
688 if (S_ISDIR(st[0].st_mode))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
689 printf("File %s is a %s while file %s is a"
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
690 " %s\n", path[0], "directory", path[1], "regular file");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
691 else
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
692 printf("File %s is a %s while file %s is a"
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
693 " %s\n", path[0], "regular file", path[1], "directory");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
694 } else {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
695 do_diff(f);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
696 show_status(path);
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
697 if (file[0].fp) fclose(file[0].fp);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
698 if (file[1].fp) fclose(file[1].fp);
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
699 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
700
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
701 if ((toys.optflags & FLAG_N) && j) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
702 if (j > 0) free(path[0]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
703 else free(path[1]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
704 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
705 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
706
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
707 static void diff_dir(int *start)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
708 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
709 int l, r, j = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
710
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
711 l = start[0]; //left side file start
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
712 r = start[1]; //right side file start
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
713 while (l < dir[0].nr_elm && r < dir[1].nr_elm) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
714 if ((j = strcmp ((dir[0].list[l] + TT.len[0]),
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
715 (dir[1].list[r] + TT.len[1]))) && !(toys.optflags & FLAG_N)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
716 if (j > 0) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
717 printf ("Only in %s: %s\n", dir[1].list[0], dir[1].list[r] + TT.len[1]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
718 free(dir[1].list[r]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
719 r++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
720 } else {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
721 printf ("Only in %s: %s\n", dir[0].list[0], dir[0].list[l] + TT.len[0]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
722 free(dir[0].list[l]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
723 l++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
724 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
725 TT.status = DIFFER;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
726 } else {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
727 create_empty_entry(l, r, j); //create non empty dirs/files if -N.
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
728 if (j > 0) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
729 free(dir[1].list[r]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
730 r++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
731 } else if (j < 0) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
732 free(dir[0].list[l]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
733 l++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
734 } else {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
735 free(dir[1].list[r]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
736 free(dir[0].list[l]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
737 l++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
738 r++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
739 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
740 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
741 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
742
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
743 if (l == dir[0].nr_elm) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
744 while (r < dir[1].nr_elm) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
745 if (!(toys.optflags & FLAG_N)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
746 printf ("Only in %s: %s\n", dir[1].list[0], dir[1].list[r] + TT.len[1]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
747 TT.status = DIFFER;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
748 } else create_empty_entry(l, r, 1);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
749 free(dir[1].list[r]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
750 r++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
751 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
752 } else if (r == dir[1].nr_elm) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
753 while (l < dir[0].nr_elm) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
754 if (!(toys.optflags & FLAG_N)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
755 printf ("Only in %s: %s\n", dir[0].list[0], dir[0].list[l] + TT.len[0]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
756 TT.status = DIFFER;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
757 } else create_empty_entry(l, r, -1);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
758 free(dir[0].list[l]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
759 l++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
760 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
761 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
762 free(dir[0].list[0]); //we are done, free root nodes too
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
763 free(dir[1].list[0]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
764 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
765
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
766 void diff_main(void)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
767 {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
768 struct stat st[2];
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
769 int j = 0, k = 1, start[2] = {1, 1};
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
770 char *files[2];
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
771 struct dirtree *root;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
772
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
773 for (j = 0; j < 2; j++) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
774 files[j] = toys.optargs[j];
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
775 if (IS_STDIN(files[j])) {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
776 if (fstat(0, &st[j]) == -1)
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
777 perror_exit("can fstat %s", files[j]);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
778 } else {
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
779 if (stat(files[j], &st[j]) == -1)
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
780 perror_exit("can't stat %s", files[j]);
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
781 }
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
782 }
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
783
1419
e22e9eee9346 Squash mix of tabs and spaces to just spaces.
Rob Landley <rob@landley.net>
parents: 1413
diff changeset
784 if (IS_STDIN(files[0]) && IS_STDIN(files[1])) { //compat :(
1413
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
785 show_status(files); //check ASAP
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
786 return;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
787 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
788
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
789 if ((IS_STDIN(files[0]) || IS_STDIN(files[1]))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
790 && (S_ISDIR(st[0].st_mode) || S_ISDIR(st[1].st_mode)))
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
791 error_exit("can't compare stdin to directory");
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
792
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
793 if ((st[0].st_ino == st[1].st_ino) //physicaly same device
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
794 &&(st[0].st_dev == st[1].st_dev)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
795 show_status(files);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
796 return ;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
797 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
798
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
799 if (S_ISDIR(st[0].st_mode) && S_ISDIR(st[1].st_mode)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
800 for (j = 0; j < 2; j++) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
801 memset(&dir[j], 0, sizeof(dir));
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
802 root = dirtree_add_node(0, files[j], 1);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
803 if (root) dirtree_handle_callback(root, list_dir);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
804 dir[j].nr_elm = TT.size; //size updated in list_dir
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
805 qsort(&(dir[j].list[1]), (TT.size - 1), sizeof(char*), cmp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
806
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
807 TT.len[j] = strlen(dir[j].list[0]); //calc root node len
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
808 TT.len[j] += (dir[j].list[0][TT.len[j] -1] != '/');
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
809
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
810 if (toys.optflags & FLAG_S) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
811 while (k < TT.size && strcmp(dir[j].list[k] +
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
812 TT.len[j], TT.start) < 0) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
813 start[j] += 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
814 k++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
815 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
816 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
817 TT.dir_num++;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
818 TT.size = 0;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
819 k = 1;
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
820 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
821 diff_dir(start);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
822 free(dir[0].list); //free array
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
823 free(dir[1].list);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
824 } else {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
825 if (S_ISDIR(st[0].st_mode) || S_ISDIR(st[1].st_mode)) {
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
826 int d = S_ISDIR(st[0].st_mode);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
827 char *slash = strrchr(files[d], '/');
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
828
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
829 files[1 - d] = concat_file_path(files[1 - d], slash ? slash + 1 : files[d]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
830 if ((stat(files[1 - d], &st[1 - d])) == -1)
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
831 perror_exit("%s", files[1 - d]);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
832 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
833 do_diff(files);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
834 show_status(files);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
835 if (file[0].fp) fclose(file[0].fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
836 if (file[1].fp) fclose(file[1].fp);
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
837 }
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
838 toys.exitval = TT.status; //exit status will be the status
27a0fe0b7582 New toys unified DIFF and USERDEL.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
839 }