summaryrefslogtreecommitdiffstats
path: root/src/H5system.c
diff options
context:
space:
mode:
authorLarry Knox <lrknox@hdfgroup.org>2023-03-24 23:42:46 (GMT)
committerGitHub <noreply@github.com>2023-03-24 23:42:46 (GMT)
commit43e4e64d886e9072a6075c6369e84c0e273fa44f (patch)
treea65896c37e5af4919e8cb169c2b40c44ad07017e /src/H5system.c
parent3fa338013907494ccfe93b8e22d89185a39067ff (diff)
downloadhdf5-43e4e64d886e9072a6075c6369e84c0e273fa44f.zip
hdf5-43e4e64d886e9072a6075c6369e84c0e273fa44f.tar.gz
hdf5-43e4e64d886e9072a6075c6369e84c0e273fa44f.tar.bz2
1 10 revert 2615 (#2629)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Diffstat (limited to 'src/H5system.c')
-rw-r--r--src/H5system.c162
1 files changed, 142 insertions, 20 deletions
diff --git a/src/H5system.c b/src/H5system.c
index 08a039b..8048f50 100644
--- a/src/H5system.c
+++ b/src/H5system.c
@@ -96,6 +96,131 @@ HDvasprintf(char **bufp, const char *fmt, va_list _ap)
#endif /* H5_HAVE_VASPRINTF */
/*-------------------------------------------------------------------------
+ * Function: HDstrtoll
+ *
+ * Purpose: Converts the string S to an int64_t value according to the
+ * given BASE, which must be between 2 and 36 inclusive, or be
+ * the special value zero.
+ *
+ * The string must begin with an arbitrary amount of white space
+ * (as determined by isspace(3c)) followed by a single optional
+ * `+' or `-' sign. If BASE is zero or 16 the string may then
+ * include a `0x' or `0X' prefix, and the number will be read in
+ * base 16; otherwise a zero BASE is taken as 10 (decimal)
+ * unless the next character is a `0', in which case it is taken
+ * as 8 (octal).
+ *
+ * The remainder of the string is converted to an int64_t in the
+ * obvious manner, stopping at the first character which is not
+ * a valid digit in the given base. (In bases above 10, the
+ * letter `A' in either upper or lower case represetns 10, `B'
+ * represents 11, and so forth, with `Z' representing 35.)
+ *
+ * If REST is not null, the address of the first invalid
+ * character in S is stored in *REST. If there were no digits
+ * at all, the original value of S is stored in *REST. Thus, if
+ * *S is not `\0' but **REST is `\0' on return the entire string
+ * was valid.
+ *
+ * Return: Success: The result.
+ *
+ * Failure: If the input string does not contain any
+ * digits then zero is returned and REST points
+ * to the original value of S. If an overflow
+ * or underflow occurs then the maximum or
+ * minimum possible value is returned and the
+ * global `errno' is set to ERANGE. If BASE is
+ * incorrect then zero is returned.
+ *
+ * Programmer: Robb Matzke
+ * Thursday, April 9, 1998
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef HDstrtoll
+int64_t
+HDstrtoll(const char *s, const char **rest, int base)
+{
+ int64_t sign = 1, acc = 0;
+ hbool_t overflow = FALSE;
+
+ errno = 0;
+ if (!s || (base && (base < 2 || base > 36))) {
+ if (rest)
+ *rest = s;
+ return 0;
+ }
+
+ /* Skip white space */
+ while (HDisspace(*s))
+ s++;
+
+ /* Optional minus or plus sign */
+ if ('+' == *s) {
+ s++;
+ }
+ else if ('-' == *s) {
+ sign = -1;
+ s++;
+ }
+
+ /* Zero base prefix */
+ if (0 == base && '0' == *s && ('x' == s[1] || 'X' == s[1])) {
+ base = 16;
+ s += 2;
+ }
+ else if (0 == base && '0' == *s) {
+ base = 8;
+ s++;
+ }
+ else if (0 == base) {
+ base = 10;
+ }
+
+ /* Digits */
+ while ((base <= 10 && *s >= '0' && *s < '0' + base) ||
+ (base > 10 && ((*s >= '0' && *s <= '9') || (*s >= 'a' && *s < 'a' + base - 10) ||
+ (*s >= 'A' && *s < 'A' + base - 10)))) {
+ if (!overflow) {
+ int64_t digit = 0;
+
+ if (*s >= '0' && *s <= '9')
+ digit = *s - '0';
+ else if (*s >= 'a' && *s <= 'z')
+ digit = (*s - 'a') + 10;
+ else
+ digit = (*s - 'A') + 10;
+
+ if (acc * base + digit < acc) {
+ overflow = TRUE;
+ }
+ else {
+ acc = acc * base + digit;
+ }
+ }
+ s++;
+ }
+
+ /* Overflow */
+ if (overflow) {
+ if (sign > 0) {
+ acc = ((uint64_t)1 << (8 * sizeof(int64_t) - 1)) - 1;
+ }
+ else {
+ acc = (int64_t)((uint64_t)1 << (8 * sizeof(int64_t) - 1));
+ }
+ errno = ERANGE;
+ }
+
+ /* Return values */
+ acc *= sign;
+ if (rest)
+ *rest = s;
+ return acc;
+} /* end HDstrtoll() */
+#endif
+
+/*-------------------------------------------------------------------------
* Function: HDrand/HDsrand
*
* Purpose: Wrapper function for rand. If rand_r exists on this system,
@@ -334,9 +459,6 @@ Wgettimeofday(struct timeval *tv, struct timezone *tz)
* Interestingly, getenv *is* available in the Windows
* POSIX layer, just not setenv.
*
- * Note: Passing an empty string ("") for the value will remove
- * the variable from the environment (like unsetenv(3))
- *
* Return: Success: 0
* Failure: non-zero error code
*
@@ -348,14 +470,14 @@ Wgettimeofday(struct timeval *tv, struct timezone *tz)
int
Wsetenv(const char *name, const char *value, int overwrite)
{
+ size_t bufsize;
+ errno_t err;
+
/* If we're not overwriting, check if the environment variable exists.
* If it does (i.e.: the required buffer size to store the variable's
* value is non-zero), then return an error code.
*/
if (!overwrite) {
- size_t bufsize;
- errno_t err;
-
err = getenv_s(&bufsize, NULL, 0, name);
if (err || bufsize)
return (int)err;
@@ -845,7 +967,7 @@ H5_nanosleep(uint64_t nanosec)
#else
- const uint64_t nanosec_per_sec = 1000 * 1000L * 1000;
+ const uint64_t nanosec_per_sec = 1000 * 1000 * 1000;
struct timespec sleeptime; /* Struct to hold time to sleep */
/* Set up time to sleep
@@ -941,8 +1063,8 @@ const char *H5_optarg; /* Flag argument (or value) */
int
H5_get_option(int argc, const char *const *argv, const char *opts, const struct h5_long_options *l_opts)
{
- static int sp = 1; /* character index in current token */
- int optchar = '?'; /* option character passed back to user */
+ static int sp = 1; /* character index in current token */
+ int optopt = '?'; /* option character passed back to user */
if (sp == 1) {
/* check for more flag-like tokens */
@@ -973,7 +1095,7 @@ H5_get_option(int argc, const char *const *argv, const char *opts, const struct
for (i = 0; l_opts && l_opts[i].name; i++) {
if (HDstrcmp(arg, l_opts[i].name) == 0) {
/* we've found a matching long command line flag */
- optchar = l_opts[i].shortval;
+ optopt = l_opts[i].shortval;
if (l_opts[i].has_arg != no_arg) {
if (H5_optarg == NULL) {
@@ -986,7 +1108,7 @@ H5_get_option(int argc, const char *const *argv, const char *opts, const struct
if (H5_opterr)
HDfprintf(stderr, "%s: option required for \"--%s\" flag\n", argv[0], arg);
- optchar = '?';
+ optopt = '?';
}
}
}
@@ -995,7 +1117,7 @@ H5_get_option(int argc, const char *const *argv, const char *opts, const struct
if (H5_opterr)
HDfprintf(stderr, "%s: no option required for \"%s\" flag\n", argv[0], arg);
- optchar = '?';
+ optopt = '?';
}
}
break;
@@ -1007,7 +1129,7 @@ H5_get_option(int argc, const char *const *argv, const char *opts, const struct
if (H5_opterr)
HDfprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], arg);
- optchar = '?';
+ optopt = '?';
}
H5_optind++;
@@ -1016,14 +1138,14 @@ H5_get_option(int argc, const char *const *argv, const char *opts, const struct
HDfree(arg);
}
else {
- char *cp; /* pointer into current token */
+ register char *cp; /* pointer into current token */
/* short command line option */
- optchar = argv[H5_optind][sp];
+ optopt = argv[H5_optind][sp];
- if (optchar == ':' || (cp = HDstrchr(opts, optchar)) == 0) {
+ if (optopt == ':' || (cp = HDstrchr(opts, optopt)) == 0) {
if (H5_opterr)
- HDfprintf(stderr, "%s: unknown option \"%c\"\n", argv[0], optchar);
+ HDfprintf(stderr, "%s: unknown option \"%c\"\n", argv[0], optopt);
/* if no chars left in this token, move to next token */
if (argv[H5_optind][++sp] == '\0') {
@@ -1041,9 +1163,9 @@ H5_get_option(int argc, const char *const *argv, const char *opts, const struct
}
else if (++H5_optind >= argc) {
if (H5_opterr)
- HDfprintf(stderr, "%s: value expected for option \"%c\"\n", argv[0], optchar);
+ HDfprintf(stderr, "%s: value expected for option \"%c\"\n", argv[0], optopt);
- optchar = '?';
+ optopt = '?';
}
else {
/* flag value is next token */
@@ -1081,5 +1203,5 @@ H5_get_option(int argc, const char *const *argv, const char *opts, const struct
}
/* return the current flag character found */
- return optchar;
+ return optopt;
}