diff --git a/tests/bufr_compare.sh b/tests/bufr_compare.sh index faa42a6e4..ecb24c11d 100755 --- a/tests/bufr_compare.sh +++ b/tests/bufr_compare.sh @@ -109,9 +109,19 @@ status=$? set -e [ $status -eq 1 ] +#---------------------------------------------------- +# Compare file with directory +#---------------------------------------------------- +temp_dir=tempdir.bufr_compare +mkdir -p $temp_dir +infile=aaen_55.bufr +cp $infile $temp_dir +${tools_dir}bufr_compare $infile $temp_dir + + #Clean up rm -f $fLog rm -f $fBufrTmp | true rm -f $fBufrInput1 | true rm -f $fBufrInput2 | true - +rm -rf $temp_dir diff --git a/tests/compare.sh b/tests/compare.sh index 92ad6914e..79c9b49f3 100755 --- a/tests/compare.sh +++ b/tests/compare.sh @@ -8,7 +8,6 @@ # virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. # - . ./include.sh REDIRECT=/dev/null @@ -33,5 +32,12 @@ cat temp_comp.3 temp_comp.2 temp_comp.1 > temp_comp.321 # Compare files in which the messages are not in the same order ${tools_dir}grib_compare -r temp_comp.123 temp_comp.321 +# GRIB-797: test last argument being a directory +temp_dir=tempdir.grib_compare +mkdir -p $temp_dir +cp $infile $temp_dir +${tools_dir}grib_compare $infile $temp_dir +rm -rf $temp_dir + rm -f temp_comp.1 temp_comp.2 temp_comp.3 temp_comp.123 temp_comp.321 rm -f $outfile || true diff --git a/tools/bufr_compare.c b/tools/bufr_compare.c index 118640b49..512eeb111 100644 --- a/tools/bufr_compare.c +++ b/tools/bufr_compare.c @@ -16,6 +16,12 @@ #include "grib_tools.h" +static const char* basename(const char* path) +{ + char* s = strrchr(path, '/'); + if (!s) return path; + else return s + 1; +} GRIB_INLINE static int grib_inline_strcmp(const char* a,const char* b) { @@ -324,6 +330,22 @@ int grib_tool_init(grib_runtime_options* options) if (grib_options_on("t:")) tolerance_factor=atof(grib_options_get_option("t:")); + { + /* Check for 2nd file being a directory. If so, we assume user is comparing to a file */ + /* with the same name as first file in that directory */ + struct stat s; + grib_tools_file* infile = options->infile; /* the 2nd file in comparison */ + if (infile) { + int stat_val = stat(infile->name, &s); + if ( stat_val == 0 && S_ISDIR(s.st_mode)) { + /* Take the filename of the 1st file and append to dir */ + char bufr[2048] = {0,}; + /* options->infile_extra->name is the 1st file */ + sprintf(bufr, "%s/%s", infile->name, basename(options->infile_extra->name)); + infile->name = strdup(bufr); + } + } + } return 0; } diff --git a/tools/grib_compare.c b/tools/grib_compare.c index 1793e4543..1ebef0566 100644 --- a/tools/grib_compare.c +++ b/tools/grib_compare.c @@ -10,6 +10,13 @@ #include "grib_tools.h" +static const char* basename(const char* path) +{ + char* s = strrchr(path, '/'); + if (!s) return path; + else return s + 1; +} + GRIB_INLINE static int grib_inline_strcmp(const char* a,const char* b) { if (*a != *b) return 1; @@ -283,6 +290,23 @@ int grib_tool_init(grib_runtime_options* options) } } + { + /* Check for 2nd file being a directory. If so, we assume user is comparing to a file */ + /* with the same name as first file in that directory */ + struct stat s; + grib_tools_file* infile = options->infile; /* the 2nd file in comparison */ + if (infile) { + int stat_val = stat(infile->name, &s); + if ( stat_val == 0 && S_ISDIR(s.st_mode)) { + /* Take the filename of the 1st file and append to dir */ + char bufr[2048] = {0,}; + /* options->infile_extra->name is the 1st file */ + sprintf(bufr, "%s/%s", infile->name, basename(options->infile_extra->name)); + infile->name = strdup(bufr); + } + } + } + return 0; }