Changeset 3140 for trunk/src/kmk/w32/compat
- Timestamp:
- Mar 14, 2018, 10:28:10 PM (7 years ago)
- Location:
- trunk/src/kmk
- Files:
-
- 2 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk
-
Property svn:mergeinfo
set to
/vendor/gnumake/current merged eligible
-
Property svn:mergeinfo
set to
-
trunk/src/kmk/w32/compat/dirent.c
r2591 r3140 1 1 /* Directory entry code for Window platforms. 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 2 Copyright (C) 1996-2016 Free Software Foundation, Inc. 4 3 This file is part of GNU Make. 5 4 … … 32 31 opendir(const char* pDirName) 33 32 { 34 35 DIR*pDir;36 char*pEndDirName;37 intnBufferLen;38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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; 84 83 85 84 #ifdef KMK_PRF 86 85 fprintf(stderr, "opendir(%s) -> %p\n", pDirName, pDir); 87 86 #endif 88 87 return pDir; 89 88 } 90 89 … … 92 91 closedir(DIR *pDir) 93 92 { 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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; 113 112 } 114 113 … … 116 115 readdir(DIR* pDir) 117 116 { 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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; 146 145 } 147 146 … … 149 148 rewinddir(DIR* pDir) 150 149 { 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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; 172 171 } 173 172 … … 175 174 telldir(DIR* pDir) 176 175 { 177 178 179 180 181 182 183 184 185 186 187 188 189 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; 190 189 } 191 190 … … 193 192 seekdir(DIR* pDir, long nPosition) 194 193 { 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 } 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 }
Note:
See TracChangeset
for help on using the changeset viewer.