Code cleanup
This commit is contained in:
parent
29fb417934
commit
2433c6b43f
48
src/cmp.c
48
src/cmp.c
|
@ -8,7 +8,8 @@
|
||||||
|
|
||||||
|
|
||||||
/* types */
|
/* types */
|
||||||
typedef enum _OutputType {
|
typedef enum _OutputType
|
||||||
|
{
|
||||||
OT_NONE,
|
OT_NONE,
|
||||||
OT_LONG,
|
OT_LONG,
|
||||||
OT_DEFAULT
|
OT_DEFAULT
|
||||||
|
@ -16,9 +17,10 @@ typedef enum _OutputType {
|
||||||
|
|
||||||
|
|
||||||
/* cmp */
|
/* cmp */
|
||||||
static int _cmp_files(OutputType ot, char * file1, char * file2,
|
static int _cmp_error(char const * message, int ret);
|
||||||
|
static int _cmp_files(OutputType ot, char const * file1, char const * file2,
|
||||||
FILE * fp1, FILE * fp2);
|
FILE * fp1, FILE * fp2);
|
||||||
static int _cmp(OutputType ot, char * file1, char * file2)
|
static int _cmp(OutputType ot, char const * file1, char const * file2)
|
||||||
{
|
{
|
||||||
FILE * fp1;
|
FILE * fp1;
|
||||||
FILE * fp2;
|
FILE * fp2;
|
||||||
|
@ -26,30 +28,34 @@ static int _cmp(OutputType ot, char * file1, char * file2)
|
||||||
if(strcmp("-", file1) == 0)
|
if(strcmp("-", file1) == 0)
|
||||||
fp1 = stdin;
|
fp1 = stdin;
|
||||||
else if((fp1 = fopen(file1, "r")) == NULL)
|
else if((fp1 = fopen(file1, "r")) == NULL)
|
||||||
{
|
return _cmp_error(file1, 1);
|
||||||
perror("fopen");
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
if(strcmp("-", file2) == 0)
|
if(strcmp("-", file2) == 0)
|
||||||
fp2 = stdin;
|
fp2 = stdin;
|
||||||
else if((fp2 = fopen(file2, "r")) == NULL)
|
else if((fp2 = fopen(file2, "r")) == NULL)
|
||||||
{
|
{
|
||||||
perror("fopen");
|
_cmp_error(file2, 0);
|
||||||
if(fp1 != stdin)
|
if(fp1 != stdin && fclose(fp1) != 0)
|
||||||
fclose(fp1);
|
return _cmp_error(file1, 1);
|
||||||
return 2;
|
return 1;
|
||||||
}
|
}
|
||||||
return _cmp_files(ot, file1, file2, fp1, fp2);
|
return _cmp_files(ot, file1, file2, fp1, fp2);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _cmp_files(OutputType ot, char * file1, char * file2,
|
static int _cmp_error(char const * message, int ret)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s", "cmp: ");
|
||||||
|
perror(message);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int _cmp_files(OutputType ot, char const * file1, char const * file2,
|
||||||
FILE * fp1, FILE * fp2)
|
FILE * fp1, FILE * fp2)
|
||||||
{
|
{
|
||||||
|
int ret = 0;
|
||||||
int c1;
|
int c1;
|
||||||
int c2;
|
int c2;
|
||||||
unsigned int byte = 1;
|
unsigned int byte = 1;
|
||||||
unsigned int line = 1;
|
unsigned int line = 1;
|
||||||
int res = 0;
|
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
|
@ -63,12 +69,12 @@ static int _cmp_files(OutputType ot, char * file1, char * file2,
|
||||||
fprintf(stderr, "%s%s\n",
|
fprintf(stderr, "%s%s\n",
|
||||||
"cmp: EOF on ",
|
"cmp: EOF on ",
|
||||||
c1 == EOF ? file1 : file2);
|
c1 == EOF ? file1 : file2);
|
||||||
res = 1;
|
ret = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(c1 != c2)
|
if(c1 != c2)
|
||||||
{
|
{
|
||||||
res = 1;
|
ret = 1;
|
||||||
if(ot == OT_DEFAULT)
|
if(ot == OT_DEFAULT)
|
||||||
{
|
{
|
||||||
printf("%s %s differ: char %u, line %u\n",
|
printf("%s %s differ: char %u, line %u\n",
|
||||||
|
@ -84,11 +90,11 @@ static int _cmp_files(OutputType ot, char * file1, char * file2,
|
||||||
line++;
|
line++;
|
||||||
byte++;
|
byte++;
|
||||||
}
|
}
|
||||||
if(fp1 != stdin)
|
if(fp1 != stdin && fclose(fp1) != 0)
|
||||||
fclose(fp1);
|
ret |= _cmp_error(file1, 1);
|
||||||
if(fp2 != stdin)
|
if(fp2 != stdin && fclose(fp2) != 0)
|
||||||
fclose(fp2);
|
ret |= _cmp_error(file2, 1);
|
||||||
return res;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -122,5 +128,5 @@ int main(int argc, char * argv[])
|
||||||
}
|
}
|
||||||
if(argc - optind != 2)
|
if(argc - optind != 2)
|
||||||
return _usage();
|
return _usage();
|
||||||
return _cmp(ot, argv[optind], argv[optind+1]);
|
return _cmp(ot, argv[optind], argv[optind+1]) == 0 ? 0 : 2;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user