ppm2tiff: output usage to stdout when using -h option

also uses std C EXIT_SUCCESS / EXIT_FAILURE
see #17
This commit is contained in:
Thomas Bernard 2020-03-07 12:50:53 +01:00
parent 3ac0c950c6
commit 113a07e9f3
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -51,6 +51,13 @@
extern int getopt(int argc, char * const argv[], const char *optstring); extern int getopt(int argc, char * const argv[], const char *optstring);
#endif #endif
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
#define streq(a,b) (strcmp(a,b) == 0) #define streq(a,b) (strcmp(a,b) == 0)
#define strneq(a,b,n) (strncmp(a,b,n) == 0) #define strneq(a,b,n) (strncmp(a,b,n) == 0)
@ -60,7 +67,7 @@ static int quality = 75; /* JPEG quality */
static int jpegcolormode = JPEGCOLORMODE_RGB; static int jpegcolormode = JPEGCOLORMODE_RGB;
static uint32 g3opts; static uint32 g3opts;
static void usage(void); static void usage(int code);
static int processCompressOptions(char*); static int processCompressOptions(char*);
static void static void
@ -162,7 +169,7 @@ static void
BadPPM(char* file) BadPPM(char* file)
{ {
fprintf(stderr, "%s: Not a PPM file.\n", file); fprintf(stderr, "%s: Not a PPM file.\n", file);
exit(-2); exit(EXIT_FAILURE);
} }
@ -202,13 +209,13 @@ main(int argc, char* argv[])
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "%s: Too few arguments\n", argv[0]); fprintf(stderr, "%s: Too few arguments\n", argv[0]);
usage(); usage(EXIT_FAILURE);
} }
while ((c = getopt(argc, argv, "c:r:R:")) != -1) while ((c = getopt(argc, argv, "c:r:R:h")) != -1)
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);
@ -216,14 +223,16 @@ main(int argc, char* argv[])
case 'R': /* resolution */ case 'R': /* resolution */
resolution = atof(optarg); resolution = atof(optarg);
break; break;
case 'h':
usage(EXIT_SUCCESS);
case '?': case '?':
usage(); usage(EXIT_FAILURE);
/*NOTREACHED*/ /*NOTREACHED*/
} }
if (optind + 2 < argc) { if (optind + 2 < argc) {
fprintf(stderr, "%s: Too many arguments\n", argv[0]); fprintf(stderr, "%s: Too many arguments\n", argv[0]);
usage(); usage(EXIT_FAILURE);
} }
/* /*
@ -235,7 +244,7 @@ main(int argc, char* argv[])
in = fopen(infile, "rb"); in = fopen(infile, "rb");
if (in == NULL) { if (in == NULL) {
fprintf(stderr, "%s: Can not open.\n", infile); fprintf(stderr, "%s: Can not open.\n", infile);
return (-1); return (EXIT_FAILURE);
} }
} else { } else {
infile = "<stdin>"; infile = "<stdin>";
@ -306,7 +315,7 @@ main(int argc, char* argv[])
if (0 != (prec & (prec + 1))) { if (0 != (prec & (prec + 1))) {
fprintf(stderr, "%s: unsupported maxval %u.\n", fprintf(stderr, "%s: unsupported maxval %u.\n",
infile, prec); infile, prec);
exit(-2); exit(EXIT_FAILURE);
} }
bpp = 0; bpp = 0;
if ((prec + 1) & 0xAAAAAAAA) bpp |= 1; if ((prec + 1) & 0xAAAAAAAA) bpp |= 1;
@ -339,7 +348,7 @@ main(int argc, char* argv[])
} }
out = TIFFOpen(argv[optind], "w"); out = TIFFOpen(argv[optind], "w");
if (out == NULL) if (out == NULL)
return (-4); return (EXIT_FAILURE);
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) w); TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) w);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h); TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h);
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
@ -379,13 +388,13 @@ main(int argc, char* argv[])
if (linebytes == 0) { if (linebytes == 0) {
fprintf(stderr, "%s: scanline size overflow\n", infile); fprintf(stderr, "%s: scanline size overflow\n", infile);
(void) TIFFClose(out); (void) TIFFClose(out);
exit(-2); exit(EXIT_FAILURE);
} }
scanline_size = TIFFScanlineSize(out); scanline_size = TIFFScanlineSize(out);
if (scanline_size == 0) { if (scanline_size == 0) {
/* overflow - TIFFScanlineSize already printed a message */ /* overflow - TIFFScanlineSize already printed a message */
(void) TIFFClose(out); (void) TIFFClose(out);
exit(-2); exit(EXIT_FAILURE);
} }
if (scanline_size < linebytes) if (scanline_size < linebytes)
buf = (unsigned char *)_TIFFmalloc(linebytes); buf = (unsigned char *)_TIFFmalloc(linebytes);
@ -394,7 +403,7 @@ main(int argc, char* argv[])
if (buf == NULL) { if (buf == NULL) {
fprintf(stderr, "%s: Not enough memory\n", infile); fprintf(stderr, "%s: Not enough memory\n", infile);
(void) TIFFClose(out); (void) TIFFClose(out);
exit(-2); exit(EXIT_FAILURE);
} }
if (resolution > 0) { if (resolution > 0) {
TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution); TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
@ -416,7 +425,7 @@ main(int argc, char* argv[])
(void) TIFFClose(out); (void) TIFFClose(out);
if (buf) if (buf)
_TIFFfree(buf); _TIFFfree(buf);
return (0); return (EXIT_SUCCESS);
} }
static void static void
@ -433,7 +442,7 @@ processG3Options(char* cp)
else if (strneq(cp, "fill", 4)) else if (strneq(cp, "fill", 4))
g3opts |= GROUP3OPT_FILLBITS; g3opts |= GROUP3OPT_FILLBITS;
else else
usage(); usage(EXIT_FAILURE);
} while( (cp = strchr(cp, ':')) ); } while( (cp = strchr(cp, ':')) );
} }
} }
@ -456,7 +465,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,':');
} }
@ -480,7 +489,7 @@ processCompressOptions(char* opt)
return (1); return (1);
} }
char* stuff[] = { const char* stuff[] = {
"usage: ppm2tiff [options] input.ppm output.tif", "usage: ppm2tiff [options] input.ppm output.tif",
"where options are:", "where options are:",
" -r # make each strip have no more than # rows", " -r # make each strip have no more than # rows",
@ -504,16 +513,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: */