File: fc.c

package info (click to toggle)
dosemu-freedos 1%3A0.0.b9r5a%2Betch.1-0etch1
  • links: PTS
  • area: contrib
  • in suites: etch
  • size: 19,744 kB
  • ctags: 23,279
  • sloc: ansic: 143,864; asm: 20,397; makefile: 3,868; perl: 1,106; yacc: 690; sh: 553; pascal: 297; xml: 150; cpp: 67
file content (1160 lines) | stat: -rw-r--r-- 35,629 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
/*			FC: FreeDOS File compare

This program implements Paul Heckel's algorithm from the Communications
of the Association for Computing Machinery, April 1978 for detecting the
differences between two files.
This algorithm has the advantage over more commonly used compare algorithms
that it is fast and can detect differences of an arbitrary number of lines.
For most applications the algorithm isolates differences similar to those
isolated by the longest common subsequence.
******************************************************************************
A design limitation of this program is that only the first
MAX_LINES lines are compared; the remaining lines are ignored.
******************************************************************************
Version 2.01: Fixed a conceptual bug in MatchNames
	      Added the /S switch and relevant routines
Version 2.10: Fixed a stupid bug in ScanArguments in case of
	      a comparison of just two files
	      Added cats for easier internationalisation
Version 2.20: Added the /LBn and /nnn switches
	      Used CRC15 as hashing function
Version 2.21: Used the current DOS code page for the upcase routine
Version 2.22: Bug fix for ASCII compare
Version 3.00: Bug fix for the /S switch: failed because of filenames
	      used as directory name
	      Added automatic support for long filenames
	      Added the /R switch for a final report (always active when using /S)
	      Added the /Q switch for suppressing the list of differences
	      Moved some routines to FCTOOLS.C
Version 3.01: Used the simpler kitten instead of cats
Version 3.02: Bug fix: file not found when the target pattern extension was
	      shorter than the source pattern extension
	      Bug fix: ASCII compare result display routine rewritten to avoid
	      an endless loop condition
	      Added the /U switch to show the source files who don't have
	      a correspondent target file
*************************************************************************** */

#define USE_KITTEN	/* Define this to use kitten */

#if !defined(__LARGE__)
  #error Must be compiled with the LARGE model.
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <io.h>
#include <sys\stat.h>
#ifdef USE_KITTEN
  #include "Kitten.h"
#endif
#include "FCTools.h"
/* ------------------------------------------------------------------------ */
#define MAX_LINES			32765
#define DEFAULT_LINES_FOR_RESYNC	2
#define DEFAULT_BINARY_MAX_DIFF		20  /* Bytes */

/* Must be signed and able to count at least up to MAX_LINES */
typedef signed int line_counter;
/* ------------------------------------------------------------------------ */
/* Values for ERRORLEVEL */
#define EL_NO_DIFF		0
#define EL_DIFFERENT		1
#define EL_PARAM_ERROR		2
#define EL_NO_FILE_ERROR	3
#define EL_OPEN_ERROR		4
/* ------------------------------------------------------------------------ */
#define TAB_SIZE	8
#define BITS_PER_BYTE	8
/* ------------------------------------------------------------------------ */
/* Option flags */
struct	{
  int Quiet		:1;
  int Report		:1;
  int ScanSubdirs	:1;
  int LineNumber	:1;
  int IgnoreCase	:1;
  int PackSpace		:1;
  int NoTabExpand	:1;
  int Brief		:1;
  int ShowUnmatched	:1;
} OptFlags = {0};
/* ------------------------------------------------------------------------ */
#define FILE_SPECS	2	/* Two files to be compared each time */
#define FIRST_FILE	0
#define SECOND_FILE	1
/* ------------------------------------------------------------------------ */
#define STANDARD_CONTEXT	1   /* Lines */
#define NO_CONTEXT		0   /* Lines */
/* ------------------------------------------------------------------------ */
FILE *File1, *File2;
char FileSpecs[FILE_SPECS][MAXPATHLFN]; /* Full pathnames */
int MaxBinDifferences = DEFAULT_BINARY_MAX_DIFF;
int MaxASCIIDiffLines = MAX_LINES;	/* Virtually unlimited */
int LinesForResync = DEFAULT_LINES_FOR_RESYNC;
int ContextLines = STANDARD_CONTEXT;	/* 0 = don't show context lines */
enum { DEFAULT, ASCII, BINARY } CompareMode = DEFAULT;
enum { MatchingNames, CommonSource, CommonTarget } NameMatching;
char SourceNamePattern[MAXNAMELFN];
char TargetNamePattern[MAXNAMELFN];
unsigned FileCount, FilesDifferent, UnmatchedCount, DirectoryCount;
/* ------------------------------------------------------------------------ */
const char AsciiDiffStart[] = "***** %s\n";
const char AsciiDiffEnd[] = "*****\n\n";
const char Omissis[] = "...\n";
const char AllFiles[] = "*.*";
const char DummyFileSpec[] = "\\.";
const char InvalidSwitch[] = "Invalid switch: %s";
const char TooManyFiles[] = "Too many filespecs";
const char InvalidFilename[] = "Invalid filename";
const char Differ[] = "The files are different";
const char NoDifferences[] = "No differences";
const char NoCorrespondent[] = "File %s has no correspondent (%s)";

