Home | History | Annotate | Download | only in loader
      1 /*
      2 
      3     Implementation of POSIX directory browsing functions and types for Win32.
      4 
      5     Author:  Kevlin Henney (kevlin (at) acm.org, kevlin (at) 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 #include <errno.h>
     12 #include <io.h> /* _findfirst and _findnext set errno iff they return -1 */
     13 #include <stdlib.h>
     14 #include <string.h>
     15 #include "vk_loader_platform.h"
     16 #include "loader.h"
     17 
     18 #ifdef __cplusplus
     19 extern "C" {
     20 #endif
     21 
     22 typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */
     23 
     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 */
     29 };
     30 
     31 DIR *opendir(const char *name) {
     32     DIR *dir = 0;
     33 
     34     if (name && name[0]) {
     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 
     39         if ((dir = (DIR *)loader_tls_heap_alloc(sizeof *dir)) != 0 &&
     40             (dir->name = (char *)loader_tls_heap_alloc(base_length +
     41                                                        strlen(all) + 1)) != 0) {
     42             strcat(strcpy(dir->name, name), all);
     43 
     44             if ((dir->handle =
     45                      (handle_type)_findfirst(dir->name, &dir->info)) != -1) {
     46                 dir->result.d_name = 0;
     47             } else /* rollback */
     48             {
     49                 loader_tls_heap_free(dir->name);
     50                 loader_tls_heap_free(dir);
     51                 dir = 0;
     52             }
     53         } else /* rollback */
     54         {
     55             loader_tls_heap_free(dir);
     56             dir = 0;
     57             errno = ENOMEM;
     58         }
     59     } else {
     60         errno = EINVAL;
     61     }
     62 
     63     return dir;
     64 }
     65 
     66 int closedir(DIR *dir) {
     67     int result = -1;
     68 
     69     if (dir) {
     70         if (dir->handle != -1) {
     71             result = _findclose(dir->handle);
     72         }
     73 
     74         loader_tls_heap_free(dir->name);
     75         loader_tls_heap_free(dir);
     76     }
     77 
     78     if (result == -1) /* map all errors to EBADF */
     79     {
     80         errno = EBADF;
     81     }
     82 
     83     return result;
     84 }
     85 
     86 struct dirent *readdir(DIR *dir) {
     87     struct dirent *result = 0;
     88 
     89     if (dir && dir->handle != -1) {
     90         if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) {
     91             result = &dir->result;
     92             result->d_name = dir->info.name;
     93         }
     94     } else {
     95         errno = EBADF;
     96     }
     97 
     98     return result;
     99 }
    100 
    101 void rewinddir(DIR *dir) {
    102     if (dir && dir->handle != -1) {
    103         _findclose(dir->handle);
    104         dir->handle = (handle_type)_findfirst(dir->name, &dir->info);
    105         dir->result.d_name = 0;
    106     } else {
    107         errno = EBADF;
    108     }
    109 }
    110 
    111 #ifdef __cplusplus
    112 }
    113 #endif
    114 
    115 /*
    116 
    117     Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved.
    118     Copyright (c) 2015 The Khronos Group Inc.
    119     Copyright (c) 2015 Valve Corporation
    120     Copyright (c) 2015 LunarG, Inc.
    121     Permission to use, copy, modify, and distribute this software and its
    122     documentation for any purpose is hereby granted without fee, provided
    123     that this copyright and permissions notice appear in all copies and
    124     derivatives.
    125 
    126     This software is supplied "as is" without express or implied warranty.
    127 
    128     But that said, if there are any problems please get in touch.
    129 
    130 */
    131