10f66f451Sopenharmony_ci/* diff.c - compare files line by line 20f66f451Sopenharmony_ci * 30f66f451Sopenharmony_ci * Copyright 2014 Sandeep Sharma <sandeep.jack2756@gmail.com> 40f66f451Sopenharmony_ci * Copyright 2014 Ashwini Kumar <ak.ashwini1981@gmail.com> 50f66f451Sopenharmony_ci * 60f66f451Sopenharmony_ci * See: http://cm.bell-labs.com/cm/cs/cstr/41.pdf 70f66f451Sopenharmony_ci 80f66f451Sopenharmony_ciUSE_DIFF(NEWTOY(diff, "<2>2(color)(strip-trailing-cr)B(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|TOYFLAG_ARGFAIL(2))) 90f66f451Sopenharmony_ci 100f66f451Sopenharmony_ciconfig DIFF 110f66f451Sopenharmony_ci bool "diff" 120f66f451Sopenharmony_ci default n 130f66f451Sopenharmony_ci help 140f66f451Sopenharmony_ci usage: diff [-abBdiNqrTstw] [-L LABEL] [-S FILE] [-U LINES] FILE1 FILE2 150f66f451Sopenharmony_ci 160f66f451Sopenharmony_ci -a Treat all files as text 170f66f451Sopenharmony_ci -b Ignore changes in the amount of whitespace 180f66f451Sopenharmony_ci -B Ignore changes whose lines are all blank 190f66f451Sopenharmony_ci -d Try hard to find a smaller set of changes 200f66f451Sopenharmony_ci -i Ignore case differences 210f66f451Sopenharmony_ci -L Use LABEL instead of the filename in the unified header 220f66f451Sopenharmony_ci -N Treat absent files as empty 230f66f451Sopenharmony_ci -q Output only whether files differ 240f66f451Sopenharmony_ci -r Recurse 250f66f451Sopenharmony_ci -S Start with FILE when comparing directories 260f66f451Sopenharmony_ci -T Make tabs line up by prefixing a tab when necessary 270f66f451Sopenharmony_ci -s Report when two files are the same 280f66f451Sopenharmony_ci -t Expand tabs to spaces in output 290f66f451Sopenharmony_ci -u Unified diff 300f66f451Sopenharmony_ci -U Output LINES lines of context 310f66f451Sopenharmony_ci -w Ignore all whitespace 320f66f451Sopenharmony_ci 330f66f451Sopenharmony_ci --color Colored output 340f66f451Sopenharmony_ci --strip-trailing-cr Strip trailing '\r's from input lines 350f66f451Sopenharmony_ci*/ 360f66f451Sopenharmony_ci 370f66f451Sopenharmony_ci#define FOR_diff 380f66f451Sopenharmony_ci#include "toys.h" 390f66f451Sopenharmony_ci 400f66f451Sopenharmony_ciGLOBALS( 410f66f451Sopenharmony_ci long ct; 420f66f451Sopenharmony_ci char *start; 430f66f451Sopenharmony_ci struct arg_list *L_list; 440f66f451Sopenharmony_ci 450f66f451Sopenharmony_ci int dir_num, size, is_binary, status, change, len[2]; 460f66f451Sopenharmony_ci int *offset[2]; 470f66f451Sopenharmony_ci struct stat st[2]; 480f66f451Sopenharmony_ci) 490f66f451Sopenharmony_ci 500f66f451Sopenharmony_ci#define MIN(x,y) ((x) < (y) ? (x) : (y)) 510f66f451Sopenharmony_ci#define MAX(x,y) ((x) > (y) ? (x) : (y)) 520f66f451Sopenharmony_ci#define IS_STDIN(s) ((s)[0] == '-' && !(s)[1]) 530f66f451Sopenharmony_ci 540f66f451Sopenharmony_cistruct v_vector { 550f66f451Sopenharmony_ci unsigned serial:31; 560f66f451Sopenharmony_ci unsigned last:1; 570f66f451Sopenharmony_ci union { 580f66f451Sopenharmony_ci unsigned hash; 590f66f451Sopenharmony_ci unsigned p; 600f66f451Sopenharmony_ci }; 610f66f451Sopenharmony_ci}; 620f66f451Sopenharmony_ci 630f66f451Sopenharmony_cistruct diff { 640f66f451Sopenharmony_ci long a, b, c, d, prev, suff; 650f66f451Sopenharmony_ci}; 660f66f451Sopenharmony_ci 670f66f451Sopenharmony_cistatic struct dir_t { 680f66f451Sopenharmony_ci char **list; 690f66f451Sopenharmony_ci int nr_elm; 700f66f451Sopenharmony_ci} dir[2]; 710f66f451Sopenharmony_ci 720f66f451Sopenharmony_cistruct candidate { 730f66f451Sopenharmony_ci int a, b; 740f66f451Sopenharmony_ci struct candidate *prev, *next; 750f66f451Sopenharmony_ci}; 760f66f451Sopenharmony_ci 770f66f451Sopenharmony_cistatic struct file_t { 780f66f451Sopenharmony_ci FILE *fp; 790f66f451Sopenharmony_ci int len; 800f66f451Sopenharmony_ci} file[2]; 810f66f451Sopenharmony_ci 820f66f451Sopenharmony_cienum { 830f66f451Sopenharmony_ci SAME, 840f66f451Sopenharmony_ci DIFFER, 850f66f451Sopenharmony_ci}; 860f66f451Sopenharmony_ci 870f66f451Sopenharmony_cienum { 880f66f451Sopenharmony_ci empty = 1 << 9, 890f66f451Sopenharmony_ci eol = 1 << 10, 900f66f451Sopenharmony_ci eof = 1 << 11, 910f66f451Sopenharmony_ci space = 1 << 12 920f66f451Sopenharmony_ci}; 930f66f451Sopenharmony_ci 940f66f451Sopenharmony_cistatic int comp(const void *a, const void* b) 950f66f451Sopenharmony_ci{ 960f66f451Sopenharmony_ci int i = ((struct v_vector *)a)->hash - 970f66f451Sopenharmony_ci ((struct v_vector *)b)->hash; 980f66f451Sopenharmony_ci 990f66f451Sopenharmony_ci if (!i) i = ((struct v_vector *)a)->serial - 1000f66f451Sopenharmony_ci ((struct v_vector *)b)->serial; 1010f66f451Sopenharmony_ci return i; 1020f66f451Sopenharmony_ci} 1030f66f451Sopenharmony_ci 1040f66f451Sopenharmony_cistatic int search (struct candidate **K, int r, int k, int j) 1050f66f451Sopenharmony_ci{ 1060f66f451Sopenharmony_ci int low = r, upper = k, mid; 1070f66f451Sopenharmony_ci 1080f66f451Sopenharmony_ci mid = (low + upper) / 2; 1090f66f451Sopenharmony_ci while (low <= mid) { 1100f66f451Sopenharmony_ci if (((struct candidate*)(K[mid]))->b < j && 1110f66f451Sopenharmony_ci ((struct candidate*)(K[mid + 1]))->b > j) 1120f66f451Sopenharmony_ci return mid; 1130f66f451Sopenharmony_ci 1140f66f451Sopenharmony_ci if (((struct candidate*)(K[mid]))->b < j) low = mid + 1; 1150f66f451Sopenharmony_ci else if (((struct candidate*)(K[mid]))->b > j) upper = mid - 1; 1160f66f451Sopenharmony_ci else return -1; 1170f66f451Sopenharmony_ci 1180f66f451Sopenharmony_ci mid = (low + upper) / 2; 1190f66f451Sopenharmony_ci } 1200f66f451Sopenharmony_ci return -1; 1210f66f451Sopenharmony_ci} 1220f66f451Sopenharmony_ci 1230f66f451Sopenharmony_cistatic struct candidate * new_candidate (int i, int j, struct candidate* prev) 1240f66f451Sopenharmony_ci{ 1250f66f451Sopenharmony_ci struct candidate *c = xzalloc(sizeof(struct candidate)); 1260f66f451Sopenharmony_ci 1270f66f451Sopenharmony_ci c->a = i; 1280f66f451Sopenharmony_ci c->b = j; 1290f66f451Sopenharmony_ci c->prev = prev; 1300f66f451Sopenharmony_ci return c; 1310f66f451Sopenharmony_ci} 1320f66f451Sopenharmony_ci 1330f66f451Sopenharmony_ci 1340f66f451Sopenharmony_cistatic void free_candidates(struct candidate *c) 1350f66f451Sopenharmony_ci{ 1360f66f451Sopenharmony_ci struct candidate *t = c; 1370f66f451Sopenharmony_ci 1380f66f451Sopenharmony_ci while ((t = c)) { 1390f66f451Sopenharmony_ci c = c->next; 1400f66f451Sopenharmony_ci free(t); 1410f66f451Sopenharmony_ci } 1420f66f451Sopenharmony_ci} 1430f66f451Sopenharmony_ci/* 1440f66f451Sopenharmony_ci * 1. Search K[r: k] for an element K[s] such that K[s]-> b < j and K[s + 1]->b > j 1450f66f451Sopenharmony_ci * 2. if found do 1460f66f451Sopenharmony_ci * 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 1470f66f451Sopenharmony_ci * 2.b. if s = k (fence reached move it further) do K[k + 2] = K[k + 1], k++ 1480f66f451Sopenharmony_ci * 3. if E[p].last true break i.e we have reached at the end of an equiv class 1490f66f451Sopenharmony_ci * else p = p + 1 //keep traversing the equiv class. 1500f66f451Sopenharmony_ci * 4. K[r] = c //Save the sucessfully filled k-candidate. 1510f66f451Sopenharmony_ci */ 1520f66f451Sopenharmony_cistatic void do_merge(struct candidate **K, int *k, int i, 1530f66f451Sopenharmony_ci struct v_vector *E, int p) 1540f66f451Sopenharmony_ci{ 1550f66f451Sopenharmony_ci int r = 0, s, j; 1560f66f451Sopenharmony_ci struct candidate *pr = 0, *c = K[0]; 1570f66f451Sopenharmony_ci 1580f66f451Sopenharmony_ci while (1) { 1590f66f451Sopenharmony_ci j = E[p].serial; 1600f66f451Sopenharmony_ci s = search(K, r, *k, j); 1610f66f451Sopenharmony_ci if (s >= 0 && (((struct candidate*)(K[s]))->b < j && 1620f66f451Sopenharmony_ci ((struct candidate*)(K[s + 1]))->b > j)) { 1630f66f451Sopenharmony_ci 1640f66f451Sopenharmony_ci if (((struct candidate*)(K[s + 1]))->b > j) { 1650f66f451Sopenharmony_ci pr = K[s]; 1660f66f451Sopenharmony_ci if (r && K[r]) c->next = K[r]; 1670f66f451Sopenharmony_ci K[r] = c; 1680f66f451Sopenharmony_ci r = s + 1; 1690f66f451Sopenharmony_ci c = new_candidate(i , j, pr); 1700f66f451Sopenharmony_ci } 1710f66f451Sopenharmony_ci if (s == *k) { 1720f66f451Sopenharmony_ci K[*k + 2] = K[*k + 1]; 1730f66f451Sopenharmony_ci *k = *k + 1; 1740f66f451Sopenharmony_ci break; 1750f66f451Sopenharmony_ci } 1760f66f451Sopenharmony_ci } 1770f66f451Sopenharmony_ci if (E[p].last) break; 1780f66f451Sopenharmony_ci else p = p + 1; 1790f66f451Sopenharmony_ci } 1800f66f451Sopenharmony_ci K[r] = c; 1810f66f451Sopenharmony_ci} 1820f66f451Sopenharmony_ci 1830f66f451Sopenharmony_cistatic FILE* read_stdin() 1840f66f451Sopenharmony_ci{ 1850f66f451Sopenharmony_ci char *tmp_name; 1860f66f451Sopenharmony_ci int tmpfd = xtempfile("stdin", &tmp_name); 1870f66f451Sopenharmony_ci 1880f66f451Sopenharmony_ci unlink(tmp_name); 1890f66f451Sopenharmony_ci free(tmp_name); 1900f66f451Sopenharmony_ci 1910f66f451Sopenharmony_ci xsendfile(0, tmpfd); 1920f66f451Sopenharmony_ci return fdopen(tmpfd, "r"); 1930f66f451Sopenharmony_ci} 1940f66f451Sopenharmony_ci 1950f66f451Sopenharmony_cistatic int read_tok(FILE *fp, off_t *off, int tok) 1960f66f451Sopenharmony_ci{ 1970f66f451Sopenharmony_ci int t = 0, is_space; 1980f66f451Sopenharmony_ci 1990f66f451Sopenharmony_ci tok |= empty; 2000f66f451Sopenharmony_ci while (!(tok & eol)) { 2010f66f451Sopenharmony_ci t = fgetc(fp); 2020f66f451Sopenharmony_ci 2030f66f451Sopenharmony_ci if (FLAG(strip_trailing_cr) && t == '\r') { 2040f66f451Sopenharmony_ci int t2 = fgetc(fp); 2050f66f451Sopenharmony_ci if (t2 == '\n') { 2060f66f451Sopenharmony_ci t = t2; 2070f66f451Sopenharmony_ci if (off) (*off)++; 2080f66f451Sopenharmony_ci } else { 2090f66f451Sopenharmony_ci ungetc(t2, fp); 2100f66f451Sopenharmony_ci } 2110f66f451Sopenharmony_ci } 2120f66f451Sopenharmony_ci 2130f66f451Sopenharmony_ci if (off && t != EOF) *off += 1; 2140f66f451Sopenharmony_ci is_space = isspace(t) || (t == EOF); 2150f66f451Sopenharmony_ci tok |= (t & (eof + eol)); //set tok eof+eol when t is eof 2160f66f451Sopenharmony_ci 2170f66f451Sopenharmony_ci if (t == '\n') tok |= eol; 2180f66f451Sopenharmony_ci if (toys.optflags & FLAG_i) 2190f66f451Sopenharmony_ci if (t >= 'A' && t <= 'Z') t = tolower(t); 2200f66f451Sopenharmony_ci 2210f66f451Sopenharmony_ci if (toys.optflags & FLAG_w && is_space) continue; 2220f66f451Sopenharmony_ci 2230f66f451Sopenharmony_ci if (toys.optflags & FLAG_b) { 2240f66f451Sopenharmony_ci if (tok & space) { 2250f66f451Sopenharmony_ci if (is_space) continue; 2260f66f451Sopenharmony_ci tok &= ~space; 2270f66f451Sopenharmony_ci } else if (is_space) t = space + ' '; 2280f66f451Sopenharmony_ci } 2290f66f451Sopenharmony_ci tok &= ~(empty + 0xff); //remove empty and char too. 2300f66f451Sopenharmony_ci tok |= t; //add most recent char 2310f66f451Sopenharmony_ci break; 2320f66f451Sopenharmony_ci } 2330f66f451Sopenharmony_ci 2340f66f451Sopenharmony_ci return tok; 2350f66f451Sopenharmony_ci} 2360f66f451Sopenharmony_ci 2370f66f451Sopenharmony_ciint bcomp(const void *a, const void *b) 2380f66f451Sopenharmony_ci{ 2390f66f451Sopenharmony_ci struct v_vector *l = (struct v_vector*)a, 2400f66f451Sopenharmony_ci *r = (struct v_vector*)b; 2410f66f451Sopenharmony_ci int ret = l->hash - r->hash; 2420f66f451Sopenharmony_ci 2430f66f451Sopenharmony_ci if (!ret) { 2440f66f451Sopenharmony_ci if ((r -1)->last) return 0; 2450f66f451Sopenharmony_ci else return -1; 2460f66f451Sopenharmony_ci } 2470f66f451Sopenharmony_ci return ret; 2480f66f451Sopenharmony_ci} 2490f66f451Sopenharmony_ci/* file[0] corresponds file 1 and file[1] correspond file 2. 2500f66f451Sopenharmony_ci * 1. calc hashes for both the files and store them in vector(v[0], v[1]) 2510f66f451Sopenharmony_ci * 2. sort file[1] with hash as primary and serial as sec. key 2520f66f451Sopenharmony_ci * 3. Form the equivalance class of file[1] stored in e vector. It lists all the equivalence 2530f66f451Sopenharmony_ci * classes of lines in file[1], with e.last = true on the last element of each class. 2540f66f451Sopenharmony_ci * The elements are ordered by serial within classes. 2550f66f451Sopenharmony_ci * 4. Form the p vector stored in p_vector. p_vector[i], if non-zero, now points in e vector 2560f66f451Sopenharmony_ci * to the beginning of the equiv class of lines in file[1] equivalent to line 2570f66f451Sopenharmony_ci * i in file[0]. 2580f66f451Sopenharmony_ci * 5. Form the k-candidates as discribed in do_merge. 2590f66f451Sopenharmony_ci * 6. Create a vector J[i] = j, such that i'th line in file[0] is j'th line of 2600f66f451Sopenharmony_ci * file[1], i.e J comprises LCS 2610f66f451Sopenharmony_ci */ 2620f66f451Sopenharmony_cistatic int * create_j_vector() 2630f66f451Sopenharmony_ci{ 2640f66f451Sopenharmony_ci int tok, i, j, size = 100, k; 2650f66f451Sopenharmony_ci off_t off; 2660f66f451Sopenharmony_ci long hash; 2670f66f451Sopenharmony_ci int *p_vector, *J; 2680f66f451Sopenharmony_ci struct v_vector *v[2], *e; 2690f66f451Sopenharmony_ci struct candidate **kcand, *pr; 2700f66f451Sopenharmony_ci 2710f66f451Sopenharmony_ci for (i = 0; i < 2; i++) { 2720f66f451Sopenharmony_ci tok = off = 0; 2730f66f451Sopenharmony_ci hash = 5831; 2740f66f451Sopenharmony_ci v[i] = xzalloc(size * sizeof(struct v_vector)); 2750f66f451Sopenharmony_ci TT.offset[i] = xzalloc(size * sizeof(int)); 2760f66f451Sopenharmony_ci file[i].len = 0; 2770f66f451Sopenharmony_ci fseek(file[i].fp, 0, SEEK_SET); 2780f66f451Sopenharmony_ci 2790f66f451Sopenharmony_ci while (1) { 2800f66f451Sopenharmony_ci tok = read_tok(file[i].fp, &off, tok); 2810f66f451Sopenharmony_ci if (!(tok & empty)) { 2820f66f451Sopenharmony_ci hash = ((hash << 5) + hash) + (tok & 0xff); 2830f66f451Sopenharmony_ci continue; 2840f66f451Sopenharmony_ci } 2850f66f451Sopenharmony_ci 2860f66f451Sopenharmony_ci if (size == ++file[i].len) { 2870f66f451Sopenharmony_ci size = size * 11 / 10; 2880f66f451Sopenharmony_ci v[i] = xrealloc(v[i], size*sizeof(struct v_vector)); 2890f66f451Sopenharmony_ci TT.offset[i] = xrealloc(TT.offset[i], size*sizeof(int)); 2900f66f451Sopenharmony_ci } 2910f66f451Sopenharmony_ci 2920f66f451Sopenharmony_ci v[i][file[i].len].hash = hash & INT_MAX; 2930f66f451Sopenharmony_ci TT.offset[i][file[i].len] = off; 2940f66f451Sopenharmony_ci if ((tok & eof)) { 2950f66f451Sopenharmony_ci TT.offset[i][file[i].len] = ++off; 2960f66f451Sopenharmony_ci break; 2970f66f451Sopenharmony_ci } 2980f66f451Sopenharmony_ci hash = 5831; //next line 2990f66f451Sopenharmony_ci tok = 0; 3000f66f451Sopenharmony_ci } 3010f66f451Sopenharmony_ci if (TT.offset[i][file[i].len] - TT.offset[i][file[i].len - 1] == 1) 3020f66f451Sopenharmony_ci file[i].len--; 3030f66f451Sopenharmony_ci } 3040f66f451Sopenharmony_ci 3050f66f451Sopenharmony_ci for (i = 0; i <= file[1].len; i++) v[1][i].serial = i; 3060f66f451Sopenharmony_ci qsort(v[1] + 1, file[1].len, sizeof(struct v_vector), comp); 3070f66f451Sopenharmony_ci 3080f66f451Sopenharmony_ci e = v[1]; 3090f66f451Sopenharmony_ci e[0].serial = 0; 3100f66f451Sopenharmony_ci e[0].last = 1; 3110f66f451Sopenharmony_ci for ( i = 1; i <= file[1].len; i++) { 3120f66f451Sopenharmony_ci if ((i == file[1].len) || (v[1][i].hash != v[1][i+1].hash)) e[i].last = 1; 3130f66f451Sopenharmony_ci else e[i].last = 0; 3140f66f451Sopenharmony_ci } 3150f66f451Sopenharmony_ci 3160f66f451Sopenharmony_ci p_vector = xzalloc((file[0].len + 2) * sizeof(int)); 3170f66f451Sopenharmony_ci for (i = 1; i <= file[0].len; i++) { 3180f66f451Sopenharmony_ci void *r = bsearch(&v[0][i], (e + 1), file[1].len, sizeof(e[0]), bcomp); 3190f66f451Sopenharmony_ci if (r) p_vector[i] = (struct v_vector*)r - e; 3200f66f451Sopenharmony_ci } 3210f66f451Sopenharmony_ci 3220f66f451Sopenharmony_ci for (i = 1; i <= file[0].len; i++) 3230f66f451Sopenharmony_ci e[i].p = p_vector[i]; 3240f66f451Sopenharmony_ci free(p_vector); 3250f66f451Sopenharmony_ci 3260f66f451Sopenharmony_ci size = 100; 3270f66f451Sopenharmony_ci kcand = xzalloc(size * sizeof(struct candidate*)); 3280f66f451Sopenharmony_ci 3290f66f451Sopenharmony_ci kcand[0] = new_candidate(0 , 0, NULL); 3300f66f451Sopenharmony_ci kcand[1] = new_candidate(file[0].len+1, file[1].len+1, NULL); //the fence 3310f66f451Sopenharmony_ci 3320f66f451Sopenharmony_ci k = 0; //last successfully filled k candidate. 3330f66f451Sopenharmony_ci for (i = 1; i <= file[0].len; i++) { 3340f66f451Sopenharmony_ci 3350f66f451Sopenharmony_ci if (!e[i].p) continue; 3360f66f451Sopenharmony_ci if ((size - 2) == k) { 3370f66f451Sopenharmony_ci size = size * 11 / 10; 3380f66f451Sopenharmony_ci kcand = xrealloc(kcand, (size * sizeof(struct candidate*))); 3390f66f451Sopenharmony_ci } 3400f66f451Sopenharmony_ci do_merge(kcand, &k, i, e, e[i].p); 3410f66f451Sopenharmony_ci } 3420f66f451Sopenharmony_ci free(v[0]); //no need for v_vector now. 3430f66f451Sopenharmony_ci free(v[1]); 3440f66f451Sopenharmony_ci 3450f66f451Sopenharmony_ci J = xzalloc((file[0].len + 2) * sizeof(int)); 3460f66f451Sopenharmony_ci 3470f66f451Sopenharmony_ci for (pr = kcand[k]; pr; pr = pr->prev) 3480f66f451Sopenharmony_ci J[pr->a] = pr->b; 3490f66f451Sopenharmony_ci J[file[0].len + 1] = file[1].len+1; //mark boundary 3500f66f451Sopenharmony_ci 3510f66f451Sopenharmony_ci for (i = k + 1; i >= 0; i--) free_candidates(kcand[i]); 3520f66f451Sopenharmony_ci free(kcand); 3530f66f451Sopenharmony_ci 3540f66f451Sopenharmony_ci for (i = 1; i <= file[0].len; i++) { // jackpot? 3550f66f451Sopenharmony_ci if (!J[i]) continue; 3560f66f451Sopenharmony_ci 3570f66f451Sopenharmony_ci fseek(file[0].fp, TT.offset[0][i - 1], SEEK_SET); 3580f66f451Sopenharmony_ci fseek(file[1].fp, TT.offset[1][J[i] - 1], SEEK_SET); 3590f66f451Sopenharmony_ci 3600f66f451Sopenharmony_ci for (j = J[i]; i <= file[0].len && J[i] == j; i++, j++) { 3610f66f451Sopenharmony_ci int tok0 = 0, tok1 = 0; 3620f66f451Sopenharmony_ci 3630f66f451Sopenharmony_ci do { 3640f66f451Sopenharmony_ci tok0 = read_tok(file[0].fp, NULL, tok0); 3650f66f451Sopenharmony_ci tok1 = read_tok(file[1].fp, NULL, tok1); 3660f66f451Sopenharmony_ci if (((tok0 ^ tok1) & empty) || ((tok0 & 0xff) != (tok1 & 0xff))) 3670f66f451Sopenharmony_ci J[i] = 0; 3680f66f451Sopenharmony_ci } while (!(tok0 & tok1 & empty)); 3690f66f451Sopenharmony_ci } 3700f66f451Sopenharmony_ci } 3710f66f451Sopenharmony_ci return J; 3720f66f451Sopenharmony_ci} 3730f66f451Sopenharmony_ci 3740f66f451Sopenharmony_cistatic int *diff(char **files) 3750f66f451Sopenharmony_ci{ 3760f66f451Sopenharmony_ci size_t i ,j; 3770f66f451Sopenharmony_ci int s, t; 3780f66f451Sopenharmony_ci char *bufi, *bufj; 3790f66f451Sopenharmony_ci 3800f66f451Sopenharmony_ci TT.is_binary = 0; //loop calls to diff 3810f66f451Sopenharmony_ci TT.status = SAME; 3820f66f451Sopenharmony_ci 3830f66f451Sopenharmony_ci for (i = 0; i < 2; i++) { 3840f66f451Sopenharmony_ci if (IS_STDIN(files[i])) file[i].fp = read_stdin(); 3850f66f451Sopenharmony_ci else file[i].fp = fopen(files[i], "r"); 3860f66f451Sopenharmony_ci 3870f66f451Sopenharmony_ci if (!file[i].fp){ 3880f66f451Sopenharmony_ci perror_msg("%s",files[i]); 3890f66f451Sopenharmony_ci TT.status = 2; 3900f66f451Sopenharmony_ci return NULL; //return SAME 3910f66f451Sopenharmony_ci } 3920f66f451Sopenharmony_ci } 3930f66f451Sopenharmony_ci 3940f66f451Sopenharmony_ci s = sizeof(toybuf)/2; 3950f66f451Sopenharmony_ci bufi = toybuf; 3960f66f451Sopenharmony_ci bufj = (toybuf + s); 3970f66f451Sopenharmony_ci 3980f66f451Sopenharmony_ci fseek(file[0].fp, 0, SEEK_SET); 3990f66f451Sopenharmony_ci fseek(file[1].fp, 0, SEEK_SET); 4000f66f451Sopenharmony_ci 4010f66f451Sopenharmony_ci if (toys.optflags & FLAG_a) return create_j_vector(); 4020f66f451Sopenharmony_ci 4030f66f451Sopenharmony_ci while (1) { 4040f66f451Sopenharmony_ci i = fread(bufi, 1, s, file[0].fp); 4050f66f451Sopenharmony_ci j = fread(bufj, 1, s, file[1].fp); 4060f66f451Sopenharmony_ci 4070f66f451Sopenharmony_ci if (i != j) TT.status = DIFFER; 4080f66f451Sopenharmony_ci 4090f66f451Sopenharmony_ci for (t = 0; t < i && !TT.is_binary; t++) 4100f66f451Sopenharmony_ci if (!bufi[t]) TT.is_binary = 1; 4110f66f451Sopenharmony_ci for (t = 0; t < j && !TT.is_binary; t++) 4120f66f451Sopenharmony_ci if (!bufj[t]) TT.is_binary = 1; 4130f66f451Sopenharmony_ci 4140f66f451Sopenharmony_ci i = MIN(i, j); 4150f66f451Sopenharmony_ci for (t = 0; t < i; t++) 4160f66f451Sopenharmony_ci if (bufi[t] != bufj[t]) TT.status = DIFFER; 4170f66f451Sopenharmony_ci 4180f66f451Sopenharmony_ci if (!i || !j) break; 4190f66f451Sopenharmony_ci } 4200f66f451Sopenharmony_ci if (TT.is_binary || (TT.status == SAME)) return NULL; 4210f66f451Sopenharmony_ci return create_j_vector(); 4220f66f451Sopenharmony_ci} 4230f66f451Sopenharmony_ci 4240f66f451Sopenharmony_cistatic void print_diff(int a, int b, char c, int *off_set, FILE *fp) 4250f66f451Sopenharmony_ci{ 4260f66f451Sopenharmony_ci int i, j, cc, cl; 4270f66f451Sopenharmony_ci char *reset = NULL; 4280f66f451Sopenharmony_ci 4290f66f451Sopenharmony_ci if (c != ' ' && (toys.optflags & FLAG_color)) { 4300f66f451Sopenharmony_ci printf("\033[%dm", c == '+' ? 32 : 31); 4310f66f451Sopenharmony_ci reset = "\033[0m"; 4320f66f451Sopenharmony_ci } 4330f66f451Sopenharmony_ci 4340f66f451Sopenharmony_ci for (i = a; i <= b; i++) { 4350f66f451Sopenharmony_ci fseek(fp, off_set[i - 1], SEEK_SET); 4360f66f451Sopenharmony_ci putchar(c); 4370f66f451Sopenharmony_ci if (toys.optflags & FLAG_T) putchar('\t'); 4380f66f451Sopenharmony_ci for (j = 0, cl = 0; j < (off_set[i] - off_set[i - 1]); j++) { 4390f66f451Sopenharmony_ci cc = fgetc(fp); 4400f66f451Sopenharmony_ci if (cc == EOF) { 4410f66f451Sopenharmony_ci printf("%s\n\\ No newline at end of file\n", reset ? reset : ""); 4420f66f451Sopenharmony_ci return; 4430f66f451Sopenharmony_ci } 4440f66f451Sopenharmony_ci if ((cc == '\t') && (toys.optflags & FLAG_t)) 4450f66f451Sopenharmony_ci do putchar(' '); while (++cl & 7); 4460f66f451Sopenharmony_ci else { 4470f66f451Sopenharmony_ci putchar(cc); //xputc has calls to fflush, it hurts performance badly. 4480f66f451Sopenharmony_ci cl++; 4490f66f451Sopenharmony_ci } 4500f66f451Sopenharmony_ci } 4510f66f451Sopenharmony_ci } 4520f66f451Sopenharmony_ci if (reset) printf("%s", reset); 4530f66f451Sopenharmony_ci} 4540f66f451Sopenharmony_ci 4550f66f451Sopenharmony_cistatic char *concat_file_path(char *path, char *default_path) 4560f66f451Sopenharmony_ci{ 4570f66f451Sopenharmony_ci char *final_path; 4580f66f451Sopenharmony_ci 4590f66f451Sopenharmony_ci if ('/' == path[strlen(path) - 1]) { 4600f66f451Sopenharmony_ci while (*default_path == '/') ++default_path; 4610f66f451Sopenharmony_ci final_path = xmprintf("%s%s", path, default_path); 4620f66f451Sopenharmony_ci } 4630f66f451Sopenharmony_ci else if (*default_path != '/') 4640f66f451Sopenharmony_ci final_path = xmprintf("%s/%s", path, default_path); 4650f66f451Sopenharmony_ci else final_path = xmprintf("%s%s", path, default_path); 4660f66f451Sopenharmony_ci return final_path; 4670f66f451Sopenharmony_ci} 4680f66f451Sopenharmony_ci 4690f66f451Sopenharmony_cistatic int skip(struct dirtree *node) 4700f66f451Sopenharmony_ci{ 4710f66f451Sopenharmony_ci int len = strlen(toys.optargs[TT.dir_num]), ret = 0; 4720f66f451Sopenharmony_ci char *tmp = NULL, *ptr, *f_path = dirtree_path(node, NULL); 4730f66f451Sopenharmony_ci struct stat st; 4740f66f451Sopenharmony_ci 4750f66f451Sopenharmony_ci ptr = f_path; 4760f66f451Sopenharmony_ci ptr += len; 4770f66f451Sopenharmony_ci if (ptr[0]) { 4780f66f451Sopenharmony_ci tmp = concat_file_path(toys.optargs[1 - TT.dir_num], ptr); 4790f66f451Sopenharmony_ci if (tmp && !stat(tmp, &st)) ret = 0; //it is there on other side 4800f66f451Sopenharmony_ci else ret = 1; //not present on other side. 4810f66f451Sopenharmony_ci } 4820f66f451Sopenharmony_ci free(f_path); 4830f66f451Sopenharmony_ci if (tmp) free(tmp); 4840f66f451Sopenharmony_ci return ret; //add otherwise 4850f66f451Sopenharmony_ci} 4860f66f451Sopenharmony_ci 4870f66f451Sopenharmony_cistatic void add_to_list(struct dirtree *node) 4880f66f451Sopenharmony_ci{ 4890f66f451Sopenharmony_ci char *full_path; 4900f66f451Sopenharmony_ci 4910f66f451Sopenharmony_ci dir[TT.dir_num].list = xrealloc(dir[TT.dir_num].list, 4920f66f451Sopenharmony_ci (TT.size + 1)*sizeof(char*)); 4930f66f451Sopenharmony_ci TT.size++; 4940f66f451Sopenharmony_ci full_path = dirtree_path(node, NULL); 4950f66f451Sopenharmony_ci dir[TT.dir_num].list[TT.size - 1] = full_path; 4960f66f451Sopenharmony_ci} 4970f66f451Sopenharmony_ci 4980f66f451Sopenharmony_cistatic int list_dir (struct dirtree *node) 4990f66f451Sopenharmony_ci{ 5000f66f451Sopenharmony_ci int ret = 0; 5010f66f451Sopenharmony_ci 5020f66f451Sopenharmony_ci if (!dirtree_notdotdot(node)) return 0; 5030f66f451Sopenharmony_ci 5040f66f451Sopenharmony_ci if (S_ISDIR(node->st.st_mode) && !node->parent) { //add root dirs. 5050f66f451Sopenharmony_ci add_to_list(node); 5060f66f451Sopenharmony_ci return (DIRTREE_RECURSE|DIRTREE_SYMFOLLOW); 5070f66f451Sopenharmony_ci } 5080f66f451Sopenharmony_ci 5090f66f451Sopenharmony_ci if (S_ISDIR(node->st.st_mode) && (toys.optflags & FLAG_r)) { 5100f66f451Sopenharmony_ci if (!(toys.optflags & FLAG_N)) ret = skip(node); 5110f66f451Sopenharmony_ci if (!ret) return (DIRTREE_RECURSE|DIRTREE_SYMFOLLOW); 5120f66f451Sopenharmony_ci else { 5130f66f451Sopenharmony_ci add_to_list(node); //only at one side. 5140f66f451Sopenharmony_ci return 0; 5150f66f451Sopenharmony_ci } 5160f66f451Sopenharmony_ci } else { 5170f66f451Sopenharmony_ci add_to_list(node); 5180f66f451Sopenharmony_ci return S_ISDIR(node->st.st_mode) ? 0 : (DIRTREE_RECURSE|DIRTREE_SYMFOLLOW); 5190f66f451Sopenharmony_ci } 5200f66f451Sopenharmony_ci} 5210f66f451Sopenharmony_ci 5220f66f451Sopenharmony_cistatic int cmp(const void *p1, const void *p2) 5230f66f451Sopenharmony_ci{ 5240f66f451Sopenharmony_ci return strcmp(* (char * const *)p1, * (char * const *)p2); 5250f66f451Sopenharmony_ci} 5260f66f451Sopenharmony_ci 5270f66f451Sopenharmony_ci// quote and escape filenames that have awkward characters 5280f66f451Sopenharmony_cichar *quote_filename(char *filename) 5290f66f451Sopenharmony_ci{ 5300f66f451Sopenharmony_ci char *to = "abfnrtv\"\\", *from = "\a\b\f\n\r\t\v\"\\"; 5310f66f451Sopenharmony_ci char *result, *s, *t; 5320f66f451Sopenharmony_ci size_t len = 0; 5330f66f451Sopenharmony_ci int quote = 0; 5340f66f451Sopenharmony_ci 5350f66f451Sopenharmony_ci // calculate memory usage and presence of quotes 5360f66f451Sopenharmony_ci for (s = filename; *s; s++) { 5370f66f451Sopenharmony_ci if (*s == '\a' || *s == '\b' || *s == '\f' || *s == '\r' || *s == '\v' 5380f66f451Sopenharmony_ci || *s == '\n' || *s == '\t' || *s == '"' || *s == '\\') 5390f66f451Sopenharmony_ci { 5400f66f451Sopenharmony_ci quote = 1; 5410f66f451Sopenharmony_ci len += 2; 5420f66f451Sopenharmony_ci } else if (*s == ' ') { 5430f66f451Sopenharmony_ci quote = 1; 5440f66f451Sopenharmony_ci len++; 5450f66f451Sopenharmony_ci } else if (*s < 0x20 || *s >= 0x80) { 5460f66f451Sopenharmony_ci quote = 1; 5470f66f451Sopenharmony_ci len += 4; 5480f66f451Sopenharmony_ci } else { 5490f66f451Sopenharmony_ci len++; 5500f66f451Sopenharmony_ci } 5510f66f451Sopenharmony_ci } 5520f66f451Sopenharmony_ci 5530f66f451Sopenharmony_ci // construct the new string 5540f66f451Sopenharmony_ci result = xmalloc(len + (quote ? 2 : 0) + 1); 5550f66f451Sopenharmony_ci t = result; 5560f66f451Sopenharmony_ci if (quote) *t++ = '"'; 5570f66f451Sopenharmony_ci for (s = filename; *s; s++) { 5580f66f451Sopenharmony_ci if (*s == '\a' || *s == '\b' || *s == '\f' || *s == '\r' || *s == '\v' 5590f66f451Sopenharmony_ci || *s == '\n' || *s == '\t' || *s == '"' || *s == '\\') 5600f66f451Sopenharmony_ci { 5610f66f451Sopenharmony_ci *t = '\\'; 5620f66f451Sopenharmony_ci t[1] = to[strchr(from, *s) - from]; 5630f66f451Sopenharmony_ci t += 2; 5640f66f451Sopenharmony_ci } else if (*s < 0x20 || *s >= 0x80) { 5650f66f451Sopenharmony_ci sprintf(t, "\\%.3o", *s); 5660f66f451Sopenharmony_ci t += 4; 5670f66f451Sopenharmony_ci } else { 5680f66f451Sopenharmony_ci *t++ = *s; 5690f66f451Sopenharmony_ci } 5700f66f451Sopenharmony_ci } 5710f66f451Sopenharmony_ci if (quote) *t++ = '"'; 5720f66f451Sopenharmony_ci *t = 0; 5730f66f451Sopenharmony_ci return result; 5740f66f451Sopenharmony_ci} 5750f66f451Sopenharmony_ci 5760f66f451Sopenharmony_cistatic void show_label(char *prefix, char *filename, struct stat *sb) 5770f66f451Sopenharmony_ci{ 5780f66f451Sopenharmony_ci char date[36]; 5790f66f451Sopenharmony_ci char *quoted_file; 5800f66f451Sopenharmony_ci 5810f66f451Sopenharmony_ci quoted_file = quote_filename(filename); 5820f66f451Sopenharmony_ci printf("%s %s\t%s\n", prefix, quoted_file, 5830f66f451Sopenharmony_ci format_iso_time(date, sizeof(date), &sb->st_mtim)); 5840f66f451Sopenharmony_ci free(quoted_file); 5850f66f451Sopenharmony_ci} 5860f66f451Sopenharmony_ci 5870f66f451Sopenharmony_cistatic void do_diff(char **files) 5880f66f451Sopenharmony_ci{ 5890f66f451Sopenharmony_ci 5900f66f451Sopenharmony_ci long i = 1, size = 1, x = 0, change = 0, ignore_white, 5910f66f451Sopenharmony_ci start1, end1, start2, end2; 5920f66f451Sopenharmony_ci struct diff *d; 5930f66f451Sopenharmony_ci struct arg_list *llist = TT.L_list; 5940f66f451Sopenharmony_ci int *J; 5950f66f451Sopenharmony_ci 5960f66f451Sopenharmony_ci TT.offset[0] = TT.offset[1] = NULL; 5970f66f451Sopenharmony_ci J = diff(files); 5980f66f451Sopenharmony_ci 5990f66f451Sopenharmony_ci if (!J) return; //No need to compare, have to status only 6000f66f451Sopenharmony_ci 6010f66f451Sopenharmony_ci d = xzalloc(size *sizeof(struct diff)); 6020f66f451Sopenharmony_ci do { 6030f66f451Sopenharmony_ci ignore_white = 0; 6040f66f451Sopenharmony_ci for (d[x].a = i; d[x].a <= file[0].len; d[x].a++) { 6050f66f451Sopenharmony_ci if (J[d[x].a] != (J[d[x].a - 1] + 1)) break; 6060f66f451Sopenharmony_ci else continue; 6070f66f451Sopenharmony_ci } 6080f66f451Sopenharmony_ci d[x].c = (J[d[x].a - 1] + 1); 6090f66f451Sopenharmony_ci 6100f66f451Sopenharmony_ci for (d[x].b = (d[x].a - 1); d[x].b <= file[0].len; d[x].b++) { 6110f66f451Sopenharmony_ci if (J[d[x].b + 1]) break; 6120f66f451Sopenharmony_ci else continue; 6130f66f451Sopenharmony_ci } 6140f66f451Sopenharmony_ci d[x].d = (J[d[x].b + 1] - 1); 6150f66f451Sopenharmony_ci 6160f66f451Sopenharmony_ci if ((toys.optflags & FLAG_B)) { 6170f66f451Sopenharmony_ci if (d[x].a <= d[x].b) { 6180f66f451Sopenharmony_ci if ((TT.offset[0][d[x].b] - TT.offset[0][d[x].a - 1]) 6190f66f451Sopenharmony_ci == (d[x].b - d[x].a + 1)) 6200f66f451Sopenharmony_ci ignore_white = 1; 6210f66f451Sopenharmony_ci } else if (d[x].c <= d[x].d){ 6220f66f451Sopenharmony_ci if ((TT.offset[1][d[x].d] - TT.offset[1][d[x].c - 1]) 6230f66f451Sopenharmony_ci == (d[x].d - d[x].c + 1)) 6240f66f451Sopenharmony_ci ignore_white = 1; 6250f66f451Sopenharmony_ci } 6260f66f451Sopenharmony_ci } 6270f66f451Sopenharmony_ci 6280f66f451Sopenharmony_ci if ((d[x].a <= d[x].b || d[x].c <= d[x].d) && !ignore_white) 6290f66f451Sopenharmony_ci change = 1; //is we have diff ? 6300f66f451Sopenharmony_ci 6310f66f451Sopenharmony_ci if (!ignore_white) d = xrealloc(d, (x + 2) *sizeof(struct diff)); 6320f66f451Sopenharmony_ci i = d[x].b + 1; 6330f66f451Sopenharmony_ci if (i > file[0].len) break; 6340f66f451Sopenharmony_ci J[d[x].b] = d[x].d; 6350f66f451Sopenharmony_ci if (!ignore_white) x++; 6360f66f451Sopenharmony_ci } while (i <= file[0].len); 6370f66f451Sopenharmony_ci 6380f66f451Sopenharmony_ci i = x+1; 6390f66f451Sopenharmony_ci TT.status = change; //update status, may change bcoz of -w etc. 6400f66f451Sopenharmony_ci 6410f66f451Sopenharmony_ci if (!(toys.optflags & FLAG_q) && change) { //start of !FLAG_q 6420f66f451Sopenharmony_ci if (toys.optflags & FLAG_color) printf("\033[1m"); 6430f66f451Sopenharmony_ci if (toys.optflags & FLAG_L) printf("--- %s\n", llist->arg); 6440f66f451Sopenharmony_ci else show_label("---", files[0], &(TT).st[0]); 6450f66f451Sopenharmony_ci if (((toys.optflags & FLAG_L) && !llist->next) || !(toys.optflags & FLAG_L)) 6460f66f451Sopenharmony_ci show_label("+++", files[1], &(TT).st[1]); 6470f66f451Sopenharmony_ci else { 6480f66f451Sopenharmony_ci while (llist->next) llist = llist->next; 6490f66f451Sopenharmony_ci printf("+++ %s\n", llist->arg); 6500f66f451Sopenharmony_ci } 6510f66f451Sopenharmony_ci if (toys.optflags & FLAG_color) printf("\033[0m"); 6520f66f451Sopenharmony_ci 6530f66f451Sopenharmony_ci struct diff *t, *ptr1 = d, *ptr2 = d; 6540f66f451Sopenharmony_ci while (i) { 6550f66f451Sopenharmony_ci long a,b; 6560f66f451Sopenharmony_ci 6570f66f451Sopenharmony_ci if (TT.ct > file[0].len) TT.ct = file[0].len; //trim context to file len. 6580f66f451Sopenharmony_ci if (ptr1->b < ptr1->a && ptr1->d < ptr1->c) { 6590f66f451Sopenharmony_ci i--; 6600f66f451Sopenharmony_ci continue; 6610f66f451Sopenharmony_ci } 6620f66f451Sopenharmony_ci //Handle the context stuff 6630f66f451Sopenharmony_ci a = ptr1->a; 6640f66f451Sopenharmony_ci b = ptr1->b; 6650f66f451Sopenharmony_ci 6660f66f451Sopenharmony_ci b = MIN(file[0].len, b); 6670f66f451Sopenharmony_ci if (i == x + 1) ptr1->suff = MAX(1,a - TT.ct); 6680f66f451Sopenharmony_ci else { 6690f66f451Sopenharmony_ci if ((ptr1 - 1)->prev >= (ptr1->a - TT.ct)) 6700f66f451Sopenharmony_ci ptr1->suff = (ptr1 - 1)->prev + 1; 6710f66f451Sopenharmony_ci else ptr1->suff = ptr1->a - TT.ct; 6720f66f451Sopenharmony_ci } 6730f66f451Sopenharmony_cicalc_ct: 6740f66f451Sopenharmony_ci if (i > 1) { 6750f66f451Sopenharmony_ci if ((ptr2->b + TT.ct) >= (ptr2 + 1)->a) { 6760f66f451Sopenharmony_ci ptr2++; 6770f66f451Sopenharmony_ci i--; 6780f66f451Sopenharmony_ci goto calc_ct; 6790f66f451Sopenharmony_ci } else ptr2->prev = ptr2->b + TT.ct; 6800f66f451Sopenharmony_ci } else ptr2->prev = ptr2->b; 6810f66f451Sopenharmony_ci start1 = (ptr2->prev - ptr1->suff + 1); 6820f66f451Sopenharmony_ci end1 = (start1 == 1) ? -1 : start1; 6830f66f451Sopenharmony_ci start2 = MAX(1, ptr1->c - (ptr1->a - ptr1->suff)); 6840f66f451Sopenharmony_ci end2 = ptr2->prev - ptr2->b + ptr2->d; 6850f66f451Sopenharmony_ci 6860f66f451Sopenharmony_ci if (toys.optflags & FLAG_color) printf("\033[36m"); 6870f66f451Sopenharmony_ci printf("@@ -%ld", start1 ? ptr1->suff: (ptr1->suff -1)); 6880f66f451Sopenharmony_ci if (end1 != -1) printf(",%ld ", ptr2->prev-ptr1->suff + 1); 6890f66f451Sopenharmony_ci else putchar(' '); 6900f66f451Sopenharmony_ci 6910f66f451Sopenharmony_ci printf("+%ld", (end2 - start2 + 1) ? start2: (start2 -1)); 6920f66f451Sopenharmony_ci if ((end2 - start2 +1) != 1) printf(",%ld ", (end2 - start2 +1)); 6930f66f451Sopenharmony_ci else putchar(' '); 6940f66f451Sopenharmony_ci printf("@@"); 6950f66f451Sopenharmony_ci if (toys.optflags & FLAG_color) printf("\033[0m"); 6960f66f451Sopenharmony_ci putchar('\n'); 6970f66f451Sopenharmony_ci 6980f66f451Sopenharmony_ci for (t = ptr1; t <= ptr2; t++) { 6990f66f451Sopenharmony_ci if (t== ptr1) print_diff(t->suff, t->a-1, ' ', TT.offset[0], file[0].fp); 7000f66f451Sopenharmony_ci print_diff(t->a, t->b, '-', TT.offset[0], file[0].fp); 7010f66f451Sopenharmony_ci print_diff(t->c, t->d, '+', TT.offset[1], file[1].fp); 7020f66f451Sopenharmony_ci if (t == ptr2) 7030f66f451Sopenharmony_ci print_diff(t->b+1, (t)->prev, ' ', TT.offset[0], file[0].fp); 7040f66f451Sopenharmony_ci else print_diff(t->b+1, (t+1)->a-1, ' ', TT.offset[0], file[0].fp); 7050f66f451Sopenharmony_ci } 7060f66f451Sopenharmony_ci ptr2++; 7070f66f451Sopenharmony_ci ptr1 = ptr2; 7080f66f451Sopenharmony_ci i--; 7090f66f451Sopenharmony_ci } //end of while 7100f66f451Sopenharmony_ci } //End of !FLAG_q 7110f66f451Sopenharmony_ci free(d); 7120f66f451Sopenharmony_ci free(J); 7130f66f451Sopenharmony_ci free(TT.offset[0]); 7140f66f451Sopenharmony_ci free(TT.offset[1]); 7150f66f451Sopenharmony_ci} 7160f66f451Sopenharmony_ci 7170f66f451Sopenharmony_cistatic void show_status(char **files) 7180f66f451Sopenharmony_ci{ 7190f66f451Sopenharmony_ci switch (TT.status) { 7200f66f451Sopenharmony_ci case SAME: 7210f66f451Sopenharmony_ci if (toys.optflags & FLAG_s) 7220f66f451Sopenharmony_ci printf("Files %s and %s are identical\n",files[0], files[1]); 7230f66f451Sopenharmony_ci break; 7240f66f451Sopenharmony_ci case DIFFER: 7250f66f451Sopenharmony_ci if ((toys.optflags & FLAG_q) || TT.is_binary) 7260f66f451Sopenharmony_ci printf("Files %s and %s differ\n",files[0], files[1]); 7270f66f451Sopenharmony_ci break; 7280f66f451Sopenharmony_ci } 7290f66f451Sopenharmony_ci} 7300f66f451Sopenharmony_ci 7310f66f451Sopenharmony_cistatic void create_empty_entry(int l , int r, int j) 7320f66f451Sopenharmony_ci{ 7330f66f451Sopenharmony_ci struct stat st[2]; 7340f66f451Sopenharmony_ci char *f[2], *path[2]; 7350f66f451Sopenharmony_ci int i; 7360f66f451Sopenharmony_ci 7370f66f451Sopenharmony_ci if (j > 0 && (toys.optflags & FLAG_N)) { 7380f66f451Sopenharmony_ci path[0] = concat_file_path(dir[0].list[0], dir[1].list[r] + TT.len[1]); 7390f66f451Sopenharmony_ci f[0] = "/dev/null"; 7400f66f451Sopenharmony_ci path[1] = f[1] = dir[1].list[r]; 7410f66f451Sopenharmony_ci stat(f[1], &st[0]); 7420f66f451Sopenharmony_ci st[1] = st[0]; 7430f66f451Sopenharmony_ci } 7440f66f451Sopenharmony_ci else if (j < 0 && (toys.optflags & FLAG_N)) { 7450f66f451Sopenharmony_ci path[1] = concat_file_path(dir[1].list[0], dir[0].list[l] + TT.len[0]); 7460f66f451Sopenharmony_ci f[1] = "/dev/null"; 7470f66f451Sopenharmony_ci path[0] = f[0] = dir[0].list[l]; 7480f66f451Sopenharmony_ci stat(f[0], &st[0]); 7490f66f451Sopenharmony_ci st[1] = st[0]; 7500f66f451Sopenharmony_ci } 7510f66f451Sopenharmony_ci 7520f66f451Sopenharmony_ci if (!j) { 7530f66f451Sopenharmony_ci for (i = 0; i < 2; i++) { 7540f66f451Sopenharmony_ci path[i] = f[i] = dir[i].list[!i ? l: r]; 7550f66f451Sopenharmony_ci stat(f[i], &st[i]); 7560f66f451Sopenharmony_ci } 7570f66f451Sopenharmony_ci } 7580f66f451Sopenharmony_ci 7590f66f451Sopenharmony_ci if (S_ISDIR(st[0].st_mode) && S_ISDIR(st[1].st_mode)) 7600f66f451Sopenharmony_ci printf("Common subdirectories: %s and %s\n", path[0], path[1]); 7610f66f451Sopenharmony_ci else if (!S_ISREG(st[0].st_mode) && !S_ISDIR(st[0].st_mode)) 7620f66f451Sopenharmony_ci printf("File %s is not a regular file or directory " 7630f66f451Sopenharmony_ci "and was skipped\n", path[0]); 7640f66f451Sopenharmony_ci else if (!S_ISREG(st[1].st_mode) && !S_ISDIR(st[1].st_mode)) 7650f66f451Sopenharmony_ci printf("File %s is not a regular file or directory " 7660f66f451Sopenharmony_ci "and was skipped\n", path[1]); 7670f66f451Sopenharmony_ci else if (S_ISDIR(st[0].st_mode) != S_ISDIR(st[1].st_mode)) { 7680f66f451Sopenharmony_ci if (S_ISDIR(st[0].st_mode)) 7690f66f451Sopenharmony_ci printf("File %s is a %s while file %s is a" 7700f66f451Sopenharmony_ci " %s\n", path[0], "directory", path[1], "regular file"); 7710f66f451Sopenharmony_ci else 7720f66f451Sopenharmony_ci printf("File %s is a %s while file %s is a" 7730f66f451Sopenharmony_ci " %s\n", path[0], "regular file", path[1], "directory"); 7740f66f451Sopenharmony_ci } else { 7750f66f451Sopenharmony_ci do_diff(f); 7760f66f451Sopenharmony_ci show_status(path); 7770f66f451Sopenharmony_ci if (file[0].fp) fclose(file[0].fp); 7780f66f451Sopenharmony_ci if (file[1].fp) fclose(file[1].fp); 7790f66f451Sopenharmony_ci } 7800f66f451Sopenharmony_ci 7810f66f451Sopenharmony_ci if ((toys.optflags & FLAG_N) && j) { 7820f66f451Sopenharmony_ci if (j > 0) free(path[0]); 7830f66f451Sopenharmony_ci else free(path[1]); 7840f66f451Sopenharmony_ci } 7850f66f451Sopenharmony_ci} 7860f66f451Sopenharmony_ci 7870f66f451Sopenharmony_cistatic void diff_dir(int *start) 7880f66f451Sopenharmony_ci{ 7890f66f451Sopenharmony_ci int l, r, j = 0; 7900f66f451Sopenharmony_ci 7910f66f451Sopenharmony_ci l = start[0]; //left side file start 7920f66f451Sopenharmony_ci r = start[1]; //right side file start 7930f66f451Sopenharmony_ci while (l < dir[0].nr_elm && r < dir[1].nr_elm) { 7940f66f451Sopenharmony_ci if ((j = strcmp ((dir[0].list[l] + TT.len[0]), 7950f66f451Sopenharmony_ci (dir[1].list[r] + TT.len[1]))) && !(toys.optflags & FLAG_N)) { 7960f66f451Sopenharmony_ci if (j > 0) { 7970f66f451Sopenharmony_ci printf ("Only in %s: %s\n", dir[1].list[0], dir[1].list[r] + TT.len[1]); 7980f66f451Sopenharmony_ci free(dir[1].list[r]); 7990f66f451Sopenharmony_ci r++; 8000f66f451Sopenharmony_ci } else { 8010f66f451Sopenharmony_ci printf ("Only in %s: %s\n", dir[0].list[0], dir[0].list[l] + TT.len[0]); 8020f66f451Sopenharmony_ci free(dir[0].list[l]); 8030f66f451Sopenharmony_ci l++; 8040f66f451Sopenharmony_ci } 8050f66f451Sopenharmony_ci TT.status = DIFFER; 8060f66f451Sopenharmony_ci } else { 8070f66f451Sopenharmony_ci create_empty_entry(l, r, j); //create non empty dirs/files if -N. 8080f66f451Sopenharmony_ci if (j > 0) { 8090f66f451Sopenharmony_ci free(dir[1].list[r]); 8100f66f451Sopenharmony_ci r++; 8110f66f451Sopenharmony_ci } else if (j < 0) { 8120f66f451Sopenharmony_ci free(dir[0].list[l]); 8130f66f451Sopenharmony_ci l++; 8140f66f451Sopenharmony_ci } else { 8150f66f451Sopenharmony_ci free(dir[1].list[r]); 8160f66f451Sopenharmony_ci free(dir[0].list[l]); 8170f66f451Sopenharmony_ci l++; 8180f66f451Sopenharmony_ci r++; 8190f66f451Sopenharmony_ci } 8200f66f451Sopenharmony_ci } 8210f66f451Sopenharmony_ci } 8220f66f451Sopenharmony_ci 8230f66f451Sopenharmony_ci if (l == dir[0].nr_elm) { 8240f66f451Sopenharmony_ci while (r < dir[1].nr_elm) { 8250f66f451Sopenharmony_ci if (!(toys.optflags & FLAG_N)) { 8260f66f451Sopenharmony_ci printf ("Only in %s: %s\n", dir[1].list[0], dir[1].list[r] + TT.len[1]); 8270f66f451Sopenharmony_ci TT.status = DIFFER; 8280f66f451Sopenharmony_ci } else create_empty_entry(l, r, 1); 8290f66f451Sopenharmony_ci free(dir[1].list[r]); 8300f66f451Sopenharmony_ci r++; 8310f66f451Sopenharmony_ci } 8320f66f451Sopenharmony_ci } else if (r == dir[1].nr_elm) { 8330f66f451Sopenharmony_ci while (l < dir[0].nr_elm) { 8340f66f451Sopenharmony_ci if (!(toys.optflags & FLAG_N)) { 8350f66f451Sopenharmony_ci printf ("Only in %s: %s\n", dir[0].list[0], dir[0].list[l] + TT.len[0]); 8360f66f451Sopenharmony_ci TT.status = DIFFER; 8370f66f451Sopenharmony_ci } else create_empty_entry(l, r, -1); 8380f66f451Sopenharmony_ci free(dir[0].list[l]); 8390f66f451Sopenharmony_ci l++; 8400f66f451Sopenharmony_ci } 8410f66f451Sopenharmony_ci } 8420f66f451Sopenharmony_ci free(dir[0].list[0]); //we are done, free root nodes too 8430f66f451Sopenharmony_ci free(dir[1].list[0]); 8440f66f451Sopenharmony_ci} 8450f66f451Sopenharmony_ci 8460f66f451Sopenharmony_civoid diff_main(void) 8470f66f451Sopenharmony_ci{ 8480f66f451Sopenharmony_ci int j = 0, k = 1, start[2] = {1, 1}; 8490f66f451Sopenharmony_ci char *files[2]; 8500f66f451Sopenharmony_ci 8510f66f451Sopenharmony_ci toys.exitval = 2; 8520f66f451Sopenharmony_ci 8530f66f451Sopenharmony_ci if ((toys.optflags & FLAG_color) && !isatty(1)) toys.optflags ^= FLAG_color; 8540f66f451Sopenharmony_ci 8550f66f451Sopenharmony_ci for (j = 0; j < 2; j++) { 8560f66f451Sopenharmony_ci files[j] = toys.optargs[j]; 8570f66f451Sopenharmony_ci if (IS_STDIN(files[j])) { 8580f66f451Sopenharmony_ci if (fstat(0, &TT.st[j]) == -1) 8590f66f451Sopenharmony_ci perror_exit("can't fstat %s", files[j]); 8600f66f451Sopenharmony_ci } else { 8610f66f451Sopenharmony_ci xstat(files[j], &TT.st[j]); 8620f66f451Sopenharmony_ci } 8630f66f451Sopenharmony_ci } 8640f66f451Sopenharmony_ci 8650f66f451Sopenharmony_ci if ((IS_STDIN(files[0]) || IS_STDIN(files[1])) 8660f66f451Sopenharmony_ci && (S_ISDIR(TT.st[0].st_mode) || S_ISDIR(TT.st[1].st_mode))) 8670f66f451Sopenharmony_ci error_exit("can't compare stdin to directory"); 8680f66f451Sopenharmony_ci 8690f66f451Sopenharmony_ci if ((TT.st[0].st_ino == TT.st[1].st_ino) //physicaly same device 8700f66f451Sopenharmony_ci && (TT.st[0].st_dev == TT.st[1].st_dev)) { 8710f66f451Sopenharmony_ci toys.exitval = 0; 8720f66f451Sopenharmony_ci return show_status(files); 8730f66f451Sopenharmony_ci } 8740f66f451Sopenharmony_ci 8750f66f451Sopenharmony_ci if (S_ISDIR(TT.st[0].st_mode) && S_ISDIR(TT.st[1].st_mode)) { 8760f66f451Sopenharmony_ci for (j = 0; j < 2; j++) { 8770f66f451Sopenharmony_ci memset(&dir[j], 0, sizeof(struct dir_t)); 8780f66f451Sopenharmony_ci dirtree_flagread(files[j], DIRTREE_SYMFOLLOW, list_dir); 8790f66f451Sopenharmony_ci dir[j].nr_elm = TT.size; //size updated in list_dir 8800f66f451Sopenharmony_ci qsort(&(dir[j].list[1]), (TT.size - 1), sizeof(char*), cmp); 8810f66f451Sopenharmony_ci 8820f66f451Sopenharmony_ci TT.len[j] = strlen(dir[j].list[0]); //calc root node len 8830f66f451Sopenharmony_ci TT.len[j] += (dir[j].list[0][TT.len[j] -1] != '/'); 8840f66f451Sopenharmony_ci 8850f66f451Sopenharmony_ci if (toys.optflags & FLAG_S) { 8860f66f451Sopenharmony_ci while (k < TT.size && strcmp(dir[j].list[k] + 8870f66f451Sopenharmony_ci TT.len[j], TT.start) < 0) { 8880f66f451Sopenharmony_ci start[j] += 1; 8890f66f451Sopenharmony_ci k++; 8900f66f451Sopenharmony_ci } 8910f66f451Sopenharmony_ci } 8920f66f451Sopenharmony_ci TT.dir_num++; 8930f66f451Sopenharmony_ci TT.size = 0; 8940f66f451Sopenharmony_ci k = 1; 8950f66f451Sopenharmony_ci } 8960f66f451Sopenharmony_ci diff_dir(start); 8970f66f451Sopenharmony_ci free(dir[0].list); //free array 8980f66f451Sopenharmony_ci free(dir[1].list); 8990f66f451Sopenharmony_ci } else { 9000f66f451Sopenharmony_ci if (S_ISDIR(TT.st[0].st_mode) || S_ISDIR(TT.st[1].st_mode)) { 9010f66f451Sopenharmony_ci int d = S_ISDIR(TT.st[0].st_mode); 9020f66f451Sopenharmony_ci char *slash = strrchr(files[d], '/'); 9030f66f451Sopenharmony_ci 9040f66f451Sopenharmony_ci files[1 - d] = concat_file_path(files[1 - d], slash ? slash + 1 : files[d]); 9050f66f451Sopenharmony_ci if ((stat(files[1 - d], &TT.st[1 - d])) == -1) 9060f66f451Sopenharmony_ci perror_exit("%s", files[1 - d]); 9070f66f451Sopenharmony_ci } 9080f66f451Sopenharmony_ci do_diff(files); 9090f66f451Sopenharmony_ci show_status(files); 9100f66f451Sopenharmony_ci if (file[0].fp) fclose(file[0].fp); 9110f66f451Sopenharmony_ci if (file[1].fp) fclose(file[1].fp); 9120f66f451Sopenharmony_ci } 9130f66f451Sopenharmony_ci toys.exitval = TT.status; //exit status will be the status 9140f66f451Sopenharmony_ci} 915