#define NewLine puts("")
/* ------------------------------------------------------------------------ */
/* Kitten section names */
#define HELP		1
#define MESSAGE		2
#define REPORT_TEXT	3
/* ------------------------------------------------------------------------ */
#ifdef USE_KITTEN
  #define Format(setnum, msgnum, message) kittengets(setnum, msgnum, message)
#else
  #define Format(dummy1, dummy2, message)  message
#endif
/* ************************************************************************ */
void HelpMessage(void)
{
  puts("FreeDOS FC version 3.02");
  printf(Format(HELP, 0,"Compares two files or sets of files and displays the differences between them")); NewLine;
  NewLine;
  printf(Format(HELP, 1,"FC [switches] [drive1:][path1]filename1 [drive2][path2]filename2 [switches]")); NewLine;
  printf(Format(HELP, 2," /A    Display only first and last lines for each set of differences")); NewLine;
  printf(Format(HELP, 3," /B    Perform a binary comparison")); NewLine;
  printf(Format(HELP, 4," /C    Disregard the case of letters")); NewLine;
  printf(Format(HELP, 5," /L    Compare files as ASCII text")); NewLine;
  printf(Format(HELP,13," /LBn  Set the maximum number of consecutive different ASCII lines to n")); NewLine;
  printf(Format(HELP, 6," /Mn   Set the maximum differences in binary comparison to n bytes")); NewLine;
  printf(Format(HELP, 7,"       (default = %d, 0 = unlimited, /M = /M0)"),DEFAULT_BINARY_MAX_DIFF); NewLine;
  printf(Format(HELP, 8," /N    Display the line numbers on a text comparison")); NewLine;
  printf(Format(HELP,17," /Q    Don't show the list of differences")); NewLine;
  printf(Format(HELP,16," /R    Show a brief final report (always active when using /S)")); NewLine;
  printf(Format(HELP, 9," /S    Extend the scan to the files in subdirectories")); NewLine;
  printf(Format(HELP,10," /T    Do not expand tabs to spaces")); NewLine;
  printf(Format(HELP,18," /U    Show the filenames of the files without a correspondent")); NewLine;
  printf(Format(HELP,11," /W    Pack white space (tabs and spaces) for text comparison")); NewLine;
  printf(Format(HELP,12," /X    Do not show context lines in text comparison")); NewLine;
  printf(Format(HELP,14," /nnn  Set the minimum number of consecutive matching lines to nnn")); NewLine;
  printf(Format(HELP,15,"       for comparison resynchronization")); NewLine;
}
/* ************************************************************************ */
/* Scan arguments for switches and files */
bool ScanArguments(void)
{
#define MAX_DIGITS	4
  int FileCounter = 0;
  char *Arguments;
  int i;
  char Temp;

  /* Set up a pointer to the argument string */
  Arguments = (char*)MK_FP(_psp, 0x80);
  i = *Arguments;
  Arguments++;
  Arguments[i] = END_OF_STRING;

  /* Arguments parsing */
  while (*Arguments != END_OF_STRING)
    switch (*Arguments)
    {
      case ' ': /* Separators */
      case '\t':
	Arguments++;
	break;

      case '/':
      case '-': /* Switch chars */
	switch (UpCase(Arguments[1]))
	{
	  case 'A':     /* The following two switches are incompatibles */
	    OptFlags.Brief = TRUE;
	    ContextLines = STANDARD_CONTEXT;
	    Arguments += 2;
	    break;

	  case 'X':
	    OptFlags.Brief = FALSE;
	    ContextLines = NO_CONTEXT;
	    Arguments += 2;
	    break;

	  case 'B':
	    CompareMode = BINARY;
	    Arguments += 2;
	    break;

	  case 'L':
	    CompareMode = ASCII;
	    if (UpCase(Arguments[2]) != 'B')
	      Arguments += 2;
	    else
	    {
	      i = 3;
	      while (isdigit(Arguments[i])) i++;
	      Temp = Arguments[i];
	      Arguments[i] = END_OF_STRING;
	      MaxASCIIDiffLines = 0;
	      if ((i > 3) && (i <= 3 + MAX_DIGITS))
		MaxASCIIDiffLines = atoi(&Arguments[3]);
	      if (MaxASCIIDiffLines == 0)
	      {
		printf(Format(MESSAGE,0,(char*)InvalidSwitch), Arguments); NewLine;
		return FALSE;
	      }
	      Arguments += i;
	      *Arguments = Temp;
	    }
	    break;

	  case 'C':
	    OptFlags.IgnoreCase = TRUE;
	    Arguments += 2;
	    break;

	  case 'N':
	    OptFlags.LineNumber = TRUE;
	    Arguments += 2;
	    break;

	  case 'T':
	    OptFlags.NoTabExpand = TRUE;
	    Arguments += 2;
	    break;

	  case 'W':
	    OptFlags.PackSpace = TRUE;
	    Arguments += 2;
	    break;

	  case 'S':     /* The scan of subdirectories implies a final report */
	    OptFlags.ScanSubdirs = TRUE;
	  case 'R':
	    OptFlags.Report = TRUE;
	    Arguments += 2;
	    break;

	  case 'U':
	    OptFlags.ShowUnmatched = TRUE;
	    Arguments += 2;
	    break;

	  case 'Q':
	    OptFlags.Quiet = TRUE;
	    Arguments += 2;
	    break;

	  case 'M':
	    i = 2;
	    while (isdigit(Arguments[i])) i++;
	    Temp = Arguments[i];
	    Arguments[i] = END_OF_STRING;
	    if ((i > 2) && (i <= 2 + MAX_DIGITS))
	      MaxBinDifferences = atoi(&Arguments[2]);
	    else
	    {
	      printf(Format(MESSAGE,0,(char*)InvalidSwitch), Arguments); NewLine;
	      return FALSE;
	    }
	    Arguments += i;
	    *Arguments = Temp;
	    break;

	  case '?':
	  case 'H':
	    HelpMessage();
	    return FALSE;

	  case '0':
	  case '1':
	  case '2':
	  case '3':
	  case '4':
	  case '5':
	  case '6':
	  case '7':
	  case '8':
	  case '9':
	    i = 2;
	    while (isdigit(Arguments[i])) i++;
	    Temp = Arguments[i];
	    Arguments[i] = END_OF_STRING;
	    if ((i > 2) && (i <= 2 + MAX_DIGITS))
	      LinesForResync = atoi(&Arguments[2]);
	    else
	    {
	      printf(Format(MESSAGE,0,(char*)InvalidSwitch), Arguments); NewLine;
	      return FALSE;
	    }
	    Arguments += i;
	    *Arguments = Temp;
	    if (LinesForResync < 1) LinesForResync = 1;
	    break;

	  default:
	    Arguments[2] = END_OF_STRING;
	    printf(Format(MESSAGE,0,(char*)InvalidSwitch), Arguments); NewLine;
	    return FALSE;
	}
	break;

      case '"': /* (Long) filename start marker */
	Arguments++;
	i = 0;
	while (Arguments[i] != '"')
	{
	  if (Arguments[i] == END_OF_STRING)
	  {
	    printf(Format(MESSAGE,2,(char*)InvalidFilename)); NewLine;
	    return FALSE;
	  }
	  i++;
	}
	Arguments[i] = END_OF_STRING;

	if (FileCounter >= FILE_SPECS)
	{
	  printf(Format(MESSAGE,1,(char*)TooManyFiles)); NewLine;
	  return FALSE;
	}

	if (FullPath(FileSpecs[FileCounter], Arguments,
		     sizeof(FileSpecs[0])) == NULL)
	{
	  printf(Format(MESSAGE,2,(char*)InvalidFilename)); NewLine;
	  return FALSE;
	}
	Arguments += i + 1;
	FileCounter++;
	break;

      default:	/* Must be a filename */
	if (FileCounter >= FILE_SPECS)
	{
	  printf(Format(MESSAGE,1,(char*)TooManyFiles)); NewLine;
	  return FALSE;
	}
	i = 0;
	while ((Arguments[i] != ' ') && (Arguments[i] != '\t') &&
	       (Arguments[i] != END_OF_STRING))
	  i++;
	Temp = Arguments[i];
	Arguments[i] = END_OF_STRING;
	if (FullPath(FileSpecs[FileCounter], Arguments,
		     sizeof(FileSpecs[0])) == NULL)
	{
	  printf(Format(MESSAGE,2,(char*)InvalidFilename)); NewLine;
	  return FALSE;
	}
	Arguments += i;
	*Arguments = Temp;
	FileCounter++;
    }

  switch (FileCounter)
  {
    case 0:
      printf(Format(MESSAGE,3,"No file specified")); NewLine;
      return FALSE;

    case 1:		/* Default: the current directory */
      strcpy(FileSpecs[SECOND_FILE], AllFiles);
  }

  for (i = FIRST_FILE; i < FILE_SPECS; i++)
  {
    int LastChar = strlen(FileSpecs[i]) - 1;
    if ((FileSpecs[i][LastChar] == '\\') ||  /* Path only */
	(FileSpecs[i][LastChar] == ':'))     /* Disk drive only */
					     /* Assume "All files" */
      strcpy(&FileSpecs[i][LastChar + 1], AllFiles);
    else
      if (!HasWildcards(FileSpecs[i]) &&
	  IsADirectory(FileSpecs[i]))	     /* Directory */
					     /* Assume "All files" */
	sprintf(&FileSpecs[i][LastChar + 1], "\\%s", AllFiles);
   /* else regular file (possibly with wildcards) */
  }

  {
    /* Pointers to the name part of the files */
    char* FirstFileName = FileNameStart(FileSpecs[FIRST_FILE]);
    char* SecondFileName = FileNameStart(FileSpecs[SECOND_FILE]);

    NameMatching = MatchingNames;
    if (!HasWildcards(SecondFileName))
      NameMatching = CommonTarget;
    if (!HasWildcards(FirstFileName) &&
	(strcmp(SecondFileName, AllFiles) != 0))
      NameMatching = CommonSource;

    strncpy(SourceNamePattern, FirstFileName,
	    sizeof(SourceNamePattern) - 1);
    SourceNamePattern[sizeof(SourceNamePattern) - 1] = END_OF_STRING;

    strncpy(TargetNamePattern, SecondFileName,
	    sizeof(TargetNamePattern) - 1);
    TargetNamePattern[sizeof(TargetNamePattern) - 1] = END_OF_STRING;
  }
  return TRUE;
}

