tiffset: print usage on stdout when -h is used

also use EXIT_FAILURE / EXIT_SUCCESS
see #17
This commit is contained in:
Thomas Bernard 2020-03-07 16:28:27 +01:00
parent 9bf9cd3926
commit 2fa1ce2f55
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -39,7 +39,14 @@
# include "libport.h" # include "libport.h"
#endif #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", "usage: tiffset [options] filename",
"where options are:", "where options are:",
" -s <tagname> [count] <value>... set the tag value", " -s <tagname> [count] <value>... set the tag value",
@ -47,16 +54,20 @@ static char* usageMsg[] = {
" -d <dirno> set the directory", " -d <dirno> set the directory",
" -sd <diroff> set the subdirectory", " -sd <diroff> set the subdirectory",
" -sf <tagname> <filename> read the tag value from file (for ASCII tags only)", " -sf <tagname> <filename> read the tag value from file (for ASCII tags only)",
" -h this help screen",
NULL NULL
}; };
static void static void
usage(void) usage(int code)
{ {
int i; int i;
FILE * out = (code == EXIT_SUCCESS) ? stdout : stderr;
fprintf(out, "%s\n\n", TIFFGetVersion());
for (i = 0; usageMsg[i]; i++) for (i = 0; usageMsg[i]; i++)
fprintf(stderr, "%s\n", usageMsg[i]); fprintf(out, "%s\n", usageMsg[i]);
exit(-1); exit(code);
} }
static const TIFFField * static const TIFFField *
@ -84,11 +95,11 @@ main(int argc, char* argv[])
int arg_index; int arg_index;
if (argc < 2) if (argc < 2)
usage(); usage(EXIT_FAILURE);
tiff = TIFFOpen(argv[argc-1], "r+"); tiff = TIFFOpen(argv[argc-1], "r+");
if (tiff == NULL) if (tiff == NULL)
return 2; return EXIT_FAILURE;
for( arg_index = 1; arg_index < argc-1; arg_index++ ) { for( arg_index = 1; arg_index < argc-1; arg_index++ ) {
if (strcmp(argv[arg_index],"-d") == 0 && arg_index < argc-2) { 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 ) if( TIFFSetDirectory(tiff, atoi(argv[arg_index]) ) != 1 )
{ {
fprintf( stderr, "Failed to set directory=%s\n", argv[arg_index] ); fprintf( stderr, "Failed to set directory=%s\n", argv[arg_index] );
return 6; return EXIT_FAILURE;
} }
arg_index++; arg_index++;
} }
@ -105,7 +116,7 @@ main(int argc, char* argv[])
if( TIFFSetSubDirectory(tiff, atoi(argv[arg_index]) ) != 1 ) if( TIFFSetSubDirectory(tiff, atoi(argv[arg_index]) ) != 1 )
{ {
fprintf( stderr, "Failed to set sub directory=%s\n", argv[arg_index] ); fprintf( stderr, "Failed to set sub directory=%s\n", argv[arg_index] );
return 7; return EXIT_FAILURE;
} }
arg_index++; arg_index++;
} }
@ -117,7 +128,7 @@ main(int argc, char* argv[])
tagname = argv[arg_index]; tagname = argv[arg_index];
fip = GetField(tiff, tagname); fip = GetField(tiff, tagname);
if (!fip) if (!fip)
return 3; return EXIT_FAILURE;
if (TIFFUnsetField(tiff, TIFFFieldTag(fip)) != 1) if (TIFFUnsetField(tiff, TIFFFieldTag(fip)) != 1)
{ {
@ -155,7 +166,7 @@ main(int argc, char* argv[])
"Number of tag values is not enough. " "Number of tag values is not enough. "
"Expected %d values for %s tag, got %d\n", "Expected %d values for %s tag, got %d\n",
wc, TIFFFieldName(fip), argc - arg_index); wc, TIFFFieldName(fip), argc - arg_index);
return 4; return EXIT_FAILURE;
} }
if (wc > 1 || TIFFFieldWriteCount(fip) == TIFF_VARIABLE) { if (wc > 1 || TIFFFieldWriteCount(fip) == TIFF_VARIABLE) {
@ -204,7 +215,7 @@ main(int argc, char* argv[])
if (!array) { if (!array) {
fprintf(stderr, "No space for %s tag\n", fprintf(stderr, "No space for %s tag\n",
tagname); tagname);
return 4; return EXIT_FAILURE;
} }
switch (TIFFFieldDataType(fip)) { switch (TIFFFieldDataType(fip)) {
@ -327,13 +338,13 @@ main(int argc, char* argv[])
fip = GetField(tiff, argv[arg_index]); fip = GetField(tiff, argv[arg_index]);
if (!fip) if (!fip)
return 3; return EXIT_FAILURE;
if (TIFFFieldDataType(fip) != TIFF_ASCII) { if (TIFFFieldDataType(fip) != TIFF_ASCII) {
fprintf( stderr, fprintf( stderr,
"Only ASCII tags can be set from file. " "Only ASCII tags can be set from file. "
"%s is not ASCII tag.\n", TIFFFieldName(fip) ); "%s is not ASCII tag.\n", TIFFFieldName(fip) );
return 5; return EXIT_FAILURE;
} }
arg_index++; arg_index++;
@ -367,16 +378,18 @@ main(int argc, char* argv[])
_TIFFfree( text ); _TIFFfree( text );
arg_index++; arg_index++;
} else if (strcmp(argv[arg_index],"-h") == 0 || strcmp(argv[arg_index],"--help") == 0) {
usage(EXIT_SUCCESS);
} else { } else {
fprintf(stderr, "Unrecognised option: %s\n", fprintf(stderr, "Unrecognised option: %s\n",
argv[arg_index]); argv[arg_index]);
usage(); usage(EXIT_FAILURE);
} }
} }
TIFFRewriteDirectory(tiff); TIFFRewriteDirectory(tiff);
TIFFClose(tiff); TIFFClose(tiff);
return 0; return EXIT_SUCCESS;
} }
/* vim: set ts=8 sts=8 sw=8 noet: */ /* vim: set ts=8 sts=8 sw=8 noet: */