2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * readdir.c - A minimal implementation of readdir to ease porting of AFS to
12 * NT. Include dirent.h to pickup the required structs and prototypes.
14 * Implemented routines:
19 #include <afs/param.h>
21 #include <afs/errmap_nt.h>
28 /* opendir() - The case insensitive version of opendir */
29 DIR *opendir(const char *path)
34 WIN32_FIND_DATA tData;
37 (void) strcpy(tPath, path);
38 (void) strcat(tPath, "\\*");
39 tH = FindFirstFile(tPath, &tData);
41 if (tH == INVALID_HANDLE_VALUE) {
42 ntErr = GetLastError();
47 case ERROR_BAD_PATHNAME:
48 /* AFS NT client returns ERROR_BAD_PATHNAME where it should return
51 case ERROR_FILE_NOT_FOUND:
52 /* If at the "root" directory, then this can happen if it's empty.
56 int len = strlen(tPath) - 1;
58 if (len >= 2 && tPath[len-2] != ':') {
61 if (stat(tPath, &status)<0) {
62 errno = nterr_nt2unix(GetLastError(), ENOENT);
65 if (!(status.st_mode & _S_IFDIR)) {
72 errno = nterr_nt2unix(GetLastError(), ENOENT);
77 tDir = (DIR*)malloc(sizeof(DIR));
82 memset((void*)tDir, 0, sizeof(*tDir));
89 int closedir(DIR *dir)
91 if (!dir || !dir->h) {
96 if (dir->h != INVALID_HANDLE_VALUE)
102 struct dirent *readdir(DIR *dir)
108 return (struct dirent*)0;
112 if (dir->h == INVALID_HANDLE_VALUE)
113 return (struct dirent*)0;
119 while(rc = FindNextFile(dir->h, &dir->data)) {
120 if ((strcmp(dir->data.cFileName, ".") == 0) ||
121 (strcmp(dir->data.cFileName, "..") == 0)) {
123 continue; /* skip "." and ".." */
125 break; /* found a non '.' or '..' entry. */
127 if (rc == 0) { /* FindNextFile() failed */
128 if (GetLastError() == ERROR_NO_MORE_FILES)
129 return (struct dirent*)0;
131 errno = nterr_nt2unix(GetLastError(), EBADF);
132 return (struct dirent*)0;
137 dir->cdirent.d_name = dir->data.cFileName;
138 return &dir->cdirent;