/* ************************************************************************ */
bool BinaryCompare(void)
{
  unsigned char a, b;
  unsigned long Offset, FileSize1, FileSize2;
  int Differences = 0;

  FileSize1 = filelength(fileno(File1));
  FileSize2 = filelength(fileno(File2));
  if (FileSize1 != FileSize2)
  {
    if (OptFlags.Quiet)
    {
      printf(Format(MESSAGE,13,"The files are of different size")); NewLine;
      NewLine;
      return FALSE;
    }
    printf(Format(MESSAGE,4,"Warning: the files are of different size!")); NewLine;
    if (FileSize1 < FileSize2) FileSize1 = FileSize2;
  }
  for (Offset = 0; Offset < FileSize1; Offset++)
  {
    a = fgetc(File1); b = fgetc(File2);
    if (a != b)
    {
      if (OptFlags.Quiet)
      {
	printf(Format(MESSAGE,14,(char*)Differ)); NewLine;
	NewLine;
	return FALSE;
      }
      Differences++;
      if ((MaxBinDifferences > 0) && (Differences > MaxBinDifferences))
      {
	printf(Format(MESSAGE,5,"Comparison stopped after %d mismatches"),
	       MaxBinDifferences); NewLine;
	NewLine;
	return FALSE;
      }
      printf("%08lX:     %02X  %c      %02X  %c\n", Offset,
	     a, (iscntrl(a) ? ' ' : a), b, (iscntrl(b) ? ' ' : b));
    }
  }
  if (Differences == 0)
  {
    printf(Format(MESSAGE,6,(char*)NoDifferences)); NewLine;
    NewLine;
    return TRUE;
  }
  return FALSE;
}

