| 124 | /* definitions*/ |
| 125 | |
| 126 | _TINYDIR_FUNC |
| 127 | int tinydir_open(tinydir_dir *dir, const char *path) { |
| 128 | if (dir == NULL || path == NULL || strlen(path) == 0) { |
| 129 | errno = EINVAL; |
| 130 | return -1; |
| 131 | } |
| 132 | if (strlen(path) + _TINYDIR_PATH_EXTRA >= _TINYDIR_PATH_MAX) { |
| 133 | errno = ENAMETOOLONG; |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | /* initialise dir */ |
| 138 | dir->_files = NULL; |
| 139 | #ifdef _WIN32 |
| 140 | dir->_h = INVALID_HANDLE_VALUE; |
| 141 | #else |
| 142 | dir->_d = NULL; |
| 143 | #endif |
| 144 | tinydir_close(dir); |
| 145 | |
| 146 | strcpy(dir->path, path); |
| 147 | #ifdef _WIN32 |
| 148 | strcat(dir->path, "\\*"); |
| 149 | dir->_h = FindFirstFileA(dir->path, &dir->_f); |
| 150 | dir->path[strlen(dir->path) - 2] = '\0'; |
| 151 | if (dir->_h == INVALID_HANDLE_VALUE) |
| 152 | #else |
| 153 | dir->_d = opendir(path); |
| 154 | if (dir->_d == NULL) |
| 155 | #endif |
| 156 | { |
| 157 | errno = ENOENT; |
| 158 | goto bail; |
| 159 | } |
| 160 | |
| 161 | /* read first file */ |
| 162 | dir->has_next = 1; |
| 163 | #ifndef _WIN32 |
| 164 | dir->_e = readdir(dir->_d); |
| 165 | if (dir->_e == NULL) { |
| 166 | dir->has_next = 0; |
| 167 | } |
| 168 | #endif |
| 169 | |
| 170 | return 0; |
| 171 | |
| 172 | bail: |
| 173 | tinydir_close(dir); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | _TINYDIR_FUNC |
| 178 | int tinydir_open_sorted(tinydir_dir *dir, const char *path) { |
no test coverage detected