-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.c
1468 lines (1440 loc) · 41.6 KB
/
diff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <dirent.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include "dirList.h"
#include "fileList.h"
#include "pairStack.h"
#define FILES 8 //used to check if the type of the dirUnit being operated on is a file
#define DIRECTORY 4 //used to check if the type of the dirUnit being operated on is a directory
typedef struct lcs {//used for storing the indices of the lines from the files which belong to the common subsequence
int *ptr;
int count;
} lcs;
typedef struct hunk {//used in -c option to mark the beginning of the hunk, whether to print it or not and the operation
int start;
int printStatus;
char *operation;
} hunk;
/*below flags represent the status of the options specified in the command line, if mention in comman line then they
are reset*/
int bFlag = 0;
int iFlag = 0;
int wFlag = 0;
int yFlag = 0;
int cFlag = 0;
int tFlag = 0;
int rFlag = 0;
enum state {//used to determine the current state of operation in printing the diff of two files
START, ADD, DELETE, CHANGE
};
int iStrcmp(char *str1, char *str2) {//used if iFlag is 1(ignores uppercase and lowercase)
int i = 0, s1, s2;
while(str1[i] != '\0' && str2[i] != '\0') {
s1 = str1[i];
s2 = str2[i];
if((s1 == s2) || (s1 == (s2 - 32)) || (s1 == (s2 + 32)))
i++;
else
return s1 - s2;
}
if(str1[i] == '\0' && str2[i] == '\0')
return 0;
else
return str1[i] - str2[i];
}
int bStrcmp(char *str1, char *str2) {//used if bFlag is 1(ignores change in amount of existing white spaces)
int i = 0, j = 0;
while(str1[i] != '\0' && str2[j] != '\0') {
if((str1[i] == str2[j]) || (str1[i] == '\t' && str2[j] == ' ') || (str1[i] == ' ' && str2[j] == '\t')) {
if(str1[i] == ' ' || str1[i] == '\t') {
while(str1[i] == ' ' || str1[i] == '\t')
i++;
while(str2[j] == ' ' || str2[j] == '\t')
j++;
}
else {
i++;
j++;
}
}
else
return str1[i] - str2[j];
}
if(str1[i] == '\0' && str2[j] == '\0')
return 0;
else
return str1[i] - str2[j];
}
int wStrcmp(char *str1, char *str2) {//used if wFlag is 1(ignores all the white spaces)
int i = 0, j = 0;
while(str1[i] != '\0' && str2[j] != '\0') {
while(str1[i] == ' ' || str1[i] == '\t')
i++;
while(str2[j] == ' ' || str2[j] == '\t')
j++;
if(str1[i] == str2[j]) {
i++;
j++;
}
else
return str1[i] - str2[j];
}
if(str1[i] == '\0' && str2[j] == '\0')
return 0;
else
return str1[i] - str2[j];
}
int iwStrcmp(char *str1, char *str2) {//used if iFlag and wFlag are both 1(ignores case and all white spaces)
int i = 0, j = 0, s1, s2;
while(str1[i] != '\0' && str2[j] != '\0') {
while(str1[i] == ' ' || str1[i] == '\t')
i++;
while(str2[j] == ' ' || str2[j] == '\t')
j++;
s1 = str1[i];
s2 = str2[j];
if((s1 == s2) || (s1 == (s2 - 32)) || (s1 == (s2 + 32))) {
i++;
j++;
}
else
return s1 - s2;
}
if(str1[i] == '\0' && str2[j] == '\0')
return 0;
else
return str1[i] - str2[j];
}
int ibStrcmp(char *str1, char *str2) {//used if iFlag and bFlag are both 1(ignores case and change in the white spaces)
int i = 0, j = 0, s1, s2;
while(str1[i] != '\0' && str2[j] != '\0') {
s1 = str1[i];
s2 = str2[j];
if((s1 == s2) || (s1 == (s2 - 32)) || (s1 == (s2 + 32)) || (str1[i] == ' ' && str2[j] == '\t') || (str1[i] == '\t' && str2[j] == ' ')) {
if(str1[i] == ' ' || str1[i] == '\t') {
while(str1[i] == ' ' || str1[i] == '\t')
i++;
while(str2[j] == ' ' || str2[j] == '\t')
j++;
}
else {
i++;
j++;
}
}
else
return s1 - s2;
}
if(str1[i] == '\0' && str2[j] == '\0')
return 0;
else
return str1[i] - str2[j];
}
int getLine(int rfd, char *str) {//reads the line from the specified file char by char and returns the number of char read and returns INT_MAX in case of binary character
int i = 0;
while(read(rfd, &str[i], 1)) {
if(str[i] == '\n') {
str[i + 1] = '\0';
i = i + 1;
break;
}
else {
if(str[i] == '\032' || str[i] == '\033')//check for binary content
return INT_MAX;
i++;
}
}
str[i] = '\0';
return i;
}
int getDirectory(char *path, dirList *dir) {//function return 1 for successful read else returns 0 if dir not found
dirUnit temp;
struct dirent *de;//directory entry structure
DIR *dp = opendir(path);//directory pointer
if(dp == NULL)
return 0;
if(path[0] == '/' && path[1] == '/')//in case of root folder there is no need of '/' again
path++;
while((de = readdir(dp)) != NULL) {
if(strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) {
strcpy(temp.name, de->d_name);
strcpy(temp.path, path);
temp.type = de->d_type;
dappend(dir, temp);
}
}
closedir(dp);
return 1;
}
int search(lcs *lcs, int num) {//used to find the whether the current index is in the lcs array if yes return 1 else 0
//uses binary search algorithm
/*Here linear search algorithm would do better, as the lines are accessed in a sequence. However it is found
to be expensive if the line that is being found is not in the lcs array*/
int mid, lo = 0, hi = lcs->count - 1, flag = 0;
while((lo != hi + 1) && (lo - 1 != hi)) {
mid = (lo + hi) / 2;
if(lcs->ptr[mid] == num) {
flag = 1;
break;
}
else if(num < lcs->ptr[mid])
hi = mid - 1;
else if(num > lcs->ptr[mid])
lo = mid + 1;
}
return flag;
}
void expandTabs(char *str) {//converts the tabs in the string to spaces till the next tabstop
int i = 0, j, numOfTabs, length = strlen(str);
while(i < length) {
if(str[i] == '\t') {
numOfTabs = 8 - (i % 8);//finding the number of spaces to next tabstop
j = length - 1;
while(j > i) {
str[j + numOfTabs - 1] = str[j];
j--;
}
j = 0;
while(j < numOfTabs) {
str[i] = ' ';
i++;
j++;
}
length = length + numOfTabs - 1;
}
else
i++;
}
str[i] = '\0';
}
int tabStrlen(char *str) {//returns the string length considering the tabs in the string are expanded
int count = 0, tabCount, shift = 0, i = 0;
while(str[i] != '\0') {
if(str[i] == '\t') {
tabCount = 8 - ((i + shift) % 8);
tabCount--;
count = count + tabCount;
shift = shift + tabCount;
}
i++;
count++;
}
return count;
}
void myTruncate(char *str) {//truncates the string so as to fit in 61 char space for -y option
int i = 0, count = 0, shift = 0, tabCount = 0;
while(count <= 61 && str[i] != '\0') {
if(str[i] == '\t') {
tabCount = 8 - ((i + shift) % 8);
tabCount--;
count = count + tabCount;
shift = shift + tabCount;
}
count++;
i++;
}
str[i - 1] = '\0';
}
void conflictError() {//used when both -y and -c options are passed by the user
printf("diff: conflicting output style options\n");
}
//the following function uses the myer's diff algorithm to find the shortest edit script for two files
void findCommonSubsequence(fileList file1, fileList file2, lcs *lcs1, lcs *lcs2) {
pair p;
pairStack s;
sinit(&s);
char *tempStr1, *tempStr2;
int n = flength(&file1), m = flength(&file2), max = n + m, x, xCurr, xPrev, y, yCurr, yPrev, d, k, lcsCount = 0;
//d represents the number of moves completed and k represents the diagonal lines on the edit graph (k = x - y)
int **editGraph, *kMaxReal, *kMax, endFlag = 0;
//editGraph is the 2d array (d v/s k) storing the max value of (x) on each k line for corresponding d
//kMaxReal is the array which stores the maxmimum value of x on each k line, indices represent the k lines
//k lines start from -d to +d so we have a pointer at the middle of the kMaxReal array which is kMax
kMaxReal = (int *)malloc(sizeof(int) * (2 * max + 1));
if(kMaxReal == NULL) {
perror("diff");
exit(errno);
}
editGraph = (int **)malloc(sizeof(int *) * (max + 1));
if(editGraph == NULL) {
perror("diff");
exit(errno);
}
for(d = 0; d <= max; d++) {
editGraph[d] = (int *)malloc(sizeof(int) * (2 * d + 1));
if(editGraph[d] == NULL) {
perror("diff");
exit(errno);
}
}
kMax = kMaxReal + m + n;
kMax[1] = 0;
//Shortest edit script length will be stored in 'd' and the value of 'k' will be set accordingly to n - m
/*The algorithm finds the maximum value of (x) on each k line, it starts from the (0, 0) position and
traverses the editGraph till it reaches the opposite point i.e. (n, m)*/
for(d = 0; d <= max; d++) {
for(k = -d; k <= d; k = k + 2) {
if(k == -d || (k != d && kMax[k - 1] < kMax[k + 1]))//move downward hence x remains the same
//move downward decreases the value of the k line by 1 hence take value of x from (k + 1)
x = kMax[k + 1];
else//move right hence increase the value of k by 1
//move in right increases the value of the k line by 1 hence take value of x from (k - 1)
x = kMax[k - 1] + 1;
y = x - k;
while(x < n && y < m){
tempStr1 = fgetline(&file1, x);
tempStr2 = fgetline(&file2, y);
if(!strcmp(tempStr1, tempStr2) ||
(!iStrcmp(tempStr1, tempStr2) && iFlag) ||
(!wStrcmp(tempStr1, tempStr2) && wFlag) ||
(!bStrcmp(tempStr1, tempStr2) && bFlag) ||
(!iwStrcmp(tempStr1, tempStr2) && iFlag && wFlag) ||
(!ibStrcmp(tempStr1, tempStr2) && iFlag && bFlag)) {
x++;
y++;
}
else
break;
}
kMax[k] = x;
editGraph[d][k + d] = kMax[k];
if(x >= n && y >= m) {
endFlag = 1;
break;
}
}
if(endFlag)
break;
}
//Tracing the shortest path for the common lines from (n, m) to (0, 0)
while(k != 0 || d != 0) {
if(k == -d || (k != d && editGraph[d - 1][k + d] > editGraph[d - 1][k + d - 2])) {
xCurr = editGraph[d][k + d];
yCurr = xCurr - k;
xPrev = editGraph[d - 1][k + d];
yPrev = xPrev - (k + 1);
while(xCurr != xPrev || yCurr != yPrev + 1) {
p.i = xCurr - 1;
p.j = yCurr - 1;
push(&s, p);
xCurr--;
yCurr--;
lcsCount++;
}
d--;
k++;
}
else {
xCurr = editGraph[d][k + d];
yCurr = xCurr - k;
xPrev = editGraph[d - 1][k + d - 2];
yPrev = xPrev - (k - 1);
while(xCurr != xPrev + 1 || yCurr != yPrev) {
p.i = xCurr - 1;
p.j = yCurr - 1;
push(&s, p);
xCurr--;
yCurr--;
lcsCount++;
}
d--;
k--;
}
}
//Finding the SNAKE from (0, 0) if any
xCurr = editGraph[d][k + d];
yCurr = xCurr - k;
while(xCurr != 0 || yCurr != 0) {
p.i = xCurr - 1;
p.j = yCurr - 1;
push(&s, p);
xCurr--;
yCurr--;
lcsCount++;
}
if(lcsCount != 0) {
lcs1->ptr = (int*)malloc(sizeof(int) * lcsCount);
if(lcs1->ptr == NULL) {
perror("diff");
exit(errno);
}
lcs2->ptr = (int*)malloc(sizeof(int) * lcsCount);
if(lcs2->ptr == NULL) {
perror("diff");
exit(errno);
}
}
else
lcs1->ptr = lcs2->ptr = NULL;//if no common subsequence
while(!isempty(&s)) {//popping from the stack and storing in the lcs structure
p = pop(&s);
lcs1->ptr[lcs1->count++] = p.i;//common lines for file1
lcs2->ptr[lcs2->count++] = p.j;//common lines for file2
}
free(kMaxReal);
for(d = 0; d <= max; d++)
free(editGraph[d]);
free(editGraph);
}
//set of functions for files' diff
//PrintDiff prints the diff in the normal manner
void defaultPrintDiff(fileList file1, fileList file2, lcs *lcs1, lcs *lcs2) {
enum state STATE = START;
pair m;//used to store the pair of integers which denote the start of the hunk in the two files
int i = 0, j = 0, k, isLineOneInLcs = 0, isLineTwoInLcs = 0, file1Len = flength(&file1), file2Len = flength(&file2);
char *temp;
while(i < file1Len || j < file2Len || STATE != START) {
//setting first flag
if(i != file1Len)
isLineOneInLcs = search(lcs1, i);
else
isLineOneInLcs = 1;
//setting second flag
if(j != file2Len)
isLineTwoInLcs = search(lcs2, j);
else
isLineTwoInLcs = 1;
//the program can be in 4 possible states - ADD, DELETE, CHANGE, or SAME lines here given by START
switch(STATE) {
case START:
if(isLineOneInLcs && !isLineTwoInLcs) {//add
m.i = i;
m.j = j;
if(j < file2Len)
j++;
STATE = ADD;
}
else if(!isLineOneInLcs && isLineTwoInLcs) {//delete
m.i = i;
m.j = j;
if(i < file1Len)
i++;
STATE = DELETE;
}
else if(!isLineOneInLcs && !isLineTwoInLcs) {//change
m.i = i;
m.j = j;
if(i < file1Len)
i++;
if(j < file2Len)
j++;
STATE = CHANGE;
}
else if(isLineOneInLcs && isLineTwoInLcs) {//same
if(i < file1Len)
i++;
if(j < file2Len)
j++;
STATE = START;
}
break;
case ADD:
if(i == file1Len && j == file2Len) {//end of file
if(j == m.j + 1)
printf("%da%d\n", m.i, j);//single line added
else
printf("%da%d,%d\n", m.i, (m.j + 1), j);//multiple lines added
for(k = m.j; k < j; k++) {
temp = fgetline(&file2, k);
if(tFlag)//checking for -t option
expandTabs(temp);
printf("> %s", temp);
if(k == file2Len - 1) {//incomplete lines
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
STATE = START;
}
else if(isLineOneInLcs && !isLineTwoInLcs) {//add
if(j < file2Len)
j++;
STATE = ADD;
}
else if(isLineOneInLcs && isLineTwoInLcs) {//same
if(j == m.j + 1)
printf("%da%d\n", m.i, j);//single addition
else
printf("%da%d,%d\n", m.i, (m.j + 1), j);//multiple lines added
for(k = m.j; k < j; k++) {
temp = fgetline(&file2, k);
if(tFlag)//checking for t option
expandTabs(temp);
printf("> %s", temp);
if(k == file2Len - 1) {
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
STATE = START;
}
break;
case DELETE:
if(i == file1Len && j == file2Len) {//end of file
if(i == m.i + 1)
printf("%dd%d\n", i, m.j);//single line delete
else
printf("%d,%dd%d\n", m.i + 1, i, m. j);//multiple lines deleted
for(k = m.i; k < i; k++) {
temp = fgetline(&file1, k);
if(tFlag)//checking for t option
expandTabs(temp);
printf("< %s", temp);
if(k == file1Len - 1) {//incomplete line check
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
STATE = START;
}
else if(!isLineOneInLcs && isLineTwoInLcs) {//delete
if(i < file1Len)
i++;
STATE = DELETE;
}
else if(isLineOneInLcs && isLineTwoInLcs) {//same
if(i == m.i + 1)
printf("%dd%d\n", i, m.j);//single line delete
else
printf("%d,%dd%d\n", m.i + 1, i, m. j);//multiple lines delete
for(k = m.i; k < i; k++) {
temp = fgetline(&file1, k);
if(tFlag)//checking for t option
expandTabs(temp);
printf("< %s", temp);
if(k == file1Len - 1) {//incomplete line check
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
STATE = START;
}
break;
case CHANGE:
if(i == file1Len && j == file2Len) {//end of file
if(i == m.i + 1 && j == m.j + 1)
printf("%dc%d\n", i, j);//one to one change
else if(i == m.i + 1 && j != m.j + 1)
printf("%dc%d,%d\n", i, m.j + 1, j);//one to many change
else if(i != m.i + 1 && j == m.j + 1)
printf("%d,%dc%d\n",m.i + 1, i, j);//many to one change
else
printf("%d,%dc%d,%d\n", m.i + 1, i, m.j + 1, j);//many to many change
for(k = m.i; k < i; k++) {
temp = fgetline(&file1, k);
if(tFlag)//checking for t option
expandTabs(temp);
printf("< %s", temp);
if(k == file1Len - 1) {//incomplete lines check
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
printf("---\n");
for(k = m.j; k < j; k++) {
temp = fgetline(&file2, k);
if(tFlag)//checking for t option
expandTabs(temp);
printf("> %s", temp);
if(k == file2Len - 1) {//incomplete lines check
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
STATE = START;
}
else if(!isLineOneInLcs && !isLineTwoInLcs) {//change
if(i < file1Len)
i++;
if(j < file2Len)
j++;
STATE = CHANGE;
}
else if(isLineOneInLcs && !isLineTwoInLcs) {//add in change from the second file
if(j < file2Len)
j++;
STATE = CHANGE;
}
else if(!isLineOneInLcs && isLineTwoInLcs) {//delete in change from the first file
if(i < file1Len)
i++;
STATE = CHANGE;
}
else if(isLineOneInLcs && isLineTwoInLcs) {//same
if(i == m.i + 1 && j == m.j + 1)
printf("%dc%d\n", i, j);//one to one change
else if(i == m.i + 1 && j != m.j + 1)
printf("%dc%d,%d\n", i, m.j + 1, j);//one to many change
else if(i != m.i + 1 && j == m.j + 1)
printf("%d,%dc%d\n",m.i + 1, i, j);//many to one change
else
printf("%d,%dc%d,%d\n", m.i + 1, i, m.j + 1, j);//many to many change
for(k = m.i; k < i; k++) {
temp = fgetline(&file1, k);
if(tFlag)//checking for t option
expandTabs(temp);
printf("< %s", temp);
if(k == file1Len - 1) {//incomplete lines check
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
printf("---\n");
for(k = m.j; k < j; k++) {
temp = fgetline(&file2, k);
if(tFlag)//checking for t option
expandTabs(temp);
printf("> %s", fgetline(&file2, k));
if(k == file2Len - 1) {//incomplete lines check
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
STATE = START;
}
break;
}
}
}
//yPrintDiff prints the diff in side by side format
void yPrintDiff(fileList file1, fileList file2, lcs *lcs1, lcs *lcs2) {
int i = 0, j = 0, isLineOneInLcs = 0, isLineTwoInLcs = 0, wordMax, newline1, newline2, file1Len = flength(&file1), file2Len = flength(&file2), str1Len, str2Len, tab1Len, tab2Len;
char *tempStr1, *tempStr2;
if(tFlag)
wordMax = 64;//of which 63 are printed
else
wordMax = 62;//of which 61 are printed
while(i < file1Len || j < file2Len) {
//setting first flag
if(i != file1Len)
isLineOneInLcs = search(lcs1, i);
else
isLineOneInLcs = 1;
//setting second flag
if(j != file2Len)
isLineTwoInLcs = search(lcs2, j);
else
isLineTwoInLcs = 1;
//finding the operation
if(isLineOneInLcs && !isLineTwoInLcs) {//add
tempStr2 = fgetline(&file2, j);
str2Len = strlen(tempStr2);
//check newline for the line
if(tempStr2[str2Len - 1] == '\n') {
newline2 = 1;
tempStr2[str2Len - 1] = '\0';
str2Len--;
}
else
newline2 = 0;
if(tFlag) {
expandTabs(tempStr2);
tab2Len = tabStrlen(tempStr2);
str2Len = tab2Len;
if(tab2Len >= wordMax)//if chars greater than 63
tempStr2[wordMax - 1] = '\0';
}
else {
tab2Len = tabStrlen(tempStr2);
if(tab2Len >= wordMax)//if chars greater than 61
myTruncate(tempStr2);
}
//print the operation
printf("%*s> %c", -wordMax, "\0", (tFlag)? ' ': '\0');
//print the second line only
printf("%s", tempStr2);
if(newline2)
printf("\n");
if(j < file2Len)
j++;
}
else if(!isLineOneInLcs && isLineTwoInLcs) {//delete
tempStr1 = fgetline(&file1, i);
str1Len = strlen(tempStr1);
//check newline for the line
if(tempStr1[str1Len - 1] == '\n') {
newline1 = 1;
tempStr1[str1Len - 1] = '\0';
str1Len--;
}
else
newline1 = 0;
if(tFlag) {
expandTabs(tempStr1);
tab1Len = tabStrlen(tempStr1);
str1Len = tab1Len;
if(tab1Len >= wordMax)//if chars greater than 63
tempStr1[wordMax - 1] = '\0';
}
else {
tab1Len = tabStrlen(tempStr1);
if(tab1Len >= wordMax) {//if chars greater than 61
myTruncate(tempStr1);
tab1Len = tabStrlen(tempStr1);//recalculate the expanded length which will be <= 61
str1Len = strlen(tempStr1);
}
}
//print the first line only with operation
printf("%*s<", -(wordMax - tab1Len + str1Len), tempStr1);
if(newline1)
printf("\n");
if(i < file1Len)
i++;
}
else if(!isLineOneInLcs && !isLineTwoInLcs) {//change
tempStr1 = fgetline(&file1, i);
tempStr2 = fgetline(&file2, j);
str1Len = strlen(tempStr1);
str2Len = strlen(tempStr2);
//check newline for first line
if(tempStr1[str1Len - 1] == '\n') {
newline1 = 1;
tempStr1[str1Len - 1] = '\0';
str1Len--;
}
else
newline1 = 0;
//check newline for second line
if(tempStr2[str2Len - 1] == '\n') {
newline2 = 1;
tempStr2[str2Len - 1] = '\0';
str2Len--;
}
else
newline2 = 0;
if(tFlag) {
expandTabs(tempStr1);
expandTabs(tempStr2);
tab1Len = tabStrlen(tempStr1);
str1Len = tab1Len;
tab2Len = tabStrlen(tempStr2);
str2Len = tab2Len;
if(tab1Len >= wordMax)//if chars greater than 63
tempStr1[wordMax - 1] = '\0';
if(tab2Len >= wordMax)//if chars greater than 63
tempStr2[wordMax - 1] = '\0';
}
else {
tab1Len = tabStrlen(tempStr1);
tab2Len = tabStrlen(tempStr2);
if(tab1Len >= wordMax) {//if chars greater than 61
myTruncate(tempStr1);
tab1Len = tabStrlen(tempStr1);
str1Len = strlen(tempStr1);
}
if(tab2Len >= wordMax)//if chars greater than 61
myTruncate(tempStr2);
}
//print the first line
printf("%*s", -(wordMax - tab1Len + str1Len), tempStr1);
//print the operation
if(newline1 && !newline2)
printf("/ %c", (tFlag)? ' ': '\0');
else if(!newline1 && newline2)
printf("\\ %c", (tFlag)? ' ': '\0');
else
printf("| %c", (tFlag)? ' ': '\0');
//print the second line
printf("%s", tempStr2);
if(newline1 || newline2)
printf("\n");
if(i < file1Len)
i++;
if(j < file2Len)
j++;
}
else if(isLineOneInLcs && isLineTwoInLcs) {
tempStr1 = fgetline(&file1, i);
tempStr2 = fgetline(&file2, j);
str1Len = strlen(tempStr1);
str2Len = strlen(tempStr2);
//check newline for first line
if(tempStr1[str1Len - 1] == '\n') {
newline1 = 1;
tempStr1[str1Len - 1] = '\0';
str1Len--;
}
else
newline1 = 0;
//check newline for second line
if(tempStr2[str2Len - 1] == '\n') {
newline2 = 1;
tempStr2[str2Len - 1] = '\0';
str2Len--;
}
else
newline2 = 0;
if(tFlag) {
expandTabs(tempStr1);
expandTabs(tempStr2);
tab1Len = tabStrlen(tempStr1);
str1Len = tab1Len;
tab2Len = tabStrlen(tempStr2);
str2Len = tab2Len;
if(tab1Len >= wordMax)//if chars greater than 63
tempStr1[wordMax - 1] = '\0';
if(tab2Len >= wordMax)//if chars greater than 63
tempStr2[wordMax - 1] = '\0';
}
else {
tab1Len = tabStrlen(tempStr1);
tab2Len = tabStrlen(tempStr2);
if(tab1Len >= wordMax) {//if chars greater than 61
myTruncate(tempStr1);
tab1Len = tabStrlen(tempStr1);
str1Len = strlen(tempStr1);
}
if(tab2Len >= wordMax)//if chars greater than 61
myTruncate(tempStr2);
}
//print the first line
printf("%*s %c", -(wordMax - tab1Len + str1Len), tempStr1, (tFlag)? ' ': '\0');
//print the second line
printf("%s", tempStr2);
if(newline1 || newline2)
printf("\n");
if(i < file1Len)
i++;
if(j < file2Len)
j++;
}
}
}
//cPrintDiff prints the diff in context format
void cPrintDiff(fileList file1, fileList file2, lcs *lcs1, lcs *lcs2, char *filename1, char *filename2) {
enum state STATE = START;
int file1Len = flength(&file1), file2Len = flength(&file2), headerFlag = 0, i = 0, j = 0, k, isLineOneInLcs = 0, isLineTwoInLcs = 0, equalCount = 0;
char *temp;
hunk h1, h2;
//initialization of the two hunks
h1.start = h2.start = h1.printStatus = h2.printStatus = 0;
if(file1Len != 0) {//malloc only a file having non-zero number of lines
h1.operation = (char *)malloc(sizeof(char) * file1Len);
if(h1.operation == NULL) {
perror("diff");
exit(errno);
}
}
if(file2Len != 0) {//malloc only a file having non-zero number of lines
h2.operation = (char *)malloc(sizeof(char) * file2Len);
if(h2.operation == NULL) {
perror("diff");
exit(errno);
}
}
//to print the timestamp at the beginning of the diff
struct stat filestat1, filestat2;
stat(filename1, &filestat1);
stat(filename2, &filestat2);
while(i < file1Len || j < file2Len || STATE != START) {
//setting first flag
if(i != file1Len)
isLineOneInLcs = search(lcs1, i);
else
isLineOneInLcs = 1;
//setting second flag
if(j != file2Len)
isLineTwoInLcs = search(lcs2, j);
else
isLineTwoInLcs = 1;
switch(STATE) {
case START:
if(isLineOneInLcs && !isLineTwoInLcs) {//add
h2.printStatus = 1;
h2.operation[j] = '+';
equalCount = 0;
if(j < file2Len)
j++;
STATE = ADD;
}
else if(!isLineOneInLcs && isLineTwoInLcs) {//delete
h1.printStatus = 1;
h1.operation[i] = '-';
equalCount = 0;
if(i < file1Len)
i++;
STATE = DELETE;
}
else if(!isLineOneInLcs && !isLineTwoInLcs) {//change
h1.printStatus = 1;
h2.printStatus = 1;
h1.operation[i] = '!';
h2.operation[j] = '!';
equalCount = 0;
if(i < file1Len)
i++;
if(j < file2Len)
j++;
STATE = CHANGE;
}
else if(isLineOneInLcs && isLineTwoInLcs) {//same
h1.operation[i] = ' ';
h2.operation[j] = ' ';
equalCount++;
if(i < file1Len)
i++;
if(j < file2Len)
j++;
if(i == file1Len && j == file2Len) {//if we reach the end of file then print the hunk
if(!headerFlag) {
printf("*** %s\t%s", filename1, ctime(&filestat1.st_mtime));
printf("--- %s\t%s", filename2, ctime(&filestat2.st_mtime));
headerFlag = 1;
}
if(h1.printStatus || h2.printStatus) {//print the hunk only if they have differences
if(equalCount > 3) {//dont print the lines below 3 equal lines so change the values of 'i' & 'j'
i = i - equalCount + 3;
j = j - equalCount + 3;
}
printf("***************\n");
if(h1.start + 1 == i || i == 0)
printf("*** %d ****\n", i);
else
printf("*** %d,%d ****\n", h1.start + 1, i);
if(h1.printStatus) {
for(k = h1.start; k < i; k++) {
temp = fgetline(&file1, k);
//print the operation
printf("%c ", h1.operation[k]);
if(tFlag)//checking for t option
expandTabs(temp);
//print the line
printf("%s", temp);
if(k == file1Len - 1) {//incomplete lines
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
}
if(h2.start + 1 == j || j == 0)
printf("--- %d ----\n", j);
else
printf("--- %d,%d ----\n", h2.start + 1, j);
if(h2.printStatus) {
for(k = h2.start; k < j; k++) {
temp = fgetline(&file2, k);
//print the operation
printf("%c ", h2.operation[k]);
if(tFlag)//checking for t option
expandTabs(temp);
printf("%s", temp);
if(k == file2Len - 1) {//incomplete lines
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
}
//reset the values of 'i' and 'j' which were changed above
if(equalCount > 3) {
i = i + equalCount - 3;
j = j + equalCount - 3;
}
}
}
/*hunk has 3 common lines at top so if we get more common lines than 3 we increment
the start of the hunk*/
else if((equalCount > 3) && (!h1.printStatus && !h2.printStatus)) {
h1.start++;
h2.start++;
equalCount--;
}
/*The hunks are divided only if we get more than 6 common lines between the
two differences, and the program divides the hunk as soon as it gets 7 common
lines*/
else if(equalCount > 6 && (h1.printStatus || h2.printStatus)) {
if(!headerFlag) {
printf("*** %s\t%s", filename1, ctime(&filestat1.st_mtime));
printf("--- %s\t%s", filename2, ctime(&filestat2.st_mtime));
headerFlag = 1;
}
if(h1.printStatus || h2.printStatus) {
printf("***************\n");
printf("*** %d,%d ****\n", h1.start + 1, i - 4);
if(h1.printStatus) {
for(k = h1.start; k < i - 4; k++) {
temp = fgetline(&file1, k);
//print the operation
printf("%c ", h1.operation[k]);
if(tFlag)//checking for t option
expandTabs(temp);
printf("%s", temp);
if(k == file1Len - 1) {//incomplete lines
if(temp[strlen(temp) - 1] != '\n')
printf("\n\\ No newline at end of file\n");
}
}
h1.printStatus = 0;//reset condition for the hunk
}
printf("--- %d,%d ----\n", h2.start + 1, j - 4);
if(h2.printStatus) {