/* ************************************************************************ */
/* Skip to the next line in the file File */
void SkipLine(FILE* File)
{
  int c;

  do
    c = fgetc(File);
  while ((c != '\n') && (c != EOF));
}
/* ------------------------------------------------------------------------ */
/* Display a line from the file File */
void OutputLine(FILE* File, line_counter LineNumber)
{
  unsigned long LineLength = 0;
  int c;

  if (OptFlags.LineNumber) printf("%5u: ", LineNumber);

  while (TRUE)
  {
    c = fgetc(File);
    if ((c == '\n') || (c == EOF)) /* End of line */
    {
      putchar('\n'); break;
    }
    if ((c == '\t') && !OptFlags.NoTabExpand) /* Tab */
      do
      {
	putchar(' '); LineLength++;
      } while ((LineLength % TAB_SIZE) != (TAB_SIZE - 1));
    else
      if (c != '\r')    /* Ignore the carriage return */
      {
	putchar(c); LineLength++;
      }
  }
}
/* ************************************************************************ */
		/* Hashing procedures */

#define CRC_BITS	15	/* 15 bits CRC */
#define CRC_POLY	0xC599	/* X^15+X^14+X^10+X^8+X^7+X^4+X^3+X^0 */
#define CRC_ARRAY_ITEMS (1 << BITS_PER_BYTE)
#define CRC_ARRAY_SIZE	(sizeof(*CRCTable)*CRC_ARRAY_ITEMS)
#define HASH_BUCKETS	(1 << CRC_BITS)
#define HASH_ARRAY_SIZE (sizeof(hashing_values)*MAX_LINES)

typedef signed int hashing_values; /* Must be signed */
typedef hashing_values* hashing_values_ptr;
hashing_values HashValue;
typedef unsigned int crc_values;   /* Max value = (1 << CRC_BITS) - 1 */
typedef crc_values* crc_values_ptr;
crc_values_ptr CRCTable = NULL;
/* ------------------------------------------------------------------------ */
/* Initialise the CRC register and create (only once) the CRC table
   used to speed-up the hashing function */
void InitCRC(void)
{
  int i, j;
  crc_values CRC;

  HashValue = (hashing_values)(-1);	/* Initialise the CRC register */
  if (CRCTable != NULL) return;		/* Table already created */

  CRCTable = (crc_values_ptr)malloc(CRC_ARRAY_SIZE);
  if (CRCTable == NULL) return;		/* Insufficient memory */

  for (i = 0; i < CRC_ARRAY_ITEMS; i++)
  {
    CRC = i << (CRC_BITS - BITS_PER_BYTE);
    for (j = 0; j < BITS_PER_BYTE; j++)
    {
      CRC <<= 1;
      if ((CRC & (1 << CRC_BITS)) != 0) CRC ^= CRC_POLY;
    }
    CRCTable[i] = CRC;
  }
}
/* ------------------------------------------------------------------------ */
/* Hash a character */
void Hash(char c)
{
  /* Update the CRC */
  HashValue = (HashValue << (CRC_BITS - BITS_PER_BYTE)) ^
	      CRCTable[c ^ ((HashValue >> (CRC_BITS - BITS_PER_BYTE)) & 0xFF)];
}

/* ************************************************************************ */
/* occurr indicate the number of occurrences for a hash value. */
typedef enum { NO_OCCURR, SINGLE_OCCURR, MULTIPLE_OCCURR } occurr;
/* Minimum number of bits to represent occurr */
#define BITS_PER_OCCUR	   2
/* The occurrence data codes are packed to save memory */
typedef unsigned char packed_occurr;
typedef packed_occurr* packed_occurr_ptr;
#define OCCUR_PER_BYTE	   ((BITS_PER_BYTE*sizeof(packed_occurr))/BITS_PER_OCCUR)
#define OCCUR_BIT_MASK	   ((1 << BITS_PER_OCCUR) - 1)
#define PACKED_ARRAY_ITEMS ((HASH_BUCKETS + OCCUR_PER_BYTE - 1)/OCCUR_PER_BYTE)
#define OCCURR_ARRAY_SIZE  (sizeof(packed_occurr)*PACKED_ARRAY_ITEMS)

