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*/
10#include "dirent_on_windows.h"
11
12#include <errno.h>
13#include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
14#include <stdlib.h>
15#include <string.h>
16
17#include "allocation.h"
18
19#if defined(__cplusplus)
20extern "C" {
21#endif
22
23typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */
24
25struct DIR {
26    handle_type handle; /* -1 for failed rewind */
27    struct _finddata_t info;
28    struct dirent result; /* d_name null iff first time */
29    char *name;           /* null-terminated char string */
30};
31
32DIR *opendir(const VkAllocationCallbacks *pAllocator, const char *name) {
33    DIR *dir = 0;
34
35    if (name && name[0]) {
36        size_t base_length = strlen(name);
37        const char *all = /* search pattern must end with suitable wildcard */
38            strchr("/\\", name[base_length - 1]) ? "*" : "/*";
39        size_t full_length = base_length + strlen(all) + 1;
40
41        if ((dir = (DIR *)loader_alloc(pAllocator, sizeof *dir, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)) != 0 &&
42            (dir->name = (char *)loader_calloc(pAllocator, full_length, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)) != 0) {
43            loader_strncpy(dir->name, full_length, name, base_length);
44            loader_strncat(dir->name, full_length, all, strlen(all));
45
46            if ((dir->handle = (handle_type)_findfirst(dir->name, &dir->info)) != -1) {
47                dir->result.d_name = 0;
48            } else /* rollback */
49            {
50                loader_free(pAllocator, dir->name);
51                loader_free(pAllocator, dir);
52                dir = 0;
53            }
54        } else /* rollback */
55        {
56            loader_free(pAllocator, dir);
57            dir = 0;
58            errno = ENOMEM;
59        }
60    } else {
61        errno = EINVAL;
62    }
63
64    return dir;
65}
66
67int closedir(const VkAllocationCallbacks *pAllocator, DIR *dir) {
68    int result = -1;
69
70    if (dir) {
71        if (dir->handle != -1) {
72            result = _findclose(dir->handle);
73        }
74
75        loader_free(pAllocator, dir->name);
76        loader_free(pAllocator, dir);
77    }
78
79    if (result == -1) /* map all errors to EBADF */
80    {
81        errno = EBADF;
82    }
83
84    return result;
85}
86
87struct dirent *readdir(DIR *dir) {
88    struct dirent *result = 0;
89
90    if (dir && dir->handle != -1) {
91        if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) {
92            result = &dir->result;
93            result->d_name = dir->info.name;
94        }
95    } else {
96        errno = EBADF;
97    }
98
99    return result;
100}
101
102void rewinddir(DIR *dir) {
103    if (dir && dir->handle != -1) {
104        _findclose(dir->handle);
105        dir->handle = (handle_type)_findfirst(dir->name, &dir->info);
106        dir->result.d_name = 0;
107    } else {
108        errno = EBADF;
109    }
110}
111
112#if defined(__cplusplus)
113}
114#endif
115
116/*
117
118    Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved.
119    Copyright (c) 2015 The Khronos Group Inc.
120    Copyright (c) 2015 Valve Corporation
121    Copyright (c) 2015 LunarG, Inc.
122    Permission to use, copy, modify, and distribute this software and its
123    documentation for any purpose is hereby granted without fee, provided
124    that this copyright and permissions notice appear in all copies and
125    derivatives.
126
127    This software is supplied "as is" without express or implied warranty.
128
129    But that said, if there are any problems please get in touch.
130
131*/
132