| 1 | /* |
| 2 | * dirent.c |
| 3 | * |
| 4 | * Author: Shuichi Kitaguchi <[email protected]> |
| 5 | * Created: Jun 17, 2001 |
| 6 | * Revised: |
| 7 | * |
| 8 | * Code: |
| 9 | */ |
| 10 | |
| 11 | #include <windows.h> |
| 12 | #include <stdio.h> |
| 13 | #include <mbstring.h> |
| 14 | |
| 15 | #include <w32.h> |
| 16 | #include <dirent.h> |
| 17 | |
| 18 | |
| 19 | /* |
| 20 | * XXX: these functions may return invalid errno. |
| 21 | */ |
| 22 | |
| 23 | DIR * |
| 24 | opendir(const char *filename) |
| 25 | { |
| 26 | DIR *dir; |
| 27 | size_t len = strlen(filename); |
| 28 | char *pt; |
| 29 | |
| 30 | dir = malloc(sizeof(DIR)); |
| 31 | if (dir == NULL) return NULL; |
| 32 | |
| 33 | /* initialize DIR strucure */ |
| 34 | memset(dir, 0, sizeof(DIR)); |
| 35 | dir->hFind = INVALID_HANDLE_VALUE; |
| 36 | |
| 37 | if (strlen(filename) == 0){ |
| 38 | strcpy(dir->dd_filename, "*.*"); |
| 39 | } else { |
| 40 | while ((pt=_mbschr(dir->dd_filename, '/')) != NULL ) |
| 41 | *pt = '\\'; |
| 42 | if (filename[len-1] == '\\') |
| 43 | _snprintf(dir->dd_filename, sizeof(dir->dd_filename), |
| 44 | "%s*.*", filename); |
| 45 | else |
| 46 | _snprintf(dir->dd_filename, sizeof(dir->dd_filename), |
| 47 | "%s\\*.*", filename); |
| 48 | } |
| 49 | |
| 50 | return dir; |
| 51 | } |
| 52 | |
| 53 | struct dirent * |
| 54 | readdir(DIR *dir) |
| 55 | { |
| 56 | WIN32_FIND_DATA wfd; |
| 57 | |
| 58 | if (dir->hFind == INVALID_HANDLE_VALUE){ |
| 59 | dir->hFind = FindFirstFile(dir->dd_filename, &wfd); |
| 60 | if (dir->hFind == INVALID_HANDLE_VALUE) return NULL; |
| 61 | } else { |
| 62 | if (!FindNextFile(dir->hFind, &wfd)) return NULL; |
| 63 | } |
| 64 | |
| 65 | /* store file attributes */ |
| 66 | dir->dir.d_ino++; /* XXX fake */ |
| 67 | dir->dir.d_namlen = strlen(wfd.cFileName); |
| 68 | strcpy(dir->dir.d_name, wfd.cFileName); |
| 69 | |
| 70 | return &dir->dir; |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | closedir(DIR *dir) |
| 75 | { |
| 76 | if (dir->hFind != INVALID_HANDLE_VALUE) |
| 77 | FindClose(dir->hFind); |
| 78 | free(dir); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Copyright (C) 2001-2003 Mew developing team. |
| 85 | * All rights reserved. |
| 86 | * |
| 87 | * Redistribution and use in source and binary forms, with or without |
| 88 | * modification, are permitted provided that the following conditions |
| 89 | * are met: |
| 90 | * |
| 91 | * 1. Redistributions of source code must retain the above copyright |
| 92 | * notice, this list of conditions and the following disclaimer. |
| 93 | * 2. Redistributions in binary form must reproduce the above copyright |
| 94 | * notice, this list of conditions and the following disclaimer in the |
| 95 | * documentation and/or other materials provided with the distribution. |
| 96 | * 3. Neither the name of the team nor the names of its contributors |
| 97 | * may be used to endorse or promote products derived from this software |
| 98 | * without specific prior written permission. |
| 99 | * |
| 100 | * THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND |
| 101 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 102 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 103 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE |
| 104 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 105 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 106 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 107 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 108 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 109 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 110 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 111 | */ |
| 112 | |
| 113 | /* |
| 114 | * dirent.c ends here |
| 115 | */ |