/* ------------------------------------------------------------------------ */
/* Extract bits from the packed occurrence array
   Index must be >= 0 */
occurr GetOccurrences(const packed_occurr Array[], hashing_values Index)
{
  Index %= HASH_BUCKETS;
  return ((Array[Index/OCCUR_PER_BYTE] >>
	   (BITS_PER_OCCUR*(Index % OCCUR_PER_BYTE))) & OCCUR_BIT_MASK);
}
/* ------------------------------------------------------------------------ */
/* Store bits in the packed occurrence array
   Index must be >= 0 */
void SetOccurrences(packed_occurr Array[], hashing_values Index, occurr Occurrence)
{
  unsigned Shifts;

  Index %= HASH_BUCKETS;
  Shifts = BITS_PER_OCCUR*(Index % OCCUR_PER_BYTE);
  Index /= OCCUR_PER_BYTE;
  Array[Index] &= ~(OCCUR_BIT_MASK << Shifts);
  Array[Index] |= (Occurrence << Shifts);
}

/* ************************************************************************ */
/* Read file, build hash & occurrence tables.
   Returns the number of lines in the file. */
line_counter HashFile(FILE* file, hashing_values HashVect[],
		      packed_occurr Occurrences[])
{
  int c;
  bool Skip = FALSE;
  line_counter LineCounter = 0;
  unsigned long LineLength = 0;

  HashValue = 0;
  do
  {
    c = fgetc(file);
    /* Beware of order! There are fall-throughs */
    switch (c)
    {
      case EOF:
	if (LineLength == 0) break;	/* End of file */
	/* Unterminated line: assume line feed */
      case '\n':
	/* This is needed to avoid problems with trailing
	   blanks when blanks are packed */
	if (!Skip) Hash(' ');

	/* HashValue must be > 0 and HashVect[] must be < 0 */
	if (HashValue < 0)
	  HashValue = -HashValue;
	else
	  if (HashValue == 0) HashValue = 1;
	HashVect[LineCounter] = -HashValue;
	switch (GetOccurrences(Occurrences, HashValue))
	{
	  case NO_OCCURR:
	    SetOccurrences(Occurrences, HashValue, SINGLE_OCCURR);
	    break;
	  case SINGLE_OCCURR:
	    SetOccurrences(Occurrences, HashValue, MULTIPLE_OCCURR);
	}
	HashValue = 0; LineCounter++; LineLength = 0;
	Skip = FALSE;
	break;

      case '\t':
	/* Tabs must be expanded and blanks are not packed */
	if (!OptFlags.NoTabExpand && !OptFlags.PackSpace)
	{
	  do
	  {
	    Hash(' '); LineLength++;
	  } while ((LineLength % TAB_SIZE) != 0);
	  break;
	}
      case ' ':
      case '\r':
	/* Blank and tab are packed */
	if (OptFlags.PackSpace)
	{
	  if (!Skip)
	  {
	    Hash(' '); LineLength++;
	    Skip = TRUE;
	  }
	  break;
	}
      default:
	if (OptFlags.IgnoreCase) c = UpCase(c);
	Hash(c); LineLength++;
	Skip = FALSE;
    }
  } while ((c != EOF) && (LineCounter < MAX_LINES));

  if (LineCounter >= MAX_LINES)
  {
    printf(Format(MESSAGE,7,"Warning: comparison interrupted after %d lines"),
	   LineCounter); NewLine;
  }
  return LineCounter;
}

