Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | Implementation of POSIX directory browsing functions and types for Win32. |
| 4 | |
| 5 | Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com) |
| 6 | History: Created March 1997. Updated June 2003 and July 2012. |
| 7 | Rights: See end of file. |
| 8 | |
| 9 | */ |
Aaron Karp | 848bad4 | 2016-03-13 16:41:59 -0400 | [diff] [blame] | 10 | #include "dirent_on_windows.h" |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | #include <io.h> /* _findfirst and _findnext set errno iff they return -1 */ |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
Jon Ashburn | 480a50a | 2015-08-28 14:58:46 -0700 | [diff] [blame] | 15 | #include "vk_loader_platform.h" |
Jon Ashburn | 87d6aa9 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 16 | #include "loader.h" |
Jon Ashburn | 480a50a | 2015-08-28 14:58:46 -0700 | [diff] [blame] | 17 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 18 | #ifdef __cplusplus |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 19 | extern "C" { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 20 | #endif |
| 21 | |
| 22 | typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */ |
| 23 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 24 | struct DIR { |
| 25 | handle_type handle; /* -1 for failed rewind */ |
| 26 | struct _finddata_t info; |
| 27 | struct dirent result; /* d_name null iff first time */ |
| 28 | char *name; /* null-terminated char string */ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 31 | DIR *opendir(const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 32 | DIR *dir = 0; |
| 33 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 34 | if (name && name[0]) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 35 | size_t base_length = strlen(name); |
| 36 | const char *all = /* search pattern must end with suitable wildcard */ |
| 37 | strchr("/\\", name[base_length - 1]) ? "*" : "/*"; |
| 38 | |
Mark Young | 0ad8313 | 2016-06-30 13:02:42 -0600 | [diff] [blame] | 39 | if ((dir = (DIR *)loader_instance_tls_heap_alloc(sizeof *dir)) != 0 && |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 40 | (dir->name = (char *)loader_instance_tls_heap_alloc(base_length + strlen(all) + 1)) != 0) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 41 | strcat(strcpy(dir->name, name), all); |
| 42 | |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 43 | if ((dir->handle = (handle_type)_findfirst(dir->name, &dir->info)) != -1) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 44 | dir->result.d_name = 0; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 45 | } else /* rollback */ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 46 | { |
Mark Young | 0ad8313 | 2016-06-30 13:02:42 -0600 | [diff] [blame] | 47 | loader_instance_tls_heap_free(dir->name); |
| 48 | loader_instance_tls_heap_free(dir); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 49 | dir = 0; |
| 50 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 51 | } else /* rollback */ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 52 | { |
Mark Young | 0ad8313 | 2016-06-30 13:02:42 -0600 | [diff] [blame] | 53 | loader_instance_tls_heap_free(dir); |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 54 | dir = 0; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 55 | errno = ENOMEM; |
| 56 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 57 | } else { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 58 | errno = EINVAL; |
| 59 | } |
| 60 | |
| 61 | return dir; |
| 62 | } |
| 63 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 64 | int closedir(DIR *dir) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 65 | int result = -1; |
| 66 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 67 | if (dir) { |
| 68 | if (dir->handle != -1) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 69 | result = _findclose(dir->handle); |
| 70 | } |
| 71 | |
Mark Young | 0ad8313 | 2016-06-30 13:02:42 -0600 | [diff] [blame] | 72 | loader_instance_tls_heap_free(dir->name); |
| 73 | loader_instance_tls_heap_free(dir); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 76 | if (result == -1) /* map all errors to EBADF */ |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 77 | { |
| 78 | errno = EBADF; |
| 79 | } |
| 80 | |
| 81 | return result; |
| 82 | } |
| 83 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 84 | struct dirent *readdir(DIR *dir) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 85 | struct dirent *result = 0; |
| 86 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 87 | if (dir && dir->handle != -1) { |
| 88 | if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) { |
| 89 | result = &dir->result; |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 90 | result->d_name = dir->info.name; |
| 91 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 92 | } else { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 93 | errno = EBADF; |
| 94 | } |
| 95 | |
| 96 | return result; |
| 97 | } |
| 98 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 99 | void rewinddir(DIR *dir) { |
| 100 | if (dir && dir->handle != -1) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 101 | _findclose(dir->handle); |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 102 | dir->handle = (handle_type)_findfirst(dir->name, &dir->info); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 103 | dir->result.d_name = 0; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 104 | } else { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 105 | errno = EBADF; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | #ifdef __cplusplus |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | /* |
| 114 | |
| 115 | Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved. |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 116 | Copyright (c) 2015 The Khronos Group Inc. |
| 117 | Copyright (c) 2015 Valve Corporation |
| 118 | Copyright (c) 2015 LunarG, Inc. |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 119 | Permission to use, copy, modify, and distribute this software and its |
| 120 | documentation for any purpose is hereby granted without fee, provided |
| 121 | that this copyright and permissions notice appear in all copies and |
| 122 | derivatives. |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 123 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 124 | This software is supplied "as is" without express or implied warranty. |
| 125 | |
| 126 | But that said, if there are any problems please get in touch. |
| 127 | |
| 128 | */ |