summaryrefslogtreecommitdiffstats
path: root/src/H5system.c
diff options
context:
space:
mode:
authorDana Robinson <43805+derobins@users.noreply.github.com>2023-08-28 13:45:16 (GMT)
committerGitHub <noreply@github.com>2023-08-28 13:45:16 (GMT)
commit11f3fee390def4b6af0a3770db8a9f53f8805dfc (patch)
treed6e11c065b1b5c74cca10f74a97af8ac31d0ecf6 /src/H5system.c
parent8f9098b284b1551bae0fa9339eeeda64cc1d8641 (diff)
downloadhdf5-11f3fee390def4b6af0a3770db8a9f53f8805dfc.zip
hdf5-11f3fee390def4b6af0a3770db8a9f53f8805dfc.tar.gz
hdf5-11f3fee390def4b6af0a3770db8a9f53f8805dfc.tar.bz2
Bring strndup changes from develop (#3437)
Diffstat (limited to 'src/H5system.c')
-rw-r--r--src/H5system.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/H5system.c b/src/H5system.c
index 08a039b..6e55545 100644
--- a/src/H5system.c
+++ b/src/H5system.c
@@ -918,6 +918,50 @@ done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5_expand_windows_env_vars() */
+
+/*-------------------------------------------------------------------------
+ * Function: H5_strndup
+ *
+ * Purpose: Similar to strndup() for use on Windows. Allocates a new
+ * string and copies at most `n` bytes from the original
+ * string into the new string. If the original string is
+ * longer than `n`, only `n` bytes are copied from the
+ * original string. In either case, the string being returned
+ * is guaranteed to be terminated with a null byte.
+ *
+ * The returned pointer is allocated by H5MM_malloc in this
+ * routine and must be freed by the caller with H5MM_free or
+ * H5MM_xfree.
+ *
+ * Return: Pointer to copied string on success
+ * NULL on failure
+ *
+ *-------------------------------------------------------------------------
+ */
+char *
+H5_strndup(const char *s, size_t n)
+{
+ size_t len;
+ char *ret_value = NULL;
+
+ FUNC_ENTER_NOAPI_NOINIT
+
+ if (!s)
+ HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "string cannot be NULL")
+
+ for (len = 0; len < n && s[len] != '\0'; len++)
+ ;
+
+ if (NULL == (ret_value = H5MM_malloc(len + 1)))
+ HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, NULL, "can't allocate buffer for string")
+
+ H5MM_memcpy(ret_value, s, len);
+ ret_value[len] = '\0';
+
+done:
+ FUNC_LEAVE_NOAPI(ret_value)
+}
+
#endif /* H5_HAVE_WIN32_API */
/* Global variables */