From 2fa1ce2f554b4f0c492b8b2b268870bb5511f053 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sat, 7 Mar 2020 16:28:27 +0100 Subject: [PATCH] tiffset: print usage on stdout when -h is used also use EXIT_FAILURE / EXIT_SUCCESS see #17 --- tools/tiffset.c | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/tools/tiffset.c b/tools/tiffset.c index 3725aa61..75cf45c0 100644 --- a/tools/tiffset.c +++ b/tools/tiffset.c @@ -39,7 +39,14 @@ # include "libport.h" #endif -static char* usageMsg[] = { +#ifndef EXIT_SUCCESS +#define EXIT_SUCCESS 0 +#endif +#ifndef EXIT_FAILURE +#define EXIT_FAILURE 1 +#endif + +static const char* usageMsg[] = { "usage: tiffset [options] filename", "where options are:", " -s [count] ... set the tag value", @@ -47,16 +54,20 @@ static char* usageMsg[] = { " -d set the directory", " -sd set the subdirectory", " -sf read the tag value from file (for ASCII tags only)", +" -h this help screen", NULL }; static void -usage(void) +usage(int code) { int i; + FILE * out = (code == EXIT_SUCCESS) ? stdout : stderr; + + fprintf(out, "%s\n\n", TIFFGetVersion()); for (i = 0; usageMsg[i]; i++) - fprintf(stderr, "%s\n", usageMsg[i]); - exit(-1); + fprintf(out, "%s\n", usageMsg[i]); + exit(code); } static const TIFFField * @@ -84,11 +95,11 @@ main(int argc, char* argv[]) int arg_index; if (argc < 2) - usage(); + usage(EXIT_FAILURE); tiff = TIFFOpen(argv[argc-1], "r+"); if (tiff == NULL) - return 2; + return EXIT_FAILURE; for( arg_index = 1; arg_index < argc-1; arg_index++ ) { if (strcmp(argv[arg_index],"-d") == 0 && arg_index < argc-2) { @@ -96,7 +107,7 @@ main(int argc, char* argv[]) if( TIFFSetDirectory(tiff, atoi(argv[arg_index]) ) != 1 ) { fprintf( stderr, "Failed to set directory=%s\n", argv[arg_index] ); - return 6; + return EXIT_FAILURE; } arg_index++; } @@ -105,7 +116,7 @@ main(int argc, char* argv[]) if( TIFFSetSubDirectory(tiff, atoi(argv[arg_index]) ) != 1 ) { fprintf( stderr, "Failed to set sub directory=%s\n", argv[arg_index] ); - return 7; + return EXIT_FAILURE; } arg_index++; } @@ -117,7 +128,7 @@ main(int argc, char* argv[]) tagname = argv[arg_index]; fip = GetField(tiff, tagname); if (!fip) - return 3; + return EXIT_FAILURE; if (TIFFUnsetField(tiff, TIFFFieldTag(fip)) != 1) { @@ -155,7 +166,7 @@ main(int argc, char* argv[]) "Number of tag values is not enough. " "Expected %d values for %s tag, got %d\n", wc, TIFFFieldName(fip), argc - arg_index); - return 4; + return EXIT_FAILURE; } if (wc > 1 || TIFFFieldWriteCount(fip) == TIFF_VARIABLE) { @@ -204,7 +215,7 @@ main(int argc, char* argv[]) if (!array) { fprintf(stderr, "No space for %s tag\n", tagname); - return 4; + return EXIT_FAILURE; } switch (TIFFFieldDataType(fip)) { @@ -327,13 +338,13 @@ main(int argc, char* argv[]) fip = GetField(tiff, argv[arg_index]); if (!fip) - return 3; + return EXIT_FAILURE; if (TIFFFieldDataType(fip) != TIFF_ASCII) { fprintf( stderr, "Only ASCII tags can be set from file. " "%s is not ASCII tag.\n", TIFFFieldName(fip) ); - return 5; + return EXIT_FAILURE; } arg_index++; @@ -367,16 +378,18 @@ main(int argc, char* argv[]) _TIFFfree( text ); arg_index++; + } else if (strcmp(argv[arg_index],"-h") == 0 || strcmp(argv[arg_index],"--help") == 0) { + usage(EXIT_SUCCESS); } else { fprintf(stderr, "Unrecognised option: %s\n", argv[arg_index]); - usage(); + usage(EXIT_FAILURE); } } TIFFRewriteDirectory(tiff); TIFFClose(tiff); - return 0; + return EXIT_SUCCESS; } /* vim: set ts=8 sts=8 sw=8 noet: */