/* See also: Four-way list. */
/* Copyright (C) 1994 by R.Harmsen, all rights reserved */
#include
#include
#include
static char *usagetext[] =
{
"Usage: 4waylist [-prefixlength] leftfile ritefile ",
" ",
"This program reads from two files (created by bincrc, for example, ",
"which from a certain position (28 by default) have path names, and, ",
"in front of that, information that identifies their contents, like a ",
"CRC and a size. Both files must be sorted by path. ",
"It then creates four different output files, which contain pathnames, ",
"as follows: ",
" ",
"Name | Contents ",
"-----+--------------------------------------------------------------- ",
"le | Paths that occur only in the file specified as first argument ",
"ri | Paths that occur only in the file specified as second argument ",
"eq | Paths that occur in both files, and have equal contents ",
"ne | Paths that occur in both files, and have different contents ",
" ",
"The purpose of this is to help in synchronizing files in different ",
"trees, possibly on different drives or computers. (Often some ",
"preparatory manipulation will be needed to make paths the same, ",
"by removing inherently differing parts. ",
NULL,
};
static void Usage (void)
{
char **p;
for (p = usagetext; *p; p++)
{
printf("%s\n", *p);
}
exit(1);
}
#define LINELEN 513
static int readin (FILE *fp, char *buf, size_t size);
int main (int argc, char **argv)
{
const int DEFAULT_PREFIX = 28;
int prefixlength = DEFAULT_PREFIX;
FILE *inle, *inri;
FILE *le, *ri, *eq, *ne;
char bufle[LINELEN], bufri[LINELEN];
long linle = 0, linri = 0, lle = 0, lri = 0, leq = 0, lne = 0;
if (argc == 4 && argv[1][0] == '-')
{
prefixlength = atoi(argv[1] + 1);
argc--, argv++;
}
if (argc != 3)
Usage();
// Open input files
if ((inle = fopen(argv[1], "rt")) == NULL)
{
printf("Can't open %s for input\n", argv[1]);
Usage();
}
if ((inri = fopen(argv[2], "rt")) == NULL)
{
printf("Can't open %s for input\n", argv[2]);
Usage();
}
// Open output files
if ((le = fopen("le", "wt")) == NULL)
{
printf("Can't open %s for output\n", "le");
exit(2);
}
if ((ri = fopen("ri", "wt")) == NULL)
{
printf("Can't open %s for output\n", "ri");
exit(2);
}
if ((eq = fopen("eq", "wt")) == NULL)
{
printf("Can't open %s for output\n", "eq");
exit(2);
}
if ((ne = fopen("ne", "wt")) == NULL)
{
printf("Can't open %s for output\n", "ne");
exit(2);
}
if (readin(inle, bufle, sizeof bufle))
linle++;
if (readin(inri, bufri, sizeof bufri))
linri++;
// Balance line method
while (!feof(inle) || !feof(inri))
{
int cmpname = 0;
int cmpcont = 0;
cmpname = strcmp(bufle + prefixlength, bufri + prefixlength);
if (cmpname == 0)
cmpcont = strcmp(bufle, bufri);
if (cmpname > 0)
{
fprintf(ri, "%s\n", bufri + prefixlength);
lri++;
if (readin(inri, bufri, sizeof bufri))
linri++;
}
else if (cmpname < 0)
{
fprintf(le, "%s\n", bufle + prefixlength);
lle++;
if (readin(inle, bufle, sizeof bufle))
linle++;
}
else // Name equal, now look at contents
{
if (cmpcont == 0)
{
fprintf(eq, "%s\n", bufle + prefixlength);
leq++;
}
else
{
fprintf(ne, "%s\n", bufle + prefixlength);
lne++;
}
if (readin(inle, bufle, sizeof bufle))
linle++;
if (readin(inri, bufri, sizeof bufri))
linri++;
}
}
fclose(inle);
fclose(inri);
fclose(le);
fclose(ri);
fclose(eq);
fclose(ne);
printf("Read %6ld << >> %6ld\n", linle, linri);
printf("Written %6ld left, %6ld right, %6ld equal, %6ld not equal\n",
lle, lri, leq, lne);
return 0;
}
static int readin (FILE *fp, char *buf, size_t size)
{
char *p;
if (feof(fp) ||
fgets(buf, size, fp) == NULL)
{
memset(buf, 0xff, size - 1);
buf[size - 1] = '\0';
return 0;
}
/* Remove any \r and/or \n, so that Unix/Windows are still valid */
if ((p = strrchr(buf, '\r')) != 0)
*p = '\0';
if ((p = strrchr(buf, '\n')) != 0)
*p = '\0';
return 1;
}