Line | Count | Source |
1 | | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | | * Copyright by The HDF Group. * |
3 | | * All rights reserved. * |
4 | | * * |
5 | | * This file is part of HDF5. The full HDF5 copyright notice, including * |
6 | | * terms governing use, modification, and redistribution, is contained in * |
7 | | * the LICENSE file, which can be found at the root of the source code * |
8 | | * distribution tree, or in https://www.hdfgroup.org/licenses. * |
9 | | * If you do not have access to either file, you may request a copy from * |
10 | | * help@hdfgroup.org. * |
11 | | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
12 | | |
13 | | /*------------------------------------------------------------------------- |
14 | | * |
15 | | * Created: H5system.c |
16 | | * |
17 | | * Purpose: System call wrapper implementations. |
18 | | * |
19 | | *------------------------------------------------------------------------- |
20 | | */ |
21 | | |
22 | | /****************/ |
23 | | /* Module Setup */ |
24 | | /****************/ |
25 | | #include "H5module.h" /* This source code file is part of the H5 module */ |
26 | | |
27 | | /***********/ |
28 | | /* Headers */ |
29 | | /***********/ |
30 | | #include "H5private.h" /* Generic Functions */ |
31 | | #include "H5Eprivate.h" /* Error handling */ |
32 | | #include "H5Fprivate.h" /* File access */ |
33 | | #include "H5MMprivate.h" /* Memory management */ |
34 | | |
35 | | /****************/ |
36 | | /* Local Macros */ |
37 | | /****************/ |
38 | | |
39 | | /******************/ |
40 | | /* Local Typedefs */ |
41 | | /******************/ |
42 | | |
43 | | /********************/ |
44 | | /* Package Typedefs */ |
45 | | /********************/ |
46 | | |
47 | | /********************/ |
48 | | /* Local Prototypes */ |
49 | | /********************/ |
50 | | |
51 | | /*********************/ |
52 | | /* Package Variables */ |
53 | | /*********************/ |
54 | | |
55 | | /*****************************/ |
56 | | /* Library Private Variables */ |
57 | | /*****************************/ |
58 | | |
59 | | /*******************/ |
60 | | /* Local Variables */ |
61 | | /*******************/ |
62 | | |
63 | | /* Track whether tzset routine was called */ |
64 | | static bool H5_ntzset = false; |
65 | | |
66 | | #ifndef H5_HAVE_VASPRINTF |
67 | | /* HDvasprintf provides vasprintf-like function on targets where it is |
68 | | * unavailable. |
69 | | */ |
70 | | int |
71 | | HDvasprintf(char **bufp, const char *fmt, va_list _ap) |
72 | | { |
73 | | char *buf; /* buffer to receive formatted string */ |
74 | | size_t bufsz; /* size of buffer to allocate */ |
75 | | |
76 | | for (bufsz = 32; (buf = malloc(bufsz)) != NULL;) { |
77 | | int ret; |
78 | | va_list ap; |
79 | | |
80 | | va_copy(ap, _ap); |
81 | | ret = vsnprintf(buf, bufsz, fmt, ap); |
82 | | va_end(ap); |
83 | | if (ret >= 0 && (size_t)ret < bufsz) { |
84 | | *bufp = buf; |
85 | | return ret; |
86 | | } |
87 | | free(buf); |
88 | | if (ret < 0) |
89 | | return ret; |
90 | | bufsz = (size_t)ret + 1; |
91 | | } |
92 | | return -1; |
93 | | } |
94 | | #endif /* H5_HAVE_VASPRINTF */ |
95 | | |
96 | | /*------------------------------------------------------------------------- |
97 | | * Function: Pflock |
98 | | * |
99 | | * Purpose: Wrapper function for POSIX systems where flock(2) is not |
100 | | * available. |
101 | | * |
102 | | * Return: Success: 0 |
103 | | * Failure: -1 |
104 | | * |
105 | | *------------------------------------------------------------------------- |
106 | | */ |
107 | | /* NOTE: Compile this all the time on POSIX systems, even when flock(2) is |
108 | | * present so that it's less likely to become dead code. |
109 | | */ |
110 | | #ifdef H5_HAVE_FCNTL |
111 | | int |
112 | | Pflock(int fd, int operation) |
113 | 0 | { |
114 | |
|
115 | 0 | struct flock flk; |
116 | | |
117 | | /* Set the lock type */ |
118 | 0 | if (operation & LOCK_UN) |
119 | 0 | flk.l_type = F_UNLCK; |
120 | 0 | else if (operation & LOCK_SH) |
121 | 0 | flk.l_type = F_RDLCK; |
122 | 0 | else |
123 | 0 | flk.l_type = F_WRLCK; |
124 | | |
125 | | /* Set the other flock struct values */ |
126 | 0 | flk.l_whence = SEEK_SET; |
127 | 0 | flk.l_start = 0; |
128 | 0 | flk.l_len = 0; /* to EOF */ |
129 | 0 | flk.l_pid = 0; /* not used with set */ |
130 | | |
131 | | /* Lock or unlock */ |
132 | 0 | if (fcntl(fd, F_SETLK, &flk) < 0) |
133 | 0 | return -1; |
134 | | |
135 | 0 | return 0; |
136 | |
|
137 | 0 | } /* end Pflock() */ |
138 | | #endif /* H5_HAVE_FCNTL */ |
139 | | |
140 | | /*------------------------------------------------------------------------- |
141 | | * Function: Nflock |
142 | | * |
143 | | * Purpose: Wrapper function for systems where no file locking is |
144 | | * available. |
145 | | * |
146 | | * Return: 0 (success) |
147 | | * |
148 | | *------------------------------------------------------------------------- |
149 | | */ |
150 | | int H5_ATTR_CONST |
151 | | Nflock(int H5_ATTR_UNUSED fd, int H5_ATTR_UNUSED operation) |
152 | 0 | { |
153 | | /* just succeed */ |
154 | 0 | return 0; |
155 | 0 | } /* end Nflock() */ |
156 | | |
157 | | /*------------------------------------------------------------------------- |
158 | | * Function: H5_make_time |
159 | | * |
160 | | * Purpose: Portability routine to abstract converting a 'tm' struct into |
161 | | * a time_t value. |
162 | | * |
163 | | * Note: This is a little problematic because mktime() operates on |
164 | | * local times. We convert to local time and then figure out the |
165 | | * adjustment based on the local time zone and daylight savings |
166 | | * setting. |
167 | | * |
168 | | * Return: Success: The value of timezone |
169 | | * Failure: -1 |
170 | | * |
171 | | *------------------------------------------------------------------------- |
172 | | */ |
173 | | time_t |
174 | | H5_make_time(struct tm *tm) |
175 | 0 | { |
176 | 0 | time_t the_time; /* The converted time */ |
177 | | #if defined(H5_HAVE_VISUAL_STUDIO) |
178 | | long timezone = 0; |
179 | | #endif |
180 | 0 | time_t ret_value = 0; /* Return value */ |
181 | |
|
182 | 0 | FUNC_ENTER_NOAPI_NOINIT |
183 | | |
184 | | /* Sanity check */ |
185 | 0 | assert(tm); |
186 | | |
187 | | /* Initialize timezone information */ |
188 | 0 | if (!H5_ntzset) { |
189 | 0 | tzset(); |
190 | 0 | H5_ntzset = true; |
191 | 0 | } |
192 | | |
193 | | /* Perform base conversion */ |
194 | 0 | if ((time_t)-1 == (the_time = mktime(tm))) |
195 | 0 | HGOTO_ERROR(H5E_INTERNAL, H5E_CANTCONVERT, FAIL, "badly formatted modification time message"); |
196 | | |
197 | | /* Adjust for timezones */ |
198 | 0 | #if defined(H5_HAVE_TM_GMTOFF) |
199 | | /* BSD-like systems */ |
200 | 0 | the_time += tm->tm_gmtoff; |
201 | | #elif defined(H5_HAVE_TIMEZONE) |
202 | | #if defined(H5_HAVE_VISUAL_STUDIO) |
203 | | _get_timezone(&timezone); |
204 | | #endif |
205 | | |
206 | | the_time -= timezone - (tm->tm_isdst ? 3600 : 0); |
207 | | #else |
208 | | /* |
209 | | * The catch-all. If we can't convert a character string universal |
210 | | * coordinated time to a time_t value reliably then we can't decode the |
211 | | * modification time message. This really isn't as bad as it sounds -- the |
212 | | * only way a user can get the modification time is from our internal |
213 | | * query routines, which can gracefully recover. |
214 | | */ |
215 | | HGOTO_ERROR(H5E_INTERNAL, H5E_UNSUPPORTED, FAIL, "unable to obtain local timezone information"); |
216 | | #endif |
217 | | |
218 | | /* Set return value */ |
219 | 0 | ret_value = the_time; |
220 | |
|
221 | 0 | done: |
222 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
223 | 0 | } /* end H5_make_time() */ |
224 | | |
225 | | /*------------------------------------------------------------------------- |
226 | | * Function: H5_get_localtime_str |
227 | | * |
228 | | * Purpose: Formats the current local time into a string buffer using |
229 | | * the thread-safe HDlocaltime_r wrapper. On failure, writes |
230 | | * "(unknown)" so callers always get a printable result. |
231 | | * |
232 | | * Return: void |
233 | | *------------------------------------------------------------------------- |
234 | | */ |
235 | | void |
236 | | H5_get_localtime_str(char *buf, size_t buf_size) |
237 | 0 | { |
238 | 0 | time_t now; |
239 | 0 | struct tm tm_buf; |
240 | |
|
241 | 0 | time(&now); |
242 | 0 | if (HDlocaltime_r(&now, &tm_buf) != NULL) |
243 | 0 | strftime(buf, buf_size, "%c", &tm_buf); /* %c is locale-dependent; fine for diagnostics */ |
244 | 0 | else |
245 | 0 | snprintf(buf, buf_size, "(unknown)"); |
246 | 0 | } /* end H5_get_localtime_str() */ |
247 | | |
248 | | #ifdef H5_HAVE_WIN32_API |
249 | | |
250 | | /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosecond units */ |
251 | | #define _W32_FT_OFFSET (116444736000000000ULL) |
252 | | |
253 | | /*------------------------------------------------------------------------- |
254 | | * Function: Wgettimeofday |
255 | | * |
256 | | * Purpose: Wrapper function for gettimeofday on Windows systems |
257 | | * |
258 | | * This function can get the time as well as a timezone |
259 | | * |
260 | | * Return: 0 |
261 | | * |
262 | | * This implementation is taken from the Cygwin source distribution at |
263 | | * src/winsup/mingw/mingwex/gettimeofday.c |
264 | | * |
265 | | * The original source code was contributed by |
266 | | * Danny Smith <dannysmith@users.sourceforge.net> |
267 | | * and released in the public domain. |
268 | | * |
269 | | *------------------------------------------------------------------------- |
270 | | */ |
271 | | int |
272 | | Wgettimeofday(struct timeval *tv, struct timezone *tz) |
273 | | { |
274 | | union { |
275 | | unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ |
276 | | FILETIME ft; |
277 | | } _now; |
278 | | |
279 | | static int tzsetflag; |
280 | | |
281 | | if (tv) { |
282 | | GetSystemTimeAsFileTime(&_now.ft); |
283 | | tv->tv_usec = (long)((_now.ns100 / 10ULL) % 1000000ULL); |
284 | | tv->tv_sec = (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); |
285 | | } |
286 | | |
287 | | if (tz) { |
288 | | if (!tzsetflag) { |
289 | | _tzset(); |
290 | | tzsetflag = 1; |
291 | | } |
292 | | tz->tz_minuteswest = _timezone / 60; |
293 | | tz->tz_dsttime = _daylight; |
294 | | } |
295 | | |
296 | | /* Always return 0 as per Open Group Base Specifications Issue 6. |
297 | | Do not set errno on error. */ |
298 | | return 0; |
299 | | } /* end Wgettimeofday() */ |
300 | | |
301 | | /*------------------------------------------------------------------------- |
302 | | * Function: Wsetenv |
303 | | * |
304 | | * Purpose: Wrapper function for setenv on Windows systems. |
305 | | * Interestingly, getenv *is* available in the Windows |
306 | | * POSIX layer, just not setenv. |
307 | | * |
308 | | * Note: Passing an empty string ("") for the value will remove |
309 | | * the variable from the environment (like unsetenv(3)) |
310 | | * |
311 | | * Return: Success: 0 |
312 | | * Failure: non-zero error code |
313 | | * |
314 | | *------------------------------------------------------------------------- |
315 | | */ |
316 | | int |
317 | | Wsetenv(const char *name, const char *value, int overwrite) |
318 | | { |
319 | | /* If we're not overwriting, check if the environment variable exists. |
320 | | * If it does (i.e.: the required buffer size to store the variable's |
321 | | * value is non-zero), then return an error code. |
322 | | */ |
323 | | if (!overwrite) { |
324 | | #ifndef H5_HAVE_MINGW |
325 | | size_t bufsize; |
326 | | errno_t err; |
327 | | |
328 | | err = getenv_s(&bufsize, NULL, 0, name); |
329 | | if (err || bufsize) |
330 | | return (int)err; |
331 | | #else |
332 | | /* MinGW doesn't have getenv_s() */ |
333 | | char *test = getenv(name); |
334 | | if (*test) |
335 | | return FAIL; |
336 | | #endif |
337 | | } |
338 | | |
339 | | return (int)_putenv_s(name, value); |
340 | | } /* end Wsetenv() */ |
341 | | |
342 | | #ifdef H5_HAVE_WIN32_API |
343 | | #pragma comment(lib, "advapi32.lib") |
344 | | #endif |
345 | | |
346 | | /*------------------------------------------------------------------------- |
347 | | * Function: H5_get_win32_times |
348 | | * |
349 | | * Purpose: Gets the elapsed, system and user times on Windows platforms. |
350 | | * All time values are in seconds. |
351 | | * |
352 | | * Return: Success: 0 |
353 | | * Failure: -1 |
354 | | * |
355 | | *------------------------------------------------------------------------- |
356 | | */ |
357 | | #ifdef H5_HAVE_WIN32_API |
358 | | int |
359 | | H5_get_win32_times(H5_timevals_t *tvs /*in,out*/) |
360 | | { |
361 | | static HANDLE process_handle; |
362 | | ULARGE_INTEGER kernel_start; |
363 | | ULARGE_INTEGER user_start; |
364 | | FILETIME KernelTime; |
365 | | FILETIME UserTime; |
366 | | FILETIME CreationTime; |
367 | | FILETIME ExitTime; |
368 | | LARGE_INTEGER counts_start; |
369 | | static LARGE_INTEGER counts_freq; |
370 | | static bool is_initialized = false; |
371 | | BOOL err; |
372 | | |
373 | | assert(tvs); |
374 | | |
375 | | if (!is_initialized) { |
376 | | /* NOTE: This is just a pseudo handle and does not need to be closed. */ |
377 | | process_handle = GetCurrentProcess(); |
378 | | err = QueryPerformanceFrequency(&counts_freq); |
379 | | if (0 == err) |
380 | | return -1; |
381 | | is_initialized = true; |
382 | | } /* end if */ |
383 | | |
384 | | /************************* |
385 | | * System and user times * |
386 | | *************************/ |
387 | | |
388 | | err = GetProcessTimes(process_handle, &CreationTime, &ExitTime, &KernelTime, &UserTime); |
389 | | if (0 == err) |
390 | | return -1; |
391 | | |
392 | | /* The 1.0E7 factor seems strange but it's due to the clock |
393 | | * ticking in 100 ns increments. |
394 | | */ |
395 | | kernel_start.HighPart = KernelTime.dwHighDateTime; |
396 | | kernel_start.LowPart = KernelTime.dwLowDateTime; |
397 | | tvs->system = (double)(kernel_start.QuadPart / 1.0E7F); |
398 | | |
399 | | user_start.HighPart = UserTime.dwHighDateTime; |
400 | | user_start.LowPart = UserTime.dwLowDateTime; |
401 | | tvs->user = (double)(user_start.QuadPart / 1.0E7F); |
402 | | |
403 | | /**************** |
404 | | * Elapsed time * |
405 | | ****************/ |
406 | | |
407 | | err = QueryPerformanceCounter(&counts_start); |
408 | | if (0 == err) |
409 | | return -1; |
410 | | |
411 | | tvs->elapsed = (double)(counts_start.QuadPart) / (double)counts_freq.QuadPart; |
412 | | |
413 | | return 0; |
414 | | } /* end H5_get_win32_times() */ |
415 | | #endif |
416 | | |
417 | | /*------------------------------------------------------------------------- |
418 | | * Function: Wflock |
419 | | * |
420 | | * Purpose: Wrapper function for flock on Windows systems |
421 | | * |
422 | | * Return: Success: 0 |
423 | | * Failure: -1 |
424 | | * |
425 | | *------------------------------------------------------------------------- |
426 | | */ |
427 | | int |
428 | | Wflock(int fd, int operation) |
429 | | { |
430 | | |
431 | | HANDLE hFile; |
432 | | DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY; |
433 | | DWORD dwReserved = 0; |
434 | | /* MAXDWORD locks the entire file */ |
435 | | DWORD nNumberOfBytesToLockLow = MAXDWORD; |
436 | | DWORD nNumberOfBytesToLockHigh = MAXDWORD; |
437 | | /* Must initialize OVERLAPPED struct */ |
438 | | OVERLAPPED overlapped = {0}; |
439 | | |
440 | | /* Get Windows HANDLE */ |
441 | | if (INVALID_HANDLE_VALUE == (hFile = (HANDLE)_get_osfhandle(fd))) |
442 | | return -1; |
443 | | |
444 | | /* Convert to Windows flags */ |
445 | | if (operation & LOCK_EX) |
446 | | dwFlags |= LOCKFILE_EXCLUSIVE_LOCK; |
447 | | |
448 | | /* Lock or unlock */ |
449 | | if (operation & LOCK_UN) { |
450 | | if (0 == |
451 | | UnlockFileEx(hFile, dwReserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh, &overlapped)) { |
452 | | /* Attempting to unlock an already unlocked file will fail and this can happen |
453 | | * in H5Fstart_swmr_write(). For now, just ignore the "error" (error code: 0x9e / 158). |
454 | | */ |
455 | | if (GetLastError() != 158) |
456 | | return -1; |
457 | | } |
458 | | } |
459 | | else { |
460 | | if (0 == LockFileEx(hFile, dwFlags, dwReserved, nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh, |
461 | | &overlapped)) |
462 | | return -1; |
463 | | } |
464 | | |
465 | | return 0; |
466 | | } /* end Wflock() */ |
467 | | |
468 | | /*------------------------------------------------------------------------- |
469 | | * Function: H5_get_utf16_str |
470 | | * |
471 | | * Purpose: Gets a UTF-16 string from an UTF-8 (or ASCII) string. |
472 | | * |
473 | | * On success, a pointer to the new UTF-16 string is returned |
474 | | * in `wstring`. This must be freed by the caller using |
475 | | * H5MM_xfree(). |
476 | | * |
477 | | * Return: Non-negative on success/Negative on failure |
478 | | * |
479 | | * On failure, the result of GetLastError() is returned |
480 | | * through the `win_error` parameter, if non-NULL. |
481 | | * |
482 | | *------------------------------------------------------------------------- |
483 | | */ |
484 | | herr_t |
485 | | H5_get_utf16_str(const char *s, wchar_t **wstring, uint32_t *win_error) |
486 | | { |
487 | | int nwchars = -1; /* Length of the UTF-16 buffer */ |
488 | | wchar_t *ret_s = NULL; /* UTF-16 version of the string */ |
489 | | |
490 | | /* Get the number of UTF-16 characters needed */ |
491 | | if (0 == (nwchars = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s, -1, NULL, 0))) |
492 | | goto error; |
493 | | |
494 | | /* Allocate a buffer for the UTF-16 string */ |
495 | | if (NULL == (ret_s = H5MM_calloc(sizeof(wchar_t) * (size_t)nwchars))) |
496 | | goto error; |
497 | | |
498 | | /* Convert the input UTF-8 string to UTF-16 */ |
499 | | if (0 == MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s, -1, ret_s, nwchars)) |
500 | | goto error; |
501 | | |
502 | | *wstring = ret_s; |
503 | | |
504 | | return SUCCEED; |
505 | | |
506 | | error: |
507 | | /* Store error value first before doing anything else */ |
508 | | if (win_error) |
509 | | *win_error = (uint32_t)GetLastError(); |
510 | | |
511 | | if (ret_s) |
512 | | H5MM_xfree((void *)ret_s); |
513 | | |
514 | | *wstring = NULL; |
515 | | |
516 | | return FAIL; |
517 | | } /* end H5_get_utf16_str() */ |
518 | | |
519 | | /*------------------------------------------------------------------------- |
520 | | * Function: Wopen |
521 | | * |
522 | | * Purpose: Equivalent of open(2) for use on Windows. Necessary to |
523 | | * handle Unicode and code pages on that platform. |
524 | | * |
525 | | * Return: Success: A POSIX file descriptor |
526 | | * Failure: -1 |
527 | | *------------------------------------------------------------------------- |
528 | | */ |
529 | | int |
530 | | Wopen(const char *path, int oflag, ...) |
531 | | { |
532 | | uint32_t win_error = 0; /* Windows error code for failures */ |
533 | | wchar_t *wpath = NULL; /* UTF-16 version of the path */ |
534 | | herr_t h5_ret = FAIL; /* HDF5 return code */ |
535 | | char *env = NULL; /* Environment variable string */ |
536 | | bool prefer_code_page = false; /* Whether to prefer using the Windows code page */ |
537 | | int fd = -1; /* POSIX file descriptor to be returned */ |
538 | | int pmode = 0; /* mode (optionally set via variable args) */ |
539 | | |
540 | | /* _O_BINARY must be set in Windows to avoid CR-LF <-> LF EOL |
541 | | * transformations when performing I/O. Note that this will |
542 | | * produce Unix-style text files, though. |
543 | | */ |
544 | | oflag |= _O_BINARY; |
545 | | |
546 | | /* Get the mode, if O_CREAT was specified */ |
547 | | if (oflag & O_CREAT) { |
548 | | va_list vl; |
549 | | |
550 | | va_start(vl, oflag); |
551 | | pmode = va_arg(vl, int); |
552 | | va_end(vl); |
553 | | } |
554 | | |
555 | | /* |
556 | | * Check HDF5_PREFER_WINDOWS_CODE_PAGE environment variable to |
557 | | * determine how to handle the pathname. |
558 | | */ |
559 | | env = getenv(HDF5_PREFER_WINDOWS_CODE_PAGE); |
560 | | if (env && (*env != '\0')) { |
561 | | if (0 == HDstrcasecmp(env, "true") || 0 == strcmp(env, "1")) |
562 | | prefer_code_page = true; |
563 | | } |
564 | | |
565 | | /* |
566 | | * Unless requested to prefer Windows code pages, try to convert |
567 | | * the pathname from UTF-8 to UTF-16. If this fails, fallback to |
568 | | * the normal POSIX open() call. |
569 | | */ |
570 | | if (!prefer_code_page) { |
571 | | h5_ret = H5_get_utf16_str(path, &wpath, &win_error); |
572 | | if (h5_ret >= 0) { |
573 | | /* Open the file using a UTF-16 path */ |
574 | | fd = _wopen(wpath, oflag, pmode); |
575 | | } |
576 | | else { |
577 | | if (ERROR_NO_UNICODE_TRANSLATION != win_error) |
578 | | goto done; |
579 | | |
580 | | fd = open(path, oflag, pmode); |
581 | | } |
582 | | } |
583 | | else |
584 | | fd = open(path, oflag, pmode); |
585 | | |
586 | | done: |
587 | | H5MM_xfree(wpath); |
588 | | |
589 | | return fd; |
590 | | } /* end Wopen() */ |
591 | | |
592 | | /*------------------------------------------------------------------------- |
593 | | * Function: Wremove |
594 | | * |
595 | | * Purpose: Equivalent of remove(3) for use on Windows. Necessary to |
596 | | * handle Unicode and code pages on that platform. |
597 | | * |
598 | | * Return: Success: 0 |
599 | | * Failure: -1 |
600 | | *------------------------------------------------------------------------- |
601 | | */ |
602 | | int |
603 | | Wremove(const char *path) |
604 | | { |
605 | | uint32_t win_error = 0; /* Windows error code for failures */ |
606 | | wchar_t *wpath = NULL; /* UTF-16 version of the path */ |
607 | | herr_t h5_ret = FAIL; /* HDF5 return code */ |
608 | | char *env = NULL; /* Environment variable string */ |
609 | | bool prefer_code_page = false; /* Whether to prefer using the Windows code page */ |
610 | | int ret = -1; |
611 | | |
612 | | /* |
613 | | * Check HDF5_PREFER_WINDOWS_CODE_PAGE environment variable to |
614 | | * determine how to handle the pathname. |
615 | | */ |
616 | | env = getenv(HDF5_PREFER_WINDOWS_CODE_PAGE); |
617 | | if (env && (*env != '\0')) { |
618 | | if (0 == HDstrcasecmp(env, "true") || 0 == strcmp(env, "1")) |
619 | | prefer_code_page = true; |
620 | | } |
621 | | |
622 | | /* |
623 | | * Unless requested to prefer Windows code pages, try to convert |
624 | | * the pathname from UTF-8 to UTF-16. If this fails, fallback to |
625 | | * the normal POSIX remove() call. |
626 | | */ |
627 | | if (!prefer_code_page) { |
628 | | h5_ret = H5_get_utf16_str(path, &wpath, &win_error); |
629 | | if (h5_ret >= 0) { |
630 | | /* Remove the file using a UTF-16 path */ |
631 | | ret = _wremove(wpath); |
632 | | } |
633 | | else { |
634 | | if (ERROR_NO_UNICODE_TRANSLATION != win_error) |
635 | | goto done; |
636 | | |
637 | | ret = remove(path); |
638 | | } |
639 | | } |
640 | | else |
641 | | ret = remove(path); |
642 | | |
643 | | done: |
644 | | H5MM_xfree(wpath); |
645 | | |
646 | | return ret; |
647 | | } /* end Wremove() */ |
648 | | |
649 | | #endif /* H5_HAVE_WIN32_API */ |
650 | | |
651 | | /*------------------------------------------------------------------------- |
652 | | * Function: H5_build_extpath |
653 | | * |
654 | | * Purpose: To build the path for later searching of target file for external |
655 | | * links and external files. This path can be either: |
656 | | * 1. The absolute path of NAME |
657 | | * or |
658 | | * 2. The current working directory + relative path of NAME |
659 | | * |
660 | | * Return: SUCCEED/FAIL |
661 | | * |
662 | | *------------------------------------------------------------------------- |
663 | | */ |
664 | 0 | #define MAX_PATH_LEN 1024 |
665 | | |
666 | | herr_t |
667 | | H5_build_extpath(const char *name, char **extpath /*out*/) |
668 | 302 | { |
669 | 302 | char *full_path = NULL; /* Pointer to the full path, as built or passed in */ |
670 | 302 | char *cwdpath = NULL; /* Pointer to the current working directory path */ |
671 | 302 | char *new_name = NULL; /* Pointer to the name of the file */ |
672 | 302 | herr_t ret_value = SUCCEED; /* Return value */ |
673 | | |
674 | 302 | FUNC_ENTER_NOAPI_NOINIT |
675 | | |
676 | 302 | assert(name); |
677 | 302 | assert(extpath); |
678 | | |
679 | | /* Clear external path pointer to begin with */ |
680 | 302 | *extpath = NULL; |
681 | | |
682 | | /* |
683 | | * Unix: name[0] is a "/" |
684 | | * Windows: name[0-2] is "<drive letter>:\" or "<drive-letter>:/" |
685 | | */ |
686 | 302 | if (H5_CHECK_ABSOLUTE(name)) { |
687 | 302 | if (NULL == (full_path = (char *)H5MM_strdup(name))) |
688 | 0 | HGOTO_ERROR(H5E_INTERNAL, H5E_NOSPACE, FAIL, "memory allocation failed"); |
689 | 302 | } |
690 | 0 | else { |
691 | | /* relative pathname */ |
692 | 0 | char *retcwd; |
693 | 0 | size_t name_len; |
694 | 0 | int drive; |
695 | |
|
696 | 0 | if (NULL == (cwdpath = (char *)H5MM_malloc(MAX_PATH_LEN))) |
697 | 0 | HGOTO_ERROR(H5E_INTERNAL, H5E_NOSPACE, FAIL, "memory allocation failed"); |
698 | 0 | name_len = strlen(name) + 1; |
699 | 0 | if (NULL == (new_name = (char *)H5MM_malloc(name_len))) |
700 | 0 | HGOTO_ERROR(H5E_INTERNAL, H5E_NOSPACE, FAIL, "memory allocation failed"); |
701 | | |
702 | | /* |
703 | | * Windows: name[0-1] is "<drive-letter>:" |
704 | | * Get current working directory on the drive specified in NAME |
705 | | * Unix: does not apply |
706 | | */ |
707 | 0 | if (H5_CHECK_ABS_DRIVE(name)) { |
708 | 0 | drive = toupper(name[0]) - 'A' + 1; |
709 | 0 | retcwd = HDgetdcwd(drive, cwdpath, MAX_PATH_LEN); |
710 | 0 | strncpy(new_name, &name[2], name_len); |
711 | 0 | } |
712 | | /* |
713 | | * Windows: name[0] is a '/' or '\' |
714 | | * Get current drive |
715 | | * Unix: does not apply |
716 | | */ |
717 | 0 | else if (H5_CHECK_ABS_PATH(name) && (0 != (drive = HDgetdrive()))) { |
718 | 0 | snprintf(cwdpath, MAX_PATH_LEN, "%c:%c", (drive + 'A' - 1), name[0]); |
719 | 0 | retcwd = cwdpath; |
720 | 0 | strncpy(new_name, &name[1], name_len); |
721 | 0 | } |
722 | | /* totally relative for Unix and Windows: get current working directory */ |
723 | 0 | else { |
724 | 0 | retcwd = HDgetcwd(cwdpath, MAX_PATH_LEN); |
725 | 0 | strncpy(new_name, name, name_len); |
726 | 0 | } |
727 | |
|
728 | 0 | if (retcwd != NULL) { |
729 | 0 | size_t cwdlen; |
730 | 0 | size_t path_len; |
731 | |
|
732 | 0 | cwdlen = strlen(cwdpath); |
733 | 0 | if (cwdlen == 0) |
734 | 0 | HGOTO_ERROR(H5E_INTERNAL, H5E_BADVALUE, FAIL, "cwd length is zero"); |
735 | 0 | path_len = cwdlen + strlen(new_name) + 2; |
736 | 0 | if (NULL == (full_path = (char *)H5MM_malloc(path_len))) |
737 | 0 | HGOTO_ERROR(H5E_INTERNAL, H5E_NOSPACE, FAIL, "memory allocation failed"); |
738 | | |
739 | | /* path_len will always be greater than zero, so no check before |
740 | | * setting the terminal NUL byte of full_path |
741 | | */ |
742 | 0 | strncpy(full_path, cwdpath, path_len); |
743 | 0 | full_path[path_len - 1] = '\0'; |
744 | |
|
745 | 0 | if (!H5_CHECK_DELIMITER(cwdpath[cwdlen - 1])) |
746 | 0 | strncat(full_path, H5_DIR_SEPS, path_len - (cwdlen + 1)); |
747 | 0 | strncat(full_path, new_name, path_len - (cwdlen + 1) - strlen(H5_DIR_SEPS)); |
748 | 0 | } |
749 | 0 | } |
750 | | |
751 | | /* Strip out the last component (the file name itself) from the path */ |
752 | 302 | if (full_path) { |
753 | 302 | char *ptr = NULL; |
754 | | |
755 | 302 | H5_GET_LAST_DELIMITER(full_path, ptr) |
756 | 302 | assert(ptr); |
757 | 302 | *++ptr = '\0'; |
758 | 302 | *extpath = full_path; |
759 | 302 | } |
760 | | |
761 | 302 | done: |
762 | 302 | if (cwdpath) |
763 | 0 | H5MM_xfree(cwdpath); |
764 | 302 | if (new_name) |
765 | 0 | H5MM_xfree(new_name); |
766 | | |
767 | 302 | FUNC_LEAVE_NOAPI(ret_value) |
768 | 302 | } /* end H5_build_extpath() */ |
769 | | |
770 | | /*-------------------------------------------------------------------------- |
771 | | * Function: H5_combine_path |
772 | | * |
773 | | * Purpose: If path2 is relative, interpret path2 as relative to path1 |
774 | | * and store the result in full_name. Otherwise store path2 |
775 | | * in full_name. |
776 | | * |
777 | | * Return: SUCCEED/FAIL |
778 | | * |
779 | | *-------------------------------------------------------------------------- |
780 | | */ |
781 | | herr_t |
782 | | H5_combine_path(const char *path1, const char *path2, char **full_name /*out*/) |
783 | 0 | { |
784 | 0 | size_t path1_len = 0; /* length of path1 */ |
785 | 0 | size_t path2_len; /* length of path2 */ |
786 | 0 | herr_t ret_value = SUCCEED; /* Return value */ |
787 | |
|
788 | 0 | FUNC_ENTER_NOAPI_NOINIT |
789 | |
|
790 | 0 | assert(path2); |
791 | |
|
792 | 0 | if (path1) |
793 | 0 | path1_len = strlen(path1); |
794 | 0 | path2_len = strlen(path2); |
795 | |
|
796 | 0 | if (path1 == NULL || *path1 == '\0' || H5_CHECK_ABSOLUTE(path2)) { |
797 | | |
798 | | /* If path1 is empty or path2 is absolute, simply use path2 */ |
799 | 0 | if (NULL == (*full_name = (char *)H5MM_strdup(path2))) |
800 | 0 | HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed"); |
801 | |
|
802 | 0 | } /* end if */ |
803 | 0 | else if (H5_CHECK_ABS_PATH(path2)) { |
804 | | |
805 | | /* On windows path2 is a path absolute name */ |
806 | 0 | if (H5_CHECK_ABSOLUTE(path1) || H5_CHECK_ABS_DRIVE(path1)) { |
807 | | /* path1 is absolute or drive absolute and path2 is path absolute. |
808 | | * Use the drive letter of path1 + path2 |
809 | | */ |
810 | 0 | if (NULL == (*full_name = (char *)H5MM_malloc(path2_len + 3))) |
811 | 0 | HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "unable to allocate path2 buffer"); |
812 | 0 | snprintf(*full_name, (path2_len + 3), "%c:%s", path1[0], path2); |
813 | 0 | } /* end if */ |
814 | 0 | else { |
815 | | /* On windows path2 is path absolute name ("\foo\bar"), |
816 | | * path1 does not have a drive letter (i.e. is "a\b" or "\a\b"). |
817 | | * Use path2. |
818 | | */ |
819 | 0 | if (NULL == (*full_name = (char *)H5MM_strdup(path2))) |
820 | 0 | HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed"); |
821 | 0 | } /* end else */ |
822 | |
|
823 | 0 | } /* end else if */ |
824 | 0 | else { |
825 | | |
826 | | /* Relative path2: |
827 | | * Allocate a buffer to hold path1 + path2 + possibly the delimiter |
828 | | * + terminating null byte |
829 | | */ |
830 | 0 | if (NULL == |
831 | 0 | (*full_name = (char *)H5MM_malloc(path1_len + path2_len + 2 + |
832 | 0 | 2))) /* Extra "+2" to quiet GCC warning - 2019/07/05, QAK */ |
833 | 0 | HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "unable to allocate filename buffer"); |
834 | | |
835 | | /* Compose the full file name */ |
836 | 0 | snprintf(*full_name, (path1_len + path2_len + 2 + 2), "%s%s%s", |
837 | 0 | path1, /* Extra "+2" to quiet GCC warning - 2019/07/05, QAK */ |
838 | 0 | (H5_CHECK_DELIMITER(path1[path1_len - 1]) ? "" : H5_DIR_SEPS), path2); |
839 | 0 | } /* end else */ |
840 | | |
841 | 0 | done: |
842 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
843 | 0 | } /* end H5_combine_path() */ |
844 | | |
845 | | /*-------------------------------------------------------------------------- |
846 | | * Function: H5_nanosleep |
847 | | * |
848 | | * Purpose: Sleep for a given # of nanoseconds |
849 | | * |
850 | | * Note that commodity hardware is probably going to have a |
851 | | * resolution of milliseconds, not nanoseconds. |
852 | | * |
853 | | * Return: void |
854 | | *-------------------------------------------------------------------------- |
855 | | */ |
856 | | void |
857 | | H5_nanosleep(uint64_t nanosec) |
858 | 0 | { |
859 | 0 | FUNC_ENTER_NOAPI_NOINIT_NOERR |
860 | |
|
861 | | #ifdef H5_HAVE_WIN32_API |
862 | | DWORD dwMilliseconds = (DWORD)ceil(nanosec / 1.0e6); |
863 | | |
864 | | /* Windows can't sleep at a ns resolution. Best we can do is ~1 ms. We |
865 | | * don't care about the return value since the second parameter |
866 | | * (bAlertable) is false, so it will always be zero. |
867 | | */ |
868 | | SleepEx(dwMilliseconds, false); |
869 | | |
870 | | #else |
871 | |
|
872 | 0 | const uint64_t nanosec_per_sec = 1000 * 1000L * 1000; |
873 | 0 | struct timespec sleeptime; /* Struct to hold time to sleep */ |
874 | | |
875 | | /* Set up time to sleep |
876 | | * |
877 | | * Assuming ILP32 or LP64 or wider architecture, (long)operand |
878 | | * satisfies 0 <= operand < nanosec_per_sec < LONG_MAX. |
879 | | * |
880 | | * It's harder to be sure that we don't overflow time_t. |
881 | | */ |
882 | 0 | sleeptime.tv_sec = (time_t)(nanosec / nanosec_per_sec); |
883 | 0 | sleeptime.tv_nsec = (long)(nanosec % nanosec_per_sec); |
884 | | |
885 | | /* Sleep for up to `sleeptime` and, in the event of an interruption, |
886 | | * save the unslept time back to `sleeptime`. |
887 | | */ |
888 | 0 | while (HDnanosleep(&sleeptime, &sleeptime) == -1) { |
889 | | /* If we were just interrupted, sleep for the remaining time. |
890 | | * Otherwise, the error was essentially impossible, so just stop |
891 | | * sleeping. |
892 | | */ |
893 | 0 | if (errno != EINTR) |
894 | 0 | break; |
895 | 0 | } |
896 | 0 | #endif |
897 | |
|
898 | 0 | FUNC_LEAVE_NOAPI_VOID |
899 | 0 | } /* end H5_nanosleep() */ |
900 | | |
901 | | #ifdef H5_HAVE_WIN32_API |
902 | | |
903 | | #define H5_WIN32_ENV_VAR_BUFFER_SIZE 32767 |
904 | | |
905 | | /*------------------------------------------------------------------------- |
906 | | * Function: H5_expand_windows_env_vars() |
907 | | * |
908 | | * Purpose: Replaces windows environment variables of the form %foo% |
909 | | * with user-specific values. |
910 | | * |
911 | | * Return: SUCCEED/FAIL |
912 | | * |
913 | | *------------------------------------------------------------------------- |
914 | | */ |
915 | | herr_t |
916 | | H5_expand_windows_env_vars(char **env_var) |
917 | | { |
918 | | long n_chars = 0; |
919 | | char *temp_buf = NULL; |
920 | | herr_t ret_value = SUCCEED; /* Return value */ |
921 | | |
922 | | FUNC_ENTER_NOAPI_NOINIT |
923 | | |
924 | | /* Allocate buffer for expanded environment variable string */ |
925 | | if (NULL == (temp_buf = (char *)H5MM_calloc((size_t)H5_WIN32_ENV_VAR_BUFFER_SIZE))) |
926 | | HGOTO_ERROR(H5E_PLUGIN, H5E_CANTALLOC, FAIL, "can't allocate memory for expanded path"); |
927 | | |
928 | | /* Expand the environment variable string */ |
929 | | if ((n_chars = ExpandEnvironmentStringsA(*env_var, temp_buf, H5_WIN32_ENV_VAR_BUFFER_SIZE)) > |
930 | | H5_WIN32_ENV_VAR_BUFFER_SIZE) |
931 | | HGOTO_ERROR(H5E_PLUGIN, H5E_NOSPACE, FAIL, "expanded path is too long"); |
932 | | |
933 | | if (0 == n_chars) |
934 | | HGOTO_ERROR(H5E_PLUGIN, H5E_CANTGET, FAIL, "failed to expand path"); |
935 | | |
936 | | *env_var = (char *)H5MM_xfree(*env_var); |
937 | | *env_var = temp_buf; |
938 | | |
939 | | done: |
940 | | if (FAIL == ret_value && temp_buf) |
941 | | temp_buf = (char *)H5MM_xfree(temp_buf); |
942 | | |
943 | | FUNC_LEAVE_NOAPI(ret_value) |
944 | | } /* end H5_expand_windows_env_vars() */ |
945 | | |
946 | | /*------------------------------------------------------------------------- |
947 | | * Function: H5_strndup |
948 | | * |
949 | | * Purpose: Similar to strndup() for use on Windows. Allocates a new |
950 | | * string and copies at most `n` bytes from the original |
951 | | * string into the new string. If the original string is |
952 | | * longer than `n`, only `n` bytes are copied from the |
953 | | * original string. In either case, the string being returned |
954 | | * is guaranteed to be terminated with a null byte. |
955 | | * |
956 | | * The returned pointer is allocated by H5MM_malloc in this |
957 | | * routine and must be freed by the caller with H5MM_free or |
958 | | * H5MM_xfree. |
959 | | * |
960 | | * Return: Pointer to copied string on success |
961 | | * NULL on failure |
962 | | * |
963 | | *------------------------------------------------------------------------- |
964 | | */ |
965 | | char * |
966 | | H5_strndup(const char *s, size_t n) |
967 | | { |
968 | | size_t len; |
969 | | char *ret_value = NULL; |
970 | | |
971 | | FUNC_ENTER_NOAPI_NOINIT |
972 | | |
973 | | if (!s) |
974 | | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "string cannot be NULL"); |
975 | | |
976 | | for (len = 0; len < n && s[len] != '\0'; len++) |
977 | | ; |
978 | | |
979 | | if (NULL == (ret_value = H5MM_malloc(len + 1))) |
980 | | HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, NULL, "can't allocate buffer for string"); |
981 | | |
982 | | H5MM_memcpy(ret_value, s, len); |
983 | | ret_value[len] = '\0'; |
984 | | |
985 | | done: |
986 | | FUNC_LEAVE_NOAPI(ret_value) |
987 | | } |
988 | | |
989 | | /*------------------------------------------------------------------------- |
990 | | * Function: Wstrcasestr_wrap |
991 | | * |
992 | | * Purpose: Windows wrapper function for strcasestr to retain GNU |
993 | | * behavior where searching for an empty substring returns the |
994 | | * input string being searched. StrStrIA on Windows does not |
995 | | * exhibit this same behavior. |
996 | | * |
997 | | * Return: Pointer to input string if 'needle' is the empty substring |
998 | | * Otherwise, returns StrStrIA(haystack, needle) |
999 | | * |
1000 | | *------------------------------------------------------------------------- |
1001 | | */ |
1002 | | char * |
1003 | | Wstrcasestr_wrap(const char *haystack, const char *needle) |
1004 | | { |
1005 | | if (needle && !*needle) |
1006 | | return (char *)haystack; |
1007 | | else |
1008 | | return StrStrIA(haystack, needle); |
1009 | | } |
1010 | | #endif /* H5_HAVE_WIN32_API */ |
1011 | | |
1012 | | /* dirname() and basename() are not easily ported to Windows and basename |
1013 | | * behavior varies depending on if you get POSIX vs. GNU. As a more |
1014 | | * platform-indpendent work-around, we've implemented H5_ versions of |
1015 | | * dirname() and basename(). |
1016 | | * |
1017 | | * - The input string is never modified. |
1018 | | * |
1019 | | * - The out parameter is a new string that was allocated with H5MM routines |
1020 | | * and must be freed by the caller via H5MM_free()/H5MM_xfree(). |
1021 | | * |
1022 | | * - NULL pointers are errors. |
1023 | | * |
1024 | | * - On errors, FAIL will be returned and the output parameter will be |
1025 | | * undefined. |
1026 | | * |
1027 | | * - Assumes the file separator is \ on Win32 and / everywhere else, |
1028 | | * including Cygwin. |
1029 | | */ |
1030 | | |
1031 | | /*------------------------------------------------------------------------- |
1032 | | * Function: H5_dirname |
1033 | | * |
1034 | | * Purpose: Similar to dirname(3) but more portable across platforms. |
1035 | | * Returns a pointer to the directory component of a specified |
1036 | | * pathname. The returned pointer is allocated by this routine |
1037 | | * and must be freed by the caller with H5MM_free or |
1038 | | * H5MM_xfree. |
1039 | | * |
1040 | | * Return: Non-negative on success/Negative on failure |
1041 | | * |
1042 | | *------------------------------------------------------------------------- |
1043 | | */ |
1044 | | herr_t |
1045 | | H5_dirname(const char *path, char **dirname) |
1046 | 0 | { |
1047 | 0 | char *sep; |
1048 | 0 | char *out = NULL; |
1049 | 0 | herr_t ret_value = SUCCEED; |
1050 | |
|
1051 | 0 | FUNC_ENTER_NOAPI_NOINIT |
1052 | |
|
1053 | 0 | if (!path) |
1054 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "path can't be NULL"); |
1055 | 0 | if (!dirname) |
1056 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "dirname can't be NULL"); |
1057 | | |
1058 | 0 | if (NULL == (sep = strrchr(path, H5_DIR_SEPC))) { |
1059 | | /* Pathname with no file separator characters */ |
1060 | 0 | out = H5MM_strdup("."); |
1061 | 0 | } |
1062 | 0 | else if (sep == path) { |
1063 | | /* Pathname of form "/" or "/filename" */ |
1064 | 0 | out = H5MM_strdup(H5_DIR_SEPS); |
1065 | 0 | } |
1066 | 0 | else { |
1067 | 0 | if (sep[1] == '\0') { |
1068 | | /* |
1069 | | * Last file separator character is last character in |
1070 | | * pathname. Skip this and any other preceding trailing |
1071 | | * file separator characters |
1072 | | */ |
1073 | 0 | while (sep != path && sep[-1] == H5_DIR_SEPC) |
1074 | 0 | sep--; |
1075 | |
|
1076 | 0 | if (sep == path) { |
1077 | | /* Contrived case: "//", "///" and similar */ |
1078 | 0 | out = H5MM_strdup(H5_DIR_SEPS); |
1079 | 0 | sep = NULL; |
1080 | 0 | } |
1081 | 0 | else { |
1082 | | /* |
1083 | | * Must have found the filename component. Search |
1084 | | * backwards to a previous file separator character, |
1085 | | * if any. |
1086 | | */ |
1087 | 0 | while (sep != path && sep[-1] != H5_DIR_SEPC) |
1088 | 0 | sep--; |
1089 | |
|
1090 | 0 | if (sep == path) { |
1091 | | /* No directory component found, just return "." */ |
1092 | 0 | out = H5MM_strdup("."); |
1093 | 0 | sep = NULL; |
1094 | 0 | } |
1095 | 0 | } |
1096 | 0 | } |
1097 | |
|
1098 | 0 | if (sep) { |
1099 | 0 | ptrdiff_t len; |
1100 | | |
1101 | | /* Skip a possible run of duplicate file separator characters */ |
1102 | 0 | while (sep != path && sep[-1] == H5_DIR_SEPC) |
1103 | 0 | sep--; |
1104 | |
|
1105 | 0 | if (sep == path) |
1106 | | /* Pathname of form "/usr/" */ |
1107 | 0 | out = H5MM_strdup(H5_DIR_SEPS); |
1108 | 0 | else { |
1109 | | /* Pathname of form "dir/filename" */ |
1110 | 0 | len = sep - path; |
1111 | 0 | assert(len >= 0); |
1112 | |
|
1113 | 0 | out = H5MM_strndup(path, (size_t)len); |
1114 | 0 | } |
1115 | 0 | } |
1116 | 0 | } |
1117 | |
|
1118 | 0 | if (NULL == out) |
1119 | 0 | HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "can't allocate buffer for dirname"); |
1120 | | |
1121 | 0 | *dirname = out; |
1122 | |
|
1123 | 0 | done: |
1124 | 0 | if (FAIL == ret_value) { |
1125 | 0 | H5MM_free(out); |
1126 | 0 | if (dirname) |
1127 | 0 | *dirname = NULL; |
1128 | 0 | } |
1129 | |
|
1130 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1131 | 0 | } /* end H5_dirname() */ |
1132 | | |
1133 | | /*------------------------------------------------------------------------- |
1134 | | * Function: H5_basename |
1135 | | * |
1136 | | * Purpose: Similar to basename(3) but more portable across platforms. |
1137 | | * Returns a pointer to the filename component of a specified |
1138 | | * pathname. The returned pointer is allocated by this routine |
1139 | | * and must be freed by the caller with H5MM_free or |
1140 | | * H5MM_xfree. |
1141 | | * |
1142 | | * NOTE: This routine follows the POSIX semantics for |
1143 | | * basename(3). That is, passing the path string "/" ("\" on |
1144 | | * Windows) returns the string "/" (again, "\" on Windows) and |
1145 | | * passing a path string with trailing file separator |
1146 | | * characters returns the filename component with the trailing |
1147 | | * file separator characters being ignored. |
1148 | | * |
1149 | | * Return: Non-negative on success/Negative on failure |
1150 | | * |
1151 | | *------------------------------------------------------------------------- |
1152 | | */ |
1153 | | herr_t |
1154 | | H5_basename(const char *path, char **basename) |
1155 | 0 | { |
1156 | 0 | const char *sep; |
1157 | 0 | char *out = NULL; |
1158 | 0 | herr_t ret_value = SUCCEED; |
1159 | |
|
1160 | 0 | FUNC_ENTER_NOAPI_NOINIT |
1161 | |
|
1162 | 0 | if (!path) |
1163 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "path can't be NULL"); |
1164 | 0 | if (!basename) |
1165 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "basename can't be NULL"); |
1166 | | |
1167 | 0 | if (NULL == (sep = strrchr(path, H5_DIR_SEPC))) { |
1168 | 0 | if (*path == '\0') |
1169 | | /* Empty pathname */ |
1170 | 0 | out = H5MM_strdup("."); |
1171 | 0 | else |
1172 | | /* Pathname with no file separator characters */ |
1173 | 0 | out = H5MM_strdup(path); |
1174 | 0 | } |
1175 | 0 | else if (sep == path) { |
1176 | 0 | if (sep[1] == '\0') |
1177 | | /* Pathname of form "/" */ |
1178 | 0 | out = H5MM_strdup(H5_DIR_SEPS); |
1179 | 0 | else |
1180 | | /* Pathname of form "/filename" */ |
1181 | 0 | out = H5MM_strdup(sep + 1); |
1182 | 0 | } |
1183 | 0 | else { |
1184 | 0 | if (sep[1] != '\0') |
1185 | | /* Pathname of form "dir/filename" */ |
1186 | 0 | out = H5MM_strdup(sep + 1); |
1187 | 0 | else { |
1188 | | /* Pathname of form "filename/", "/dir/filename/", etc. */ |
1189 | | |
1190 | | /* |
1191 | | * Last file separator character is last character in |
1192 | | * pathname. Skip this and any other preceding trailing |
1193 | | * file separator characters |
1194 | | */ |
1195 | 0 | while (sep != path && sep[-1] == H5_DIR_SEPC) |
1196 | 0 | sep--; |
1197 | |
|
1198 | 0 | if (sep == path) |
1199 | | /* Contrived case: "//", "///" and similar */ |
1200 | 0 | out = H5MM_strdup(H5_DIR_SEPS); |
1201 | 0 | else { |
1202 | 0 | const char *c_ptr = sep; |
1203 | 0 | ptrdiff_t len; |
1204 | | |
1205 | | /* |
1206 | | * Skip back to a previous file separator character, |
1207 | | * if any, and form final filename component |
1208 | | */ |
1209 | 0 | while (c_ptr != path && c_ptr[-1] != H5_DIR_SEPC) |
1210 | 0 | c_ptr--; |
1211 | |
|
1212 | 0 | len = sep - c_ptr; |
1213 | 0 | assert(len >= 0); |
1214 | |
|
1215 | 0 | out = H5MM_strndup(c_ptr, (size_t)len); |
1216 | 0 | } |
1217 | 0 | } |
1218 | 0 | } |
1219 | |
|
1220 | 0 | if (NULL == out) |
1221 | 0 | HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "can't allocate buffer for basename"); |
1222 | | |
1223 | 0 | *basename = out; |
1224 | |
|
1225 | 0 | done: |
1226 | 0 | if (FAIL == ret_value) { |
1227 | 0 | H5MM_free(out); |
1228 | 0 | if (basename) |
1229 | 0 | *basename = NULL; |
1230 | 0 | } |
1231 | |
|
1232 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
1233 | 0 | } /* end H5_basename() */ |
1234 | | |
1235 | | /* Global variables */ |
1236 | | int H5_opterr = 1; /* Get_option prints errors if this is on */ |
1237 | | int H5_optind = 1; /* Token pointer */ |
1238 | | const char *H5_optarg; /* Flag argument (or value) */ |
1239 | | |
1240 | | /*------------------------------------------------------------------------- |
1241 | | * Function: H5_get_option |
1242 | | * |
1243 | | * Purpose: Determine the command-line options a user specified. We can |
1244 | | * accept both short and long type command-lines. |
1245 | | * |
1246 | | * Return: Success: The short valued "name" of the command line |
1247 | | * parameter or EOF if there are no more |
1248 | | * parameters to process. |
1249 | | * |
1250 | | * Failure: A question mark. |
1251 | | *------------------------------------------------------------------------- |
1252 | | */ |
1253 | | int |
1254 | | H5_get_option(int argc, const char *const *argv, const char *opts, const struct h5_long_options *l_opts) |
1255 | 0 | { |
1256 | 0 | static int sp = 1; /* character index in current token */ |
1257 | 0 | int optchar = '?'; /* option character passed back to user */ |
1258 | |
|
1259 | 0 | if (sp == 1) { |
1260 | | /* check for more flag-like tokens */ |
1261 | 0 | if (H5_optind >= argc || argv[H5_optind][0] != '-' || argv[H5_optind][1] == '\0') { |
1262 | 0 | return EOF; |
1263 | 0 | } |
1264 | 0 | else if (strcmp(argv[H5_optind], "--") == 0) { |
1265 | 0 | H5_optind++; |
1266 | 0 | return EOF; |
1267 | 0 | } |
1268 | 0 | } |
1269 | | |
1270 | 0 | if (sp == 1 && argv[H5_optind][0] == '-' && argv[H5_optind][1] == '-') { |
1271 | | /* long command line option */ |
1272 | 0 | int i; |
1273 | 0 | const char ch = '='; |
1274 | 0 | char *arg = strdup(&argv[H5_optind][2]); |
1275 | 0 | size_t arg_len = 0; |
1276 | |
|
1277 | 0 | H5_optarg = strchr(&argv[H5_optind][2], ch); |
1278 | 0 | arg_len = strlen(&argv[H5_optind][2]); |
1279 | 0 | if (H5_optarg) { |
1280 | 0 | arg_len -= strlen(H5_optarg); |
1281 | 0 | H5_optarg++; /* skip the equal sign */ |
1282 | 0 | } |
1283 | 0 | arg[arg_len] = 0; |
1284 | |
|
1285 | 0 | for (i = 0; l_opts && l_opts[i].name; i++) { |
1286 | 0 | if (strcmp(arg, l_opts[i].name) == 0) { |
1287 | | /* we've found a matching long command line flag */ |
1288 | 0 | optchar = l_opts[i].shortval; |
1289 | |
|
1290 | 0 | if (l_opts[i].has_arg != no_arg) { |
1291 | 0 | if (H5_optarg == NULL) { |
1292 | 0 | if (l_opts[i].has_arg != optional_arg) { |
1293 | 0 | if (H5_optind < (argc - 1)) |
1294 | 0 | if (argv[H5_optind + 1][0] != '-') |
1295 | 0 | H5_optarg = argv[++H5_optind]; |
1296 | 0 | } |
1297 | 0 | else if (l_opts[i].has_arg == require_arg) { |
1298 | 0 | if (H5_opterr) |
1299 | 0 | fprintf(stderr, "%s: option required for \"--%s\" flag\n", argv[0], arg); |
1300 | |
|
1301 | 0 | optchar = '?'; |
1302 | 0 | } |
1303 | 0 | } |
1304 | 0 | } |
1305 | 0 | else { |
1306 | 0 | if (H5_optarg) { |
1307 | 0 | if (H5_opterr) |
1308 | 0 | fprintf(stderr, "%s: no option required for \"%s\" flag\n", argv[0], arg); |
1309 | |
|
1310 | 0 | optchar = '?'; |
1311 | 0 | } |
1312 | 0 | } |
1313 | 0 | break; |
1314 | 0 | } |
1315 | 0 | } |
1316 | |
|
1317 | 0 | if (l_opts && l_opts[i].name == NULL) { |
1318 | | /* exhausted all of the l_opts we have and still didn't match */ |
1319 | 0 | if (H5_opterr) |
1320 | 0 | fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], arg); |
1321 | |
|
1322 | 0 | optchar = '?'; |
1323 | 0 | } |
1324 | |
|
1325 | 0 | H5_optind++; |
1326 | 0 | sp = 1; |
1327 | |
|
1328 | 0 | free(arg); |
1329 | 0 | } |
1330 | 0 | else { |
1331 | 0 | char *cp; /* pointer into current token */ |
1332 | | |
1333 | | /* short command line option */ |
1334 | 0 | optchar = argv[H5_optind][sp]; |
1335 | |
|
1336 | 0 | if (optchar == ':' || (cp = strchr(opts, optchar)) == 0) { |
1337 | 0 | if (H5_opterr) |
1338 | 0 | fprintf(stderr, "%s: unknown option \"%c\"\n", argv[0], optchar); |
1339 | | |
1340 | | /* if no chars left in this token, move to next token */ |
1341 | 0 | if (argv[H5_optind][++sp] == '\0') { |
1342 | 0 | H5_optind++; |
1343 | 0 | sp = 1; |
1344 | 0 | } |
1345 | 0 | return '?'; |
1346 | 0 | } |
1347 | | |
1348 | 0 | if (*++cp == ':') { |
1349 | | /* if a value is expected, get it */ |
1350 | 0 | if (argv[H5_optind][sp + 1] != '\0') { |
1351 | | /* flag value is rest of current token */ |
1352 | 0 | H5_optarg = &argv[H5_optind++][sp + 1]; |
1353 | 0 | } |
1354 | 0 | else if (++H5_optind >= argc) { |
1355 | 0 | if (H5_opterr) |
1356 | 0 | fprintf(stderr, "%s: value expected for option \"%c\"\n", argv[0], optchar); |
1357 | |
|
1358 | 0 | optchar = '?'; |
1359 | 0 | } |
1360 | 0 | else { |
1361 | | /* flag value is next token */ |
1362 | 0 | H5_optarg = argv[H5_optind++]; |
1363 | 0 | } |
1364 | |
|
1365 | 0 | sp = 1; |
1366 | 0 | } |
1367 | | /* wildcard argument */ |
1368 | 0 | else if (*cp == '*') { |
1369 | | /* check the next argument */ |
1370 | 0 | H5_optind++; |
1371 | | /* we do have an extra argument, check if not last */ |
1372 | 0 | if ((H5_optind + 1) < argc) { |
1373 | 0 | if (argv[H5_optind][0] != '-') { |
1374 | 0 | H5_optarg = argv[H5_optind++]; |
1375 | 0 | } |
1376 | 0 | else { |
1377 | 0 | H5_optarg = NULL; |
1378 | 0 | } |
1379 | 0 | } |
1380 | 0 | else { |
1381 | 0 | H5_optarg = NULL; |
1382 | 0 | } |
1383 | 0 | } |
1384 | 0 | else { |
1385 | | /* set up to look at next char in token, next time */ |
1386 | 0 | if (argv[H5_optind][++sp] == '\0') { |
1387 | | /* no more in current token, so setup next token */ |
1388 | 0 | H5_optind++; |
1389 | 0 | sp = 1; |
1390 | 0 | } |
1391 | 0 | H5_optarg = NULL; |
1392 | 0 | } |
1393 | 0 | } |
1394 | | |
1395 | | /* return the current flag character found */ |
1396 | 0 | return optchar; |
1397 | 0 | } |
1398 | | |
1399 | | /*------------------------------------------------------------------------- |
1400 | | * Function: H5_strcasestr |
1401 | | * |
1402 | | * Purpose: Find the first occurrence of the substring needle in the |
1403 | | * string haystack ignoring case. |
1404 | | * |
1405 | | * Return: Success: Pointer to the beginning of the located substring |
1406 | | * |
1407 | | * Failure: NULL |
1408 | | * |
1409 | | *------------------------------------------------------------------------- |
1410 | | */ |
1411 | | char * |
1412 | | H5_strcasestr(const char *haystack, const char *needle) |
1413 | 0 | { |
1414 | | /* Check arguments. */ |
1415 | 0 | assert(haystack); |
1416 | 0 | assert(needle); |
1417 | | |
1418 | | /* begin once from each character of haystack, until needle is found */ |
1419 | 0 | do { |
1420 | 0 | const char *h = haystack; |
1421 | 0 | const char *n = needle; |
1422 | | /* loop while lowercase strings match, or needle ends */ |
1423 | 0 | while (tolower(*h) == tolower(*n) && *n) { |
1424 | 0 | h++; |
1425 | 0 | n++; |
1426 | 0 | } |
1427 | | /* if all characters in needle matched we found it */ |
1428 | 0 | if (*n == 0) { |
1429 | | /* must discard const qualifier here, so turn off the warning */ |
1430 | 0 | H5_WARN_CAST_AWAY_CONST_OFF |
1431 | 0 | return (char *)haystack; |
1432 | 0 | H5_WARN_CAST_AWAY_CONST_ON |
1433 | 0 | } |
1434 | 0 | } while (*haystack++); |
1435 | 0 | return 0; |
1436 | 0 | } /* end H5_strcasestr() */ |
1437 | | |
1438 | | /* |
1439 | | * HDqsort_context - Reentrant qsort with context parameter |
1440 | | * |
1441 | | * Provides a uniform interface to platform-specific reentrant qsort functions. |
1442 | | * All library code should use the GNU/Linux signature for comparator functions: |
1443 | | * |
1444 | | * int comparator(const void *a, const void *b, void *context) |
1445 | | * |
1446 | | * The macro handles platform differences internally, allowing the same |
1447 | | * comparator signature to work on Windows (qsort_s), macOS (BSD qsort_r), |
1448 | | * FreeBSD (use BSD qsort_r < 14.0, but GNU qsort_r >= 14.0), and Linux (GNU qsort_r). |
1449 | | * |
1450 | | * Usage: |
1451 | | * HDqsort_context(base, count, elem_size, compare_func, context); |
1452 | | */ |
1453 | | #if defined(H5_HAVE_WIN32_API) || defined(H5_HAVE_DARWIN) || (defined(__FreeBSD__) && __FreeBSD__ < 14) |
1454 | | /* Need wrapper for Windows, macOS, and FreeBSD < 14 which expect context-first comparators */ |
1455 | | typedef struct HDqsort_context_wrapper_t { |
1456 | | int (*gnu_compar)(const void *, const void *, void *); |
1457 | | void *gnu_arg; |
1458 | | } HDqsort_context_wrapper_t; |
1459 | | |
1460 | | static int |
1461 | | HDqsort_context_wrapper_func(void *wrapper_arg, const void *a, const void *b) |
1462 | | { |
1463 | | HDqsort_context_wrapper_t *w = (HDqsort_context_wrapper_t *)wrapper_arg; |
1464 | | return w->gnu_compar(a, b, w->gnu_arg); |
1465 | | } |
1466 | | |
1467 | | herr_t |
1468 | | HDqsort_context(void *base, size_t nel, size_t size, int (*compar)(const void *, const void *, void *), |
1469 | | void *arg) |
1470 | | { |
1471 | | HDqsort_context_wrapper_t wrapper; |
1472 | | wrapper.gnu_compar = compar; |
1473 | | wrapper.gnu_arg = arg; |
1474 | | #if defined(H5_HAVE_WIN32_API) |
1475 | | qsort_s(base, nel, size, HDqsort_context_wrapper_func, &wrapper); |
1476 | | #elif defined(H5_HAVE_DARWIN) || (defined(__FreeBSD__) && __FreeBSD__ < 14) |
1477 | | /* Old BSD-style: context parameter comes before comparator function */ |
1478 | | qsort_r(base, nel, size, &wrapper, HDqsort_context_wrapper_func); |
1479 | | #endif |
1480 | | return SUCCEED; |
1481 | | } |
1482 | | #endif |
1483 | | |
1484 | | /* |
1485 | | * HDqsort_fallback - Fallback qsort implementation for platforms without qsort_r/qsort_s |
1486 | | * |
1487 | | * This implementation is not threadsafe, since it uses a global variable to store the |
1488 | | * comparator context, then uses standard qsort(). A beta branch of a threadsafe implementation |
1489 | | * of these routines may be found in the 'qsort_r_threadsafe' branch of the HDF5 GitHub repository. |
1490 | | * |
1491 | | */ |
1492 | | #ifndef H5_HAVE_QSORT_REENTRANT |
1493 | | |
1494 | | typedef struct HDqsort_fallback_context_t { |
1495 | | int (*gnu_compar)(const void *, const void *, void *); |
1496 | | void *gnu_arg; |
1497 | | } HDqsort_fallback_context_t; |
1498 | | |
1499 | | /* Non-threadsafe: use global variable */ |
1500 | | static HDqsort_fallback_context_t *HDqsort_fallback_global_ctx = NULL; |
1501 | | |
1502 | | static int |
1503 | | HDqsort_fallback_wrapper(const void *a, const void *b) |
1504 | | { |
1505 | | /* Call the original GNU-style comparator with context from global */ |
1506 | | return HDqsort_fallback_global_ctx->gnu_compar(a, b, HDqsort_fallback_global_ctx->gnu_arg); |
1507 | | } |
1508 | | |
1509 | | herr_t |
1510 | | HDqsort_fallback(void *base, size_t nel, size_t size, int (*compar)(const void *, const void *, void *), |
1511 | | void *arg) |
1512 | | { |
1513 | | HDqsort_fallback_context_t ctx; |
1514 | | |
1515 | | ctx.gnu_compar = compar; |
1516 | | ctx.gnu_arg = arg; |
1517 | | |
1518 | | /* Store context in global variable */ |
1519 | | HDqsort_fallback_global_ctx = &ctx; |
1520 | | |
1521 | | qsort(base, nel, size, HDqsort_fallback_wrapper); |
1522 | | |
1523 | | /* Clear the global pointer */ |
1524 | | HDqsort_fallback_global_ctx = NULL; |
1525 | | |
1526 | | return SUCCEED; |
1527 | | } |
1528 | | |
1529 | | #endif /* !H5_HAVE_QSORT_REENTRANT */ |