From 2c6e8805ba226b8592863b4b248404dd218ad270 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sat, 7 Mar 2020 15:59:35 +0100 Subject: [PATCH] tiffgt: output usage on stdout with -h also use EXIT_SUCCESS / EXIT_FAILURE --- tools/tiffgt.c | 51 +++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/tools/tiffgt.c b/tools/tiffgt.c index 2f11b0ca..f3cca8c9 100644 --- a/tools/tiffgt.c +++ b/tools/tiffgt.c @@ -48,6 +48,13 @@ #include "tiffio.h" #include "tiffiop.h" +#ifndef EXIT_SUCCESS +#define EXIT_SUCCESS 0 +#endif +#ifndef EXIT_FAILURE +#define EXIT_FAILURE 1 +#endif + #ifndef HAVE_GETOPT extern int getopt(int argc, char * const argv[], const char *optstring); #endif @@ -68,12 +75,12 @@ static int filenum; static TIFFErrorHandler oerror; static TIFFErrorHandler owarning; -static void cleanup_and_exit(void); +static void cleanup_and_exit(int); static int initImage(void); static int prevImage(void); static int nextImage(void); static void setWindowSize(void); -static void usage(void); +static void usage(int); static uint16 photoArg(const char*); static void raster_draw(void); static void raster_reshape(int, int); @@ -102,7 +109,7 @@ main(int argc, char* argv[]) oerror = TIFFSetErrorHandler(NULL); owarning = TIFFSetWarningHandler(NULL); - while ((c = getopt(argc, argv, "d:o:p:eflmsvw?")) != -1) + while ((c = getopt(argc, argv, "d:o:p:eflmsvwh")) != -1) switch (c) { case 'd': dirnum = atoi(optarg); @@ -131,13 +138,16 @@ main(int argc, char* argv[]) case 'v': verbose = 1; break; + case 'h': + usage(EXIT_SUCCESS); + /*NOTREACHED*/ case '?': - usage(); + usage(EXIT_FAILURE); /*NOTREACHED*/ } filenum = argc - optind; if ( filenum < 1) - usage(); + usage(EXIT_FAILURE); glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); @@ -157,13 +167,13 @@ main(int argc, char* argv[]) filelist = (char **) _TIFFmalloc(filenum * sizeof(char*)); if (!filelist) { TIFFError(argv[0], "Can not allocate space for the file list."); - return 1; + return EXIT_FAILURE; } _TIFFmemcpy(filelist, argv + optind, filenum * sizeof(char*)); fileindex = -1; if (nextImage() < 0) { _TIFFfree(filelist); - return 2; + return EXIT_FAILURE; } /* * Set initial directory if user-specified @@ -177,7 +187,7 @@ main(int argc, char* argv[]) photo = photo0; if (initImage() < 0){ _TIFFfree(filelist); - return 3; + return EXIT_FAILURE; } /* * Create a new window or reconfigure an existing @@ -193,12 +203,12 @@ main(int argc, char* argv[]) glutSpecialFunc(raster_special); glutMainLoop(); - cleanup_and_exit(); - return 0; + cleanup_and_exit(EXIT_SUCCESS); + return EXIT_SUCCESS; } static void -cleanup_and_exit(void) +cleanup_and_exit(int code) { TIFFRGBAImageEnd(&img); if (filelist != NULL) @@ -207,7 +217,7 @@ cleanup_and_exit(void) _TIFFfree(raster); if (tif != NULL) TIFFClose(tif); - exit(0); + exit(code); } static int @@ -250,7 +260,7 @@ initImage(void) if (raster == NULL) { width = height = 0; TIFFError(filelist[fileindex], "No space for raster buffer"); - cleanup_and_exit(); + cleanup_and_exit(EXIT_FAILURE); } width = w; height = h; @@ -361,7 +371,7 @@ raster_keys(unsigned char key, int x, int y) break; case 'q': /* exit */ case '\033': - cleanup_and_exit(); + cleanup_and_exit(EXIT_SUCCESS); } glutPostRedisplay(); } @@ -422,7 +432,7 @@ raster_special(int key, int x, int y) # pragma GCC diagnostic pop # endif -char* stuff[] = { +static const char* stuff[] = { "usage: tiffgt [options] file.tif", "where options are:", " -c use colormap visual", @@ -440,16 +450,15 @@ NULL }; static void -usage(void) +usage(int code) { - char buf[BUFSIZ]; int i; + FILE * out = (code == EXIT_SUCCESS) ? stdout : stderr; - setbuf(stderr, buf); - fprintf(stderr, "%s\n\n", TIFFGetVersion()); + fprintf(out, "%s\n\n", TIFFGetVersion()); for (i = 0; stuff[i] != NULL; i++) - fprintf(stderr, "%s\n", stuff[i]); - exit(-1); + fprintf(out, "%s\n", stuff[i]); + exit(code); } static uint16