raw2tiff: print usage to stdout when -h is used

see #17
This commit is contained in:
Thomas Bernard 2020-03-07 16:09:54 +01:00
parent ee6cf2f59d
commit 46c8624972
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -59,6 +59,13 @@
#include "tiffiop.h" #include "tiffiop.h"
#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
@ -81,7 +88,7 @@ static void swapBytesInScanline(void *, uint32, TIFFDataType);
static int guessSize(int, TIFFDataType, _TIFF_off_t, uint32, int, static int guessSize(int, TIFFDataType, _TIFF_off_t, uint32, int,
uint32 *, uint32 *); uint32 *, uint32 *);
static double correlation(void *, void *, uint32, TIFFDataType); static double correlation(void *, void *, uint32, TIFFDataType);
static void usage(void); static void usage(int);
static int processCompressOptions(char*); static int processCompressOptions(char*);
int int
@ -114,7 +121,7 @@ main(int argc, char* argv[])
switch (c) { switch (c) {
case 'c': /* compression scheme */ case 'c': /* compression scheme */
if (!processCompressOptions(optarg)) if (!processCompressOptions(optarg))
usage(); usage(EXIT_FAILURE);
break; break;
case 'r': /* rows/strip */ case 'r': /* rows/strip */
rowsperstrip = atoi(optarg); rowsperstrip = atoi(optarg);
@ -193,24 +200,24 @@ main(int argc, char* argv[])
outfilename = optarg; outfilename = optarg;
break; break;
case 'h': case 'h':
usage(); usage(EXIT_SUCCESS);
default: default:
break; break;
} }
} }
if (argc - optind < 2) if (argc - optind < 2)
usage(); usage(EXIT_FAILURE);
fd = open(argv[optind], O_RDONLY|O_BINARY, 0); fd = open(argv[optind], O_RDONLY|O_BINARY, 0);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "%s: %s: Cannot open input file.\n", fprintf(stderr, "%s: %s: Cannot open input file.\n",
argv[0], argv[optind]); argv[0], argv[optind]);
return (-1); return (EXIT_FAILURE);
} }
if (guessSize(fd, dtype, hdr_size, nbands, swab, &width, &length) < 0) if (guessSize(fd, dtype, hdr_size, nbands, swab, &width, &length) < 0)
return 1; return EXIT_FAILURE;
if (outfilename == NULL) if (outfilename == NULL)
outfilename = argv[optind+1]; outfilename = argv[optind+1];
@ -218,7 +225,7 @@ main(int argc, char* argv[])
if (out == NULL) { if (out == NULL) {
fprintf(stderr, "%s: %s: Cannot open file for output.\n", fprintf(stderr, "%s: %s: Cannot open file for output.\n",
argv[0], outfilename); argv[0], outfilename);
return (-1); return (EXIT_FAILURE);
} }
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, length); TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);
@ -336,7 +343,7 @@ main(int argc, char* argv[])
if (buf1) if (buf1)
_TIFFfree(buf1); _TIFFfree(buf1);
TIFFClose(out); TIFFClose(out);
return (0); return (EXIT_SUCCESS);
} }
static void static void
@ -598,7 +605,7 @@ processCompressOptions(char* opt)
else if (cp[1] == 'r' ) else if (cp[1] == 'r' )
jpegcolormode = JPEGCOLORMODE_RAW; jpegcolormode = JPEGCOLORMODE_RAW;
else else
usage(); usage(EXIT_FAILURE);
cp = strchr(cp+1,':'); cp = strchr(cp+1,':');
} }
@ -617,7 +624,7 @@ processCompressOptions(char* opt)
return (1); return (1);
} }
static char* stuff[] = { static const char* stuff[] = {
"raw2tiff --- tool for converting raw byte sequences in TIFF images", "raw2tiff --- tool for converting raw byte sequences in TIFF images",
"usage: raw2tiff [options] input.raw output.tif", "usage: raw2tiff [options] input.raw output.tif",
"where options are:", "where options are:",
@ -678,16 +685,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);
} }
/* vim: set ts=8 sts=8 sw=8 noet: */ /* vim: set ts=8 sts=8 sw=8 noet: */