| 1 | /* Directory entry code for Window platforms.
|
|---|
| 2 | Copyright (C) 1996-2016 Free Software Foundation, Inc.
|
|---|
| 3 | This file is part of GNU Make.
|
|---|
| 4 |
|
|---|
| 5 | GNU Make is free software; you can redistribute it and/or modify it under the
|
|---|
| 6 | terms of the GNU General Public License as published by the Free Software
|
|---|
| 7 | Foundation; either version 3 of the License, or (at your option) any later
|
|---|
| 8 | version.
|
|---|
| 9 |
|
|---|
| 10 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|---|
| 12 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License along with
|
|---|
| 15 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | #include <config.h>
|
|---|
| 19 | #include <sys/types.h>
|
|---|
| 20 | #include <sys/stat.h>
|
|---|
| 21 | #include <errno.h>
|
|---|
| 22 | #include <string.h>
|
|---|
| 23 | #include <stdlib.h>
|
|---|
| 24 | #ifdef KMK_PRF
|
|---|
| 25 | # include <stdio.h>
|
|---|
| 26 | #endif
|
|---|
| 27 | #include "dirent.h"
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | DIR*
|
|---|
| 31 | opendir(const char* pDirName)
|
|---|
| 32 | {
|
|---|
| 33 | struct stat sb;
|
|---|
| 34 | DIR* pDir;
|
|---|
| 35 | char* pEndDirName;
|
|---|
| 36 | int nBufferLen;
|
|---|
| 37 |
|
|---|
| 38 | /* sanity checks */
|
|---|
| 39 | if (!pDirName) {
|
|---|
| 40 | errno = EINVAL;
|
|---|
| 41 | return NULL;
|
|---|
| 42 | }
|
|---|
| 43 | if (stat(pDirName, &sb) != 0) {
|
|---|
| 44 | errno = ENOENT;
|
|---|
| 45 | return NULL;
|
|---|
| 46 | }
|
|---|
| 47 | if ((sb.st_mode & S_IFMT) != S_IFDIR) {
|
|---|
| 48 | errno = ENOTDIR;
|
|---|
| 49 | return NULL;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /* allocate a DIR structure to return */
|
|---|
| 53 | pDir = (DIR *) malloc(sizeof (DIR));
|
|---|
| 54 |
|
|---|
| 55 | if (!pDir)
|
|---|
| 56 | return NULL;
|
|---|
| 57 |
|
|---|
| 58 | /* input directory name length */
|
|---|
| 59 | nBufferLen = strlen(pDirName);
|
|---|
| 60 |
|
|---|
| 61 | /* copy input directory name to DIR buffer */
|
|---|
| 62 | strcpy(pDir->dir_pDirectoryName, pDirName);
|
|---|
| 63 |
|
|---|
| 64 | /* point to end of the copied directory name */
|
|---|
| 65 | pEndDirName = &pDir->dir_pDirectoryName[nBufferLen - 1];
|
|---|
| 66 |
|
|---|
| 67 | /* if directory name did not end in '/' or '\', add '/' */
|
|---|
| 68 | if ((*pEndDirName != '/') && (*pEndDirName != '\\')) {
|
|---|
| 69 | pEndDirName++;
|
|---|
| 70 | *pEndDirName = '/';
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /* now append the wildcard character to the buffer */
|
|---|
| 74 | pEndDirName++;
|
|---|
| 75 | *pEndDirName = '*';
|
|---|
| 76 | pEndDirName++;
|
|---|
| 77 | *pEndDirName = '\0';
|
|---|
| 78 |
|
|---|
| 79 | /* other values defaulted */
|
|---|
| 80 | pDir->dir_nNumFiles = 0;
|
|---|
| 81 | pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
|
|---|
| 82 | pDir->dir_ulCookie = __DIRENT_COOKIE;
|
|---|
| 83 |
|
|---|
| 84 | #ifdef KMK_PRF
|
|---|
| 85 | fprintf(stderr, "opendir(%s) -> %p\n", pDirName, pDir);
|
|---|
| 86 | #endif
|
|---|
| 87 | return pDir;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void
|
|---|
| 91 | closedir(DIR *pDir)
|
|---|
| 92 | {
|
|---|
| 93 | /* got a valid pointer? */
|
|---|
| 94 | if (!pDir) {
|
|---|
| 95 | errno = EINVAL;
|
|---|
| 96 | return;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /* sanity check that this is a DIR pointer */
|
|---|
| 100 | if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
|
|---|
| 101 | errno = EINVAL;
|
|---|
| 102 | return;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /* close the WINDOWS32 directory handle */
|
|---|
| 106 | if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
|
|---|
| 107 | FindClose(pDir->dir_hDirHandle);
|
|---|
| 108 |
|
|---|
| 109 | free(pDir);
|
|---|
| 110 |
|
|---|
| 111 | return;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | struct dirent *
|
|---|
| 115 | readdir(DIR* pDir)
|
|---|
| 116 | {
|
|---|
| 117 | WIN32_FIND_DATA wfdFindData;
|
|---|
| 118 |
|
|---|
| 119 | if (!pDir) {
|
|---|
| 120 | errno = EINVAL;
|
|---|
| 121 | return NULL;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /* sanity check that this is a DIR pointer */
|
|---|
| 125 | if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
|
|---|
| 126 | errno = EINVAL;
|
|---|
| 127 | return NULL;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | if (pDir->dir_nNumFiles == 0) {
|
|---|
| 131 | pDir->dir_hDirHandle = FindFirstFile(pDir->dir_pDirectoryName, &wfdFindData);
|
|---|
| 132 | if (pDir->dir_hDirHandle == INVALID_HANDLE_VALUE)
|
|---|
| 133 | return NULL;
|
|---|
| 134 | } else if (!FindNextFile(pDir->dir_hDirHandle, &wfdFindData))
|
|---|
| 135 | return NULL;
|
|---|
| 136 |
|
|---|
| 137 | /* bump count for next call to readdir() or telldir() */
|
|---|
| 138 | pDir->dir_nNumFiles++;
|
|---|
| 139 |
|
|---|
| 140 | /* fill in struct dirent values */
|
|---|
| 141 | pDir->dir_sdReturn.d_ino = (ino_t)-1;
|
|---|
| 142 | strcpy(pDir->dir_sdReturn.d_name, wfdFindData.cFileName);
|
|---|
| 143 |
|
|---|
| 144 | return &pDir->dir_sdReturn;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | void
|
|---|
| 148 | rewinddir(DIR* pDir)
|
|---|
| 149 | {
|
|---|
| 150 | if (!pDir) {
|
|---|
| 151 | errno = EINVAL;
|
|---|
| 152 | return;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /* sanity check that this is a DIR pointer */
|
|---|
| 156 | if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
|
|---|
| 157 | errno = EINVAL;
|
|---|
| 158 | return;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /* close the WINDOWS32 directory handle */
|
|---|
| 162 | if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
|
|---|
| 163 | if (!FindClose(pDir->dir_hDirHandle))
|
|---|
| 164 | errno = EBADF;
|
|---|
| 165 |
|
|---|
| 166 | /* reset members which control readdir() */
|
|---|
| 167 | pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
|
|---|
| 168 | pDir->dir_nNumFiles = 0;
|
|---|
| 169 |
|
|---|
| 170 | return;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | int
|
|---|
| 174 | telldir(DIR* pDir)
|
|---|
| 175 | {
|
|---|
| 176 | if (!pDir) {
|
|---|
| 177 | errno = EINVAL;
|
|---|
| 178 | return -1;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /* sanity check that this is a DIR pointer */
|
|---|
| 182 | if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
|
|---|
| 183 | errno = EINVAL;
|
|---|
| 184 | return -1;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /* return number of times readdir() called */
|
|---|
| 188 | return pDir->dir_nNumFiles;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | void
|
|---|
| 192 | seekdir(DIR* pDir, long nPosition)
|
|---|
| 193 | {
|
|---|
| 194 | if (!pDir)
|
|---|
| 195 | return;
|
|---|
| 196 |
|
|---|
| 197 | /* sanity check that this is a DIR pointer */
|
|---|
| 198 | if (pDir->dir_ulCookie != __DIRENT_COOKIE)
|
|---|
| 199 | return;
|
|---|
| 200 |
|
|---|
| 201 | /* go back to beginning of directory */
|
|---|
| 202 | rewinddir(pDir);
|
|---|
| 203 |
|
|---|
| 204 | /* loop until we have found position we care about */
|
|---|
| 205 | for (--nPosition; nPosition && readdir(pDir); nPosition--);
|
|---|
| 206 |
|
|---|
| 207 | /* flag invalid nPosition value */
|
|---|
| 208 | if (nPosition)
|
|---|
| 209 | errno = EINVAL;
|
|---|
| 210 |
|
|---|
| 211 | return;
|
|---|
| 212 | }
|
|---|