/* ************************************************************************ */
/* Show a block of differences */
void ShowBlockOfDiff(line_counter Start, line_counter Finish,
		     FILE* File, char FileName[],
		     line_counter* LinesRead, line_counter LinesInFile)
{
  line_counter LineCounter = *LinesRead;

		    /* Go to the starting line (of the context) */
  while (LineCounter + ContextLines < Start)
  {
    LineCounter++; SkipLine(File);
  }
  *LinesRead = LineCounter;

  /* If there are no differences and the context is not required
     we are finished */
  if ((Finish < Start) && (ContextLines <= NO_CONTEXT)) return;

  printf(AsciiDiffStart, FileName);
		    /* Show the starting context if needed */
  while (LineCounter < Start)
  {
    LineCounter++; OutputLine(File, LineCounter);
  }
		    /* Show the differing lines */
  if (OptFlags.Brief)
  {
    printf(Omissis);
    while (LineCounter < Finish)
    {
      LineCounter++; SkipLine(File);
    }
  }
  else
    while (LineCounter < Finish)
    {
      LineCounter++; OutputLine(File, LineCounter);
    }
		    /* Show the final context */
  while ((LineCounter < LinesInFile) &&
	 (LineCounter < Finish + ContextLines))
  {
    LineCounter++; OutputLine(File, LineCounter);
  }

  *LinesRead = LineCounter;
}
/* ------------------------------------------------------------------------ */
bool AsciiCompare(void)
{
  bool Linked;
  bool Different = FALSE;
  line_counter i;
  line_counter LinesInFile1, LinesInFile2, LinesRead1, LinesRead2;
  line_counter Line1, Line2;
  hashing_values_ptr Hash1, Hash2;
  /* Arrays of packed occurrences */
  packed_occurr_ptr Occurr1, Occurr2;

  InitCRC();	/* Initialise the CRC for hashing */

  /* Allocate the big structures on the heap */
  Occurr1 = (packed_occurr_ptr)malloc(OCCURR_ARRAY_SIZE);
  Occurr2 = (packed_occurr_ptr)malloc(OCCURR_ARRAY_SIZE);
  Hash1 = (hashing_values_ptr)malloc(HASH_ARRAY_SIZE);
  Hash2 = (hashing_values_ptr)malloc(HASH_ARRAY_SIZE);
  if ((CRCTable == NULL) ||
      (Hash1 == NULL) || (Hash2 == NULL) ||
      (Occurr1 == NULL) || (Occurr2 == NULL))
  {
    printf(Format(MESSAGE,8,"Insufficient memory")); NewLine;
    return FALSE;
  }
  memset(Hash1, 0, HASH_ARRAY_SIZE);
  memset(Hash2, 0, HASH_ARRAY_SIZE);
  memset(Occurr1, 0, OCCURR_ARRAY_SIZE); /* NO_OCCURR */
  memset(Occurr2, 0, OCCURR_ARRAY_SIZE); /* NO_OCCURR */

  /* Step 1: read files and hash them */
  LinesInFile1 = HashFile(File1, Hash1, Occurr1);
  LinesInFile2 = HashFile(File2, Hash2, Occurr2);

  /* Step 2: identify lines that are unique in both files
	     i.e. have a single occur of a hash value
     N.B. Warning: to speed things up this exploits the peculiarity
	  of the definition of occurr */
  for (i = 0; i < PACKED_ARRAY_ITEMS; i++) Occurr1[i] &= Occurr2[i];

  /* Step 3: link together the nearest matching unique lines
     N.B. Hashx is originally < 0 and is set to the matching
	  line number (>= 0) if a match is found */
  for (i = 0; i < LinesInFile1; i++)
    if (GetOccurrences(Occurr1, -Hash1[i]) == SINGLE_OCCURR)
    {
      line_counter Forward = i, Backward = i;
      while ((Forward < LinesInFile2) || (Backward >= 0))
      {
	if (Forward < LinesInFile2)
	{
	  if (Hash2[Forward] == Hash1[i])
	  {
	    Hash1[i] = Forward; Hash2[Forward] = i;
	    break;
	  }
	  Forward++;
	}

	if (Backward >= 0)
	{
	  if (Hash2[Backward] == Hash1[i])
	  {
	    Hash1[i] = Backward; Hash2[Backward] = i;
	    break;
	  }
	  Backward--;
	}
      }
    }
  free (Occurr1); free (Occurr2);	/* No more needed */

  /* Step 4: link the first and last lines, if possible */
  if ((Hash1[0] < 0) && (Hash1[0] == Hash2[0])) Hash1[0] = Hash2[0] = 0;
  if ((Hash1[LinesInFile1 - 1] < 0) &&
      (Hash1[LinesInFile1 - 1] == Hash2[LinesInFile2 - 1]))
  {
    Hash1[LinesInFile1 - 1] = LinesInFile2 - 1;
    Hash2[LinesInFile2 - 1] = LinesInFile1 - 1;
  }

  /* Step 5: starting from linked lines, link following lines that match
     N.B. Hashx was set to matching line number (>= 0) if match found */
  Linked = FALSE;
  for (i = 0; i < LinesInFile1; i++)
    if (Hash1[i] >= 0)
      Linked = TRUE;
    else
      if (Linked)
      {
	Line2 = Hash1[i - 1] + 1;
	if (Hash1[i] == Hash2[Line2])
	{
	  Hash1[i] = Line2;
	  Hash2[Line2] = i;
	}
	else
	  Linked = FALSE;
      }

  /* Step 6: link matching lines that precede linked lines */
  Linked = FALSE;
  for (i = LinesInFile1 - 1; i >= 0; i--)
    if (Hash1[i] >= 0)
      Linked = TRUE;
    else
      if (Linked)
      {
	Line2 = Hash1[i + 1] - 1;
	if (Hash1[i] == Hash2[Line2])
	{
	  Hash1[i] = Line2;
	  Hash2[Line2] = i;
	}
	else
	  Linked = FALSE;
      }

  /* Step 7: display the results (greedy algorithm) */
  free (Hash2);		/* No more needed: redundant */
  rewind(File1); rewind(File2);
  LinesRead1 = LinesRead2 = 0;
  Line1 = Line2 = 0;
  while (TRUE)
  {
    line_counter i;
    line_counter ResyncLine;
			      /* Skip matching lines */
    while ((Line1 < LinesInFile1) && (Line2 < LinesInFile2) &&
	   (Hash1[Line1] == Line2))
    {
      Line1++; Line2++;
    }
			      /* Compare terminated */
    if ((Line1 >= LinesInFile1) && (Line2 >= LinesInFile2)) break;

    if (OptFlags.Quiet)
    {
      printf(Format(MESSAGE,14,(char*)Differ)); NewLine;
      NewLine;
      Different = TRUE;
      break;
    }

    ResyncLine = Line1;
    i = 0;
    do		    /* Find the end of the mismatching block */
    {
      ResyncLine += i;

      i = 0;	    /* Skip mismatching lines */
      while ((ResyncLine + 1 < LinesInFile1) &&
	     ((Hash1[ResyncLine] <= Line2) ||
	      (Hash1[ResyncLine] + 1 != Hash1[ResyncLine + 1])))
      {
	i++; ResyncLine++;
	if (i > MaxASCIIDiffLines)
	{
	  printf(Format(MESSAGE,12,"Resync failed: files too different")); NewLine;
	  NewLine;
	  Different = TRUE;
	  break;
	}
      }
      /* Found at least 2 consecutive line matching unless eof */

      i = ResyncLine + 1; /* Count matching lines */
      while ((i - ResyncLine < LinesForResync) && (i < LinesInFile1) &&
	     (Hash1[i] >= 0) && (Hash1[i] - 1 == Hash1[i - 1]))
	i++;

      i -= ResyncLine;
    } while ((i < LinesForResync) && (ResyncLine < LinesInFile1));

    ShowBlockOfDiff(Line1, ResyncLine, File1, FileSpecs[FIRST_FILE],
		    &LinesRead1, LinesInFile1);
    Line1 = ResyncLine;
    if (ResyncLine < LinesInFile1)
      ResyncLine = Hash1[ResyncLine];
    else
      ResyncLine = LinesInFile2; /* No resync up to the end of the file */
    ShowBlockOfDiff(Line2, ResyncLine,
		    File2, FileSpecs[SECOND_FILE],
		    &LinesRead2, LinesInFile2);
    Line2 = ResyncLine;
    printf(AsciiDiffEnd);
    Different = TRUE;
  }
  free (Hash1);

  if (!Different)
  {
    printf(Format(MESSAGE,6,(char*)NoDifferences)); NewLine;
    NewLine;
    return TRUE;
  }
  return FALSE;
}

