Repository: Unidata/netcdf-c (c)

Issue:
dpathmgr.c: error: ‘path’ undeclared when cross-compiling netCDF 4.8.0 
I am trying to update the netCDF Julia package to the current version. 


* the version of the software with which you are encountering an issue
NetCDF 4.8.0

* environmental information (i.e. Operating System, compiler info, java version, python version, etc.)
Cross-compilation from Linux to Windows (x86_64-w64-mingw32) in Julia's build tree [Yggdrasil](https://github.com/JuliaPackaging/Yggdrasil). 

* a description of the issue with the steps needed to reproduce it

NetCDF is cross-compiled using the following steps (line 26-46)
https://github.com/JuliaPackaging/Yggdrasil/blob/7a37fabb2eb7c680766aea06af77a055183c92f1/N/NetCDF/common.jl#L26

In addition, we used the option `CPPFLAGS=-DH5_USE_110_API`.
(https://github.com/Unidata/netcdf-c/issues/1978#issuecomment-817224097 )

Compiliation succeeds on Linux and Mac OS, but fails on Windows with the error:
```
2021-05-17T15:55:38.3906619Z [15:55:38] dpathmgr.c: In function ‘NCclosedir’:
2021-05-17T15:55:38.3907622Z [15:55:38] (first use in this function)
2021-05-17T15:55:38.3908452Z [15:55:38]      char* cvtname = NCpathcvt(path);
2021-05-17T15:55:38.3909108Z [15:55:38]                                ^
2021-05-17T15:55:38.3909968Z [15:55:38] dpathmgr.c:359:31: note: each undeclared identifier is reported only once for each function it appears in
```
For reference the full log files is attached. I am not sure if this error is related to the option `-DH5_USE_110_API`.
Do you have any idea what could be the issue?
Thanks a lot for any insight!


[44.txt](https://github.com/Unidata/netcdf-c/files/6496519/44.txt) 

Pull request:
Fix NCclosedir in dpathmgr.c
* Fixes #1999 

re: Issue https://github.com/Unidata/netcdf-c/issues/1999

NCclosedir code is incorrect. Fix.
Note that this issue crops up when using a non-VisualStudio windows build
such as Mingw because Mingw defines dirent.h, but Visual Studio does not.

Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index adb93ef18..b2e126f3d 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -6,6 +6,7 @@ Release Notes       {#RELEASE_NOTES}
 This file contains a high-level description of this package's evolution. Releases are in reverse chronological order (most recent first). Note that, as of netcdf 4.2, the `netcdf-c++` and `netcdf-fortran` libraries have been separated into their own libraries.
 ## 4.8.1 - TBD
 
+* [Bug Fix] Fix bug in NCclosedir in dpathmgr.c. See [Github #2003](https://github.com/Unidata/netcdf-c/issues/2003).
 * [Bug Fix] Fix bug in ncdump that assumes that there is a relationship between the total number of dimensions and the max dimension id. See [Github #2004](https://github.com/Unidata/netcdf-c/issues/2004).  
 * [Bug Fix] Fix bug in JSON processing of strings with embedded quotes. See [Github #1993](https://github.com/Unidata/netcdf-c/issues/1993).  
 * [Enhancement] Add support for the new "dimension_separator" enhancement to Zarr v2. See [Github #1990](https://github.com/Unidata/netcdf-c/pull/1990) for more information.
diff --git a/configure.ac b/configure.ac
index 25598e9de..1283aa75a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1095,10 +1095,16 @@ AC_C_BIGENDIAN
 # Figure out platforms of special interest
 case "`uname`" in
   CYGWIN*) ISCYGWIN=yes;;
+  Darwin*) ISOSX=yes;;
   WIN*) ISMSVC=yes;;
 esac
 AM_CONDITIONAL(ISCYGWIN, [test "x$ISCYGWIN" = xyes])
 AM_CONDITIONAL(ISMSVC, [test "x$ISMSVC" = xyes])
+AM_CONDITIONAL(ISOSX, [test "x$ISOSX" = xyes])
+
+AC_SUBST([ISMSVC], [${ISMSVC}])
+AC_SUBST([ISCYGWIN], [${ISCYGWIN}])
+AC_SUBST([ISOSX], [${ISOSX}])
 
 ###
 # Crude hack to work around an issue
@@ -1722,8 +1728,6 @@ AC_DEFINE_UNQUOTED([NC_DISPATCH_VERSION], [${NC_DISPATCH_VERSION}], [Dispatch ta
 # End netcdf_meta.h definitions.
 #####
 
-# This would be true for a visual studio build.
-AC_SUBST([ISMSVC], [${ISMSVC}])
 # This would be true for a cmake build.
 AC_SUBST([ISCMAKE], [])
 
diff --git a/include/ncpathmgr.h b/include/ncpathmgr.h
index 0245a40ad..631327b7e 100644
--- a/include/ncpathmgr.h
+++ b/include/ncpathmgr.h
@@ -14,6 +14,9 @@
 #ifdef HAVE_DIRENT_H
 #include <dirent.h>
 #endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
@@ -133,11 +136,11 @@ EXTERNL int NCclosedir(DIR* ent);
 #define NCaccess(path,mode) access(path,mode)
 #define NCmkdir(path,mode) mkdir(path,mode)
 #define NCgetcwd(buf,len) getcwd(buf,len)
+#define NCcwd(buf, len) getcwd(buf,len)
+#define NCrmdir(path) rmdir(path)
 #ifdef HAVE_SYS_STAT_H
 #define NCstat(path,buf) stat(path,buf)
 #endif
-#define NCcwd(buf, len) getcwd(buf,len)
-#define NCrmdir(path) rmdir(path)
 #ifdef HAVE_DIRENT_H
 #define NCopendir(path) opendir(path)
 #define NCclosedir(ent) closedir(ent)
diff --git a/libdispatch/dpathmgr.c b/libdispatch/dpathmgr.c
index 0a3e312eb..6ab6588af 100644
--- a/libdispatch/dpathmgr.c
+++ b/libdispatch/dpathmgr.c
@@ -356,10 +356,7 @@ int
 NCclosedir(DIR* ent)
 {
     int stat = NC_NOERR;
-    char* cvtname = NCpathcvt(path);
-    if(cvtname == NULL) {errno = ENOENT; return -1;}
-    stat = closedir(cvtname);
-    free(cvtname);    
+    if(closedir(ent) < 0) stat = errno;
     return stat;
 }
 #endif
@@ -911,33 +908,3 @@ printutf8hex(const char* s, char* sx)
     }
     *q = '\0';
 }
-
-/**************************************************/
-#if 0
-#ifdef HAVE_DIRENT_H
-EXTERNL
-DIR*
-NCopendir(const char* path)
-{
-    DIR* ent = NULL;
-    char* cvtpath = NCpathcvt(path);
-    if(cvtpath == NULL) return -1;
-    ent = opendir(cvtpath);
-    free(cvtpath);    
-    return ent;
-}
-
-EXTERNL
-int
-NCclosedir(DIR* ent)
-{
-    int stat = 0;
-    char* cvtpath = NCpathcvt(path);
-    if(cvtpath == NULL) return -1;
-    stat = closedir(cvtpath);
-    free(cvtpath);    
-    return stat;
-}
-#endif
-#endif /*0*/
-
diff --git a/libnczarr/zdebug.h b/libnczarr/zdebug.h
index f6f12dc72..a7cbd5be2 100644
--- a/libnczarr/zdebug.h
+++ b/libnczarr/zdebug.h
@@ -9,7 +9,7 @@
 #undef ZDEBUG1 /* detailed debug */
 
 #undef ZCATCH /* Warning: significant performance impact */
-#define ZTRACING /* Warning: significant performance impact */
+#undef ZTRACING /* Warning: significant performance impact */
 
 #include "ncexternl.h"
 #include "nclog.h"
diff --git a/libnczarr/zincludes.h b/libnczarr/zincludes.h
index 137986408..893c2b04c 100644
--- a/libnczarr/zincludes.h
+++ b/libnczarr/zincludes.h
@@ -26,7 +26,7 @@
 #include <unistd.h>
 #endif
 #endif
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include <malloc.h>
 #endif
 
diff --git a/libnczarr/zmap.c b/libnczarr/zmap.c
index 19e19a2cb..ed162dead 100644
--- a/libnczarr/zmap.c
+++ b/libnczarr/zmap.c
@@ -377,13 +377,16 @@ nczm_localize(const char* path, char** localpathp, int localize)
     char* p;
     int forward = 1;
     int offset = 0;
+    static const char* windrive = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
 #ifdef _MSC_VER
     forward = (localize?0:1);
 #endif
     /* If path comes from a url, then it may start with: /x:/...
        where x is a drive letter. If so, then remove leading / */
-    if(path[0] == '/' && NChasdriveletter(path+1))
+    if(strlen(path) >= 4
+       && path[0] == '/' && strchr(windrive,path[1]) != NULL
+       && path[2] == ':' && path[3] == '/')
 	offset = 1;
     if((localpath = strdup(path+offset))==NULL) return NC_ENOMEM;
 
diff --git a/ncdump/Makefile.am b/ncdump/Makefile.am
index b5cd0438b..017a00d99 100644
--- a/ncdump/Makefile.am
+++ b/ncdump/Makefile.am
@@ -66,7 +66,7 @@ bom tst_dimsizes nctrunc tst_rcmerge
 # Tests for classic and 64-bit offset files.
 TESTS = tst_inttags.sh run_tests.sh tst_64bit.sh ref_ctest	\
 ref_ctest64 tst_output.sh tst_lengths.sh tst_calendars.sh	\
-run_utf8_tests.sh test_unicode_directory.sh tst_nccopy3.sh tst_nccopy3_subset.sh		\
+run_utf8_tests.sh tst_nccopy3.sh tst_nccopy3_subset.sh		\
 tst_charfill.sh tst_iter.sh tst_formatx3.sh tst_bom.sh		\
 tst_dimsizes.sh run_ncgen_tests.sh tst_ncgen4_classic.sh test_radix.sh test_rcmerge.sh
 
@@ -80,6 +80,10 @@ if USE_STRICT_NULL_BYTE_HEADER_PADDING
 XFAIL_TESTS += tst_null_byte_padding.sh
 endif
 
+if ! ISCYGWIN
+TESTS += test_unicode_directory.sh 
+endif
+
 if LARGE_FILE_TESTS
 TESTS += tst_iter.sh
 endif
diff --git a/ncdump/nccopy.c b/ncdump/nccopy.c
index bf9b6dd5e..cfa99178c 100644
--- a/ncdump/nccopy.c
+++ b/ncdump/nccopy.c
@@ -13,7 +13,7 @@
 #include <getopt.h>
 #endif
 
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include "XGetopt.h"
 #define snprintf _snprintf
 #endif
diff --git a/ncdump/ncdump.c b/ncdump/ncdump.c
index b6981a53c..ea6e44d1d 100644
--- a/ncdump/ncdump.c
+++ b/ncdump/ncdump.c
@@ -8,7 +8,7 @@ Research/Unidata. See \ref copyright file for more info.  */
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
 #endif
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include "XGetopt.h"
 #define snprintf _snprintf
 #endif
diff --git a/ncdump/ncvalidator.c b/ncdump/ncvalidator.c
index 3cb6353f8..b878a622c 100644
--- a/ncdump/ncvalidator.c
+++ b/ncdump/ncvalidator.c
@@ -73,7 +73,7 @@ THIS SOFTWARE.
 #include <unistd.h>     /* read() getopt() */
 #endif
 
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include <io.h>
 #include "XGetopt.h"
 #define snprintf _snprintf
diff --git a/ncdump/ocprint.c b/ncdump/ocprint.c
index 270aba1c2..a31c43c8c 100755
--- a/ncdump/ocprint.c
+++ b/ncdump/ocprint.c
@@ -31,7 +31,7 @@
 #include "oc.h"
 #include "ocx.h"
 
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include "XGetopt.h"
 #endif
 
diff --git a/ncgen/main.c b/ncgen/main.c
index 9ed31d47e..3ec53817e 100644
--- a/ncgen/main.c
+++ b/ncgen/main.c
@@ -12,7 +12,7 @@
 #include <getopt.h>
 #endif
 
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include "XGetopt.h"
 #endif
 
diff --git a/ncgen3/main.c b/ncgen3/main.c
index 9c148cc6a..263bd699b 100644
--- a/ncgen3/main.c
+++ b/ncgen3/main.c
@@ -15,7 +15,7 @@
 #include <getopt.h>
 #endif
 
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include "XGetopt.h"
 #define snprintf _snprintf
 #endif


Test patch:
diff --git a/dap4_test/dump.c b/dap4_test/dump.c
index cc17356ab..6c322ef17 100644
--- a/dap4_test/dump.c
+++ b/dap4_test/dump.c
@@ -15,7 +15,7 @@
 #include <getopt.h>
 #endif
 
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include "XGetopt.h"
 #define snprintf _snprintf
 #endif
diff --git a/nc_test4/findplugin.in b/nc_test4/findplugin.in
index aafe051de..981965968 100644
--- a/nc_test4/findplugin.in
+++ b/nc_test4/findplugin.in
@@ -27,14 +27,8 @@ FP_NAME="$1"
 # Figure out the compiler (some values from ./configure)
 FP_ISCMAKE=@ISCMAKE@
 FP_ISMSVC=@ISMSVC@
-
-# Are we operating under OS-X? (test using uname)
-FP_OS=`uname | cut -d '_'  -f 1`
-if test "x$FP_OS" = xDarwin ; then FP_ISOSX=1; fi
-
-# Are we operating under CYGWIN? (test using uname)
-FP_OS=`uname | cut -d '_'  -f 1`
-if test "x$FP_OS" = xCYGWIN ; then FP_ISCYGWIN=1; fi
+FP_ISCYGWIN=@ISCYGWIN@
+FP_ISOSX=@ISOSX@
 
 FP_PLUGINS="$TOPBUILDDIR/plugins"
 
diff --git a/nczarr_test/bm_utils.c b/nczarr_test/bm_utils.c
index 22717d77e..8349407b9 100644
--- a/nczarr_test/bm_utils.c
+++ b/nczarr_test/bm_utils.c
@@ -15,7 +15,7 @@
 #include <getopt.h>
 #endif
 
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include "XGetopt.h"
 #endif
 
diff --git a/nczarr_test/test_nczarr_utils.h b/nczarr_test/test_nczarr_utils.h
index 50f52dab4..1588ee316 100644
--- a/nczarr_test/test_nczarr_utils.h
+++ b/nczarr_test/test_nczarr_utils.h
@@ -21,7 +21,7 @@
 #include <getopt.h>
 #endif
 
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include "XGetopt.h"
 #endif
 
diff --git a/nczarr_test/ut_test.c b/nczarr_test/ut_test.c
index 9c376d7e4..8482df815 100755
--- a/nczarr_test/ut_test.c
+++ b/nczarr_test/ut_test.c
@@ -11,7 +11,7 @@ x *      Copyright 2018, University Corporation for Atmospheric Research
 #include <getopt.h>
 #endif
 
-#ifdef _MSC_VER
+#ifdef _WIN32
 #include "XGetopt.h"
 #endif
 
diff --git a/nczarr_test/zs3parse.c b/nczarr_test/zs3parse.c
index 9ec5476c3..4056daf45 100644
--- a/nczarr_test/zs3parse.c
+++ b/nczarr_test/zs3parse.c
@@ -5,6 +5,8 @@
 
 #include "config.h"
 
+#include <stdlib.h>
+#include <stdio.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
diff --git a/test_common.in b/test_common.in
index 539be6fdf..5e36b301b 100644
--- a/test_common.in
+++ b/test_common.in
@@ -10,6 +10,7 @@ TOPSRCDIR='@abs_top_srcdir@'
 TOPBUILDDIR='@abs_top_builddir@'
 FP_ISCMAKE=@ISCMAKE@
 FP_ISMSVC=@ISMSVC@
+FP_ISCYGWIN=@ISCYGWIN@
 
 # Feature flags
 FEATURE_HDF5=@HAS_HDF5@