tiffcmp: match exit status for posix cmp and diff tools

This commit is contained in:
Thomas Bernard 2020-03-07 13:32:46 +01:00
parent 02875964eb
commit 5e0eb5bbf4
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -39,6 +39,13 @@
#include "tiffio.h" #include "tiffio.h"
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
#ifndef HAVE_GETOPT #ifndef HAVE_GETOPT
extern int getopt(int argc, char * const argv[], const char *optstring); extern int getopt(int argc, char * const argv[], const char *optstring);
#endif #endif
@ -51,7 +58,7 @@ static uint16 sampleformat = SAMPLEFORMAT_UINT;
static uint32 imagewidth; static uint32 imagewidth;
static uint32 imagelength; static uint32 imagelength;
static void usage(void); static void usage(int code);
static int tiffcmp(TIFF*, TIFF*); static int tiffcmp(TIFF*, TIFF*);
static int cmptags(TIFF*, TIFF*); static int cmptags(TIFF*, TIFF*);
static int ContigCompare(int, uint32, unsigned char*, unsigned char*, tsize_t); static int ContigCompare(int, uint32, unsigned char*, unsigned char*, tsize_t);
@ -61,6 +68,12 @@ static void PrintFloatDiff(uint32, int, uint32, double, double);
static void leof(const char*, uint32, int); static void leof(const char*, uint32, int);
/*
* exit with status :
* 0 No differences were found.
* 1 Differences were found.
* >1 An error occurred.
*/
int int
main(int argc, char* argv[]) main(int argc, char* argv[])
{ {
@ -71,7 +84,7 @@ main(int argc, char* argv[])
extern char* optarg; extern char* optarg;
#endif #endif
while ((c = getopt(argc, argv, "ltz:")) != -1) while ((c = getopt(argc, argv, "ltz:h")) != -1)
switch (c) { switch (c) {
case 'l': case 'l':
stopondiff = 0; stopondiff = 0;
@ -82,18 +95,20 @@ main(int argc, char* argv[])
case 't': case 't':
stoponfirsttag = 0; stoponfirsttag = 0;
break; break;
case 'h':
usage(EXIT_SUCCESS);
case '?': case '?':
usage(); usage(2);
/*NOTREACHED*/ /*NOTREACHED*/
} }
if (argc - optind < 2) if (argc - optind < 2)
usage(); usage(2);
tif1 = TIFFOpen(argv[optind], "r"); tif1 = TIFFOpen(argv[optind], "r");
if (tif1 == NULL) if (tif1 == NULL)
return (-1); return (2);
tif2 = TIFFOpen(argv[optind+1], "r"); tif2 = TIFFOpen(argv[optind+1], "r");
if (tif2 == NULL) if (tif2 == NULL)
return (-2); return (2);
dirnum = 0; dirnum = 0;
while (tiffcmp(tif1, tif2)) { while (tiffcmp(tif1, tif2)) {
if (!TIFFReadDirectory(tif1)) { if (!TIFFReadDirectory(tif1)) {
@ -115,7 +130,7 @@ main(int argc, char* argv[])
return (0); return (0);
} }
char* stuff[] = { static const char* stuff[] = {
"usage: tiffcmp [options] file1 file2", "usage: tiffcmp [options] file1 file2",
"where options are:", "where options are:",
" -l list each byte of image data that differs between the files", " -l list each byte of image data that differs between the files",
@ -125,16 +140,15 @@ NULL
}; };
static void static void
usage(void) usage(int code)
{ {
char buf[BUFSIZ];
int i; int i;
FILE * out = (code == EXIT_SUCCESS) ? stdout : stderr;
setbuf(stderr, buf); fprintf(out, "%s\n\n", TIFFGetVersion());
fprintf(stderr, "%s\n\n", TIFFGetVersion());
for (i = 0; stuff[i] != NULL; i++) for (i = 0; stuff[i] != NULL; i++)
fprintf(stderr, "%s\n", stuff[i]); fprintf(out, "%s\n", stuff[i]);
exit(-1); exit(code);
} }
#define checkEOF(tif, row, sample) { \ #define checkEOF(tif, row, sample) { \
@ -177,7 +191,7 @@ tiffcmp(TIFF* tif1, TIFF* tif2)
buf2 = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif2)); buf2 = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif2));
if (buf1 == NULL || buf2 == NULL) { if (buf1 == NULL || buf2 == NULL) {
fprintf(stderr, "No space for scanline buffers\n"); fprintf(stderr, "No space for scanline buffers\n");
exit(-1); exit(2);
} }
if (config1 != config2 && bitspersample != 8 && samplesperpixel > 1) { if (config1 != config2 && bitspersample != 8 && samplesperpixel > 1) {
fprintf(stderr, fprintf(stderr,