/* ************************************************************************ */
/* Warning: this procedure alters the name part of FileSpecs
	    but leaves the path part unchanged */
bool CompareSetOfFiles(void)
{
  find_data FindData1, FindData2;
  bool Done1, Done2;
  /* Pointers to the name part of the files */
  char* FirstFileName = FileNameStart(FileSpecs[FIRST_FILE]);
  char* SecondFileName = FileNameStart(FileSpecs[SECOND_FILE]);

  /* Avoid path length overflow */
  if (FirstFileName - FileSpecs[FIRST_FILE] + strlen (SourceNamePattern) >=
      sizeof(FileSpecs[FIRST_FILE]))
    return FALSE;
  strcpy(FirstFileName, SourceNamePattern);

  Done1 = (FindFirst(FileSpecs[FIRST_FILE], &FindData1, FA_RDONLY) != 0);
  while (!Done1)
  {
    /* Avoid path length overflow */
    if (FirstFileName - FileSpecs[FIRST_FILE] + strlen (FindData1.Filename) >=
	sizeof(FileSpecs[FIRST_FILE]))
    {
      FindClose(&FindData1);
      return FALSE;
    }
    strcpy(FirstFileName, FindData1.Filename);
    Done2 = FALSE;
    switch (NameMatching)
    {
      case MatchingNames:
	Done2 = !MatchNames(TargetNamePattern, FirstFileName, SecondFileName);
	break;

      case CommonSource:
	Done2 = (FindFirst(FileSpecs[SECOND_FILE], &FindData2, FA_RDONLY) != 0);
	if (Done2)
	{
	  if (OptFlags.ShowUnmatched)
	  {
	    printf(Format(MESSAGE,15,(char*)NoCorrespondent),
		   FileSpecs[FIRST_FILE], FileSpecs[SECOND_FILE]); NewLine;
	    NewLine;
	  }
	  UnmatchedCount++;
	}
	else
	  /* Avoid path length overflow */
	  if (SecondFileName - FileSpecs[SECOND_FILE] +
	      strlen (FindData2.Filename) >= sizeof(FileSpecs[SECOND_FILE]))
	    Done2 = TRUE;
	  else
	    strcpy(SecondFileName, FindData2.Filename);
	break;
    }

    while (!Done2)
    {
      File2 = FileOpen(FileSpecs[SECOND_FILE], "rb");
      if (File2 == NULL)
      {
	if (OptFlags.ShowUnmatched)
	{
	  printf(Format(MESSAGE,15,(char*)NoCorrespondent),
		 FileSpecs[FIRST_FILE], FileSpecs[SECOND_FILE]); NewLine;
	  NewLine;
	}
	UnmatchedCount++;
      }
      else
      {
	File1 = FileOpen(FileSpecs[FIRST_FILE], "rb");
	if (File1 == NULL)
	{
	  fcloseall();
	  printf(Format(MESSAGE,9,"Error opening file %s"),
		 FileSpecs[FIRST_FILE]); NewLine;
	  FindClose(&FindData1);
	  if (NameMatching == CommonSource) FindClose(&FindData2);
	  return FALSE;
	}
	FileCount++;
	printf(Format(MESSAGE,10,"Comparing %s and %s"),
	       FileSpecs[FIRST_FILE], FileSpecs[SECOND_FILE]); NewLine;
	switch (CompareMode)
	{
	  case ASCII:
	    if (!AsciiCompare()) FilesDifferent++;
	    break;

	  case BINARY:
	    if (!BinaryCompare()) FilesDifferent++;
	    break;

	  default:
	    if (BinaryFile(FileSpecs[FIRST_FILE]) ||
		BinaryFile(FileSpecs[SECOND_FILE]))
	    {
	      if (!BinaryCompare()) FilesDifferent++;
	    }
	    else
	      if (!AsciiCompare()) FilesDifferent++;
	}
	fcloseall();
      }

      Done2 = TRUE;
      if (NameMatching == CommonSource)
      {
	Done2 = (FindNext(&FindData2) != 0);
	if (!Done2)
	  /* Avoid path length overflow */
	  if (SecondFileName - FileSpecs[SECOND_FILE] +
	      strlen (FindData2.Filename) >= sizeof(FileSpecs[SECOND_FILE]))
	    Done2 = TRUE;
	  else
	    strcpy(SecondFileName, FindData2.Filename);
      }
    }
    Done1 = (FindNext(&FindData1) != 0);
  }
  return TRUE;
}
/* ------------------------------------------------------------------------ */
/* Warning: this procedure appends data to FileSpecs */
bool ScanSubdirs(void)
{
  find_data FindData1;
  char* FirstSubdirName;  /* Pointers to the subdirectory names */
  char* SecondSubdirName;

  FirstSubdirName = FileNameStart(FileSpecs[FIRST_FILE]);
  SecondSubdirName = FileNameStart(FileSpecs[SECOND_FILE]);

  /* Avoid path length overflow */
  if (FirstSubdirName - FileSpecs[FIRST_FILE] + strlen (AllFiles) >=
      sizeof(FileSpecs[FIRST_FILE]))
    return FALSE;
  strcpy(FirstSubdirName, AllFiles); /* All subdirectories */

  if (FindFirst(FileSpecs[FIRST_FILE], &FindData1, FA_DIREC | FA_RDONLY) != 0)
    return FALSE;

  do
    if (((FindData1.Attributes & FA_DIREC) != 0) && /* Is a directory */
	(*(FindData1.Filename) != '.'))
    {
      find_data FindData2;
      /* Avoid path length overflow */
      if (SecondSubdirName - FileSpecs[SECOND_FILE] +
	  strlen (FindData1.Filename) >
	  sizeof(FileSpecs[SECOND_FILE]) - sizeof(DummyFileSpec))
      {
	FindClose(&FindData1);
	return FALSE;
      }
      /* If the same subdirectory exists in the second path */
      strcpy(SecondSubdirName, FindData1.Filename);
      FileSpecs[SECOND_FILE][sizeof(FileSpecs[SECOND_FILE]) - 1] = END_OF_STRING;
      if (FindFirst(FileSpecs[SECOND_FILE], &FindData2, FA_DIREC | FA_RDONLY) == 0)
      {
	FindClose(&FindData2);
	if ((FindData2.Attributes & FA_DIREC) == 0) break; /* Not a directory */

	/* Avoid path length overflow */
	if (FirstSubdirName - FileSpecs[FIRST_FILE] +
	    strlen (FindData1.Filename) >
	    sizeof(FileSpecs[FIRST_FILE]) - sizeof(DummyFileSpec))
	{
	  FindClose(&FindData1);
	  return FALSE;
	}
	strcpy(FirstSubdirName, FindData1.Filename);
	FileSpecs[FIRST_FILE][sizeof(FileSpecs[FIRST_FILE]) - 1] = END_OF_STRING;

	/* Do compare the files in the subdirs */
	DirectoryCount++;
	strcat(FileSpecs[FIRST_FILE], DummyFileSpec);
	strcat(FileSpecs[SECOND_FILE], DummyFileSpec);
	if (!CompareSetOfFiles() || !ScanSubdirs())
	{
	  FindClose(&FindData1);
	  return FALSE;
	}
      }
  } while (FindNext(&FindData1) == 0);
  return TRUE;
}
/* ------------------------------------------------------------------------ */
void OnExit(void)
{
  /* If created destroy the CRC speed-up table */
  if (CRCTable != NULL) free(CRCTable);
#ifdef USE_KITTEN
  kittenclose();	/* Close the NLS catalog */
#endif
}
/* ------------------------------------------------------------------------ */
int main(void)
{
#ifdef USE_KITTEN
  kittenopen("FC");     /* Try opening NLS catalog */
#endif
  atexit (OnExit);	/* Install the clean-up procedure */
  UpCaseInit();		/* Initialize the table for uppercase conversion */

  if (!ScanArguments()) return EL_PARAM_ERROR;

  FileCount = FilesDifferent = UnmatchedCount = 0;
  DirectoryCount = 1;

  if (!CompareSetOfFiles()) return EL_OPEN_ERROR;

  if (OptFlags.ScanSubdirs)
    if (!ScanSubdirs()) return EL_OPEN_ERROR;

  if (OptFlags.Report)
  {
    printf(Format(REPORT_TEXT,0,"Compared %d files"),FileCount);
    if (OptFlags.ScanSubdirs)
      printf(Format(REPORT_TEXT,1," in %d directories"),DirectoryCount);
    NewLine;
    printf(Format(REPORT_TEXT,2,"%d files match, %d files are different"),
	   FileCount - FilesDifferent, FilesDifferent); NewLine;
    if ((NameMatching == MatchingNames) || (UnmatchedCount > 0))
    {
      printf(Format(REPORT_TEXT,3,"%d files have no correspondent"),
	     UnmatchedCount);
      NewLine;
    }
  }

  if ((FileCount == 0) && !OptFlags.Report)
  {
    printf(Format(MESSAGE,11,"No such file or directory")); NewLine;
    return EL_NO_FILE_ERROR;
  }

  if (FilesDifferent > 0) return EL_DIFFERENT;
  return EL_NO_DIFF;
}