ReactOS  0.4.14-dev-342-gdc047f9
dirent.c
Go to the documentation of this file.
1 /* @(#)dirent.c 1.4 17/02/02 Copyright 2011-2017 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static UConst char sccsid[] =
5  "@(#)dirent.c 1.4 17/02/02 Copyright 2011-2017 J. Schilling";
6 #endif
7 /*
8  * Copyright (c) 2011-2017 J. Schilling
9  */
10 /*
11  * The contents of this file are subject to the terms of the
12  * Common Development and Distribution License, Version 1.0 only
13  * (the "License"). You may not use this file except in compliance
14  * with the License.
15  *
16  * See the file CDDL.Schily.txt in this distribution for details.
17  * A copy of the CDDL is also available via the Internet at
18  * http://www.opensource.org/licenses/cddl1.txt
19  *
20  * When distributing Covered Code, include this CDDL HEADER in each
21  * file and include the License file CDDL.Schily.txt from this distribution.
22  */
23 
24 #include <schily/dirent.h>
25 #include <schily/maxpath.h>
26 #include <schily/string.h>
27 #include <schily/errno.h>
28 
29 #ifdef NEED_READDIR
30 
31 #if defined(__MINGW32__) || defined(_MSC_VER)
32 
33 #include <schily/windows.h>
34 #include <schily/utypes.h>
35 #include <schily/schily.h>
36 
37 EXPORT DIR *opendir __PR((const char *));
38 EXPORT int closedir __PR((DIR *));
39 EXPORT struct dirent *readdir __PR((DIR *));
40 
41 EXPORT DIR *
42 opendir(dname)
43  const char *dname;
44 {
45  char path[PATH_MAX];
46  size_t len;
47  uint32_t attr;
48  DIR *dp;
49 
50  if (dname == NULL) {
52  return ((DIR *)0);
53  }
54  len = strlen(dname);
55  if (len > PATH_MAX) {
57  return ((DIR *)0);
58  }
59  if (len == 0) {
61  return ((DIR *)0);
62  }
63 
64  attr = GetFileAttributes(dname);
66  (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
68  return ((DIR *)0);
69  }
70  path[0] = '\0';
71  _fullpath(path, dname, PATH_MAX);
72  len = strlen(path);
73  if (len == 0) {
75  return ((DIR *)0);
76  }
77 
78  dp = malloc(sizeof (*dp) + len + 2); /* Add 2 for "/ *" */
79  if (dp == NULL) {
81  return ((DIR *)0);
82  }
83  strcpy(dp->dd_dirname, path);
84  if (dp->dd_dirname[len-1] != '/' &&
85  dp->dd_dirname[len-1] != '\\') {
86  dp->dd_dirname[len] = '\\';
87  len++;
88  }
89  dp->dd_dirname[len++] = '*';
90  dp->dd_dirname[len] = '\0';
91  dp->dd_handle = -1;
92  dp->dd_state = 0;
93 
94  dp->dd_dir.d_ino = 0;
95  dp->dd_dir.d_reclen = 0;
96  dp->dd_dir.d_namlen = 0;
97  zerobytes(dp->dd_dir.d_name, sizeof (dp->dd_dir.d_name));
98 
99  return (dp);
100 }
101 
102 EXPORT int
103 closedir(dp)
104  DIR *dp;
105 {
106  int ret = 0;
107 
108  if (dp == NULL) {
109  seterrno(EFAULT);
110  return (-1);
111  }
112  if (dp->dd_handle != -1) {
113  ret = _findclose(dp->dd_handle);
114  }
115  free(dp);
116 
117  return (ret);
118 }
119 
120 EXPORT struct dirent *
121 readdir(dp)
122  DIR *dp;
123 {
124  if (dp == NULL) {
125  seterrno(EFAULT);
126  return ((struct dirent *)0);
127  }
128  if (dp->dd_state == (char)-1) {
129  return ((struct dirent *)0);
130  } else if (dp->dd_state == (char)0) {
131  dp->dd_handle = _findfirst(dp->dd_dirname, &(dp->dd_data));
132  if (dp->dd_handle != -1)
133  dp->dd_state = 1;
134  else
135  dp->dd_state = -1;
136  } else {
137  if (_findnext(dp->dd_handle, &(dp->dd_data))) {
138  uint32_t werrno = GetLastError();
139 
140  if (werrno == ERROR_NO_MORE_FILES)
141  seterrno(0);
142  _findclose(dp->dd_handle);
143  dp->dd_handle = -1;
144  dp->dd_state = -1;
145  } else {
146  dp->dd_state = 1; /* state++ to support seekdir */
147  }
148  }
149  if (dp->dd_state > 0) {
150  strlcpy(dp->dd_dir.d_name, dp->dd_data.name,
151  sizeof (dp->dd_dir.d_name));
152 
153  return (&dp->dd_dir);
154  }
155  return ((struct dirent *)0);
156 }
157 
158 #endif /* defined(__MINGW32__) || defined(_MSC_VER) */
159 
160 #endif /* NEED_READDIR */
#define __PR(a)
Definition: prototyp.h:106
static UConst char sccsid[]
Definition: dirent.c:4
_Check_return_ _CRTIMP char *__cdecl _fullpath(_Out_writes_opt_z_(_SizeInBytes) char *_FullPath, _In_z_ const char *_Path, _In_ size_t _SizeInBytes)
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
EXPORT char * zerobytes(void *tov, ssize_t cnt)
Definition: zerobytes.c:30
Definition: arc.h:38
#define free
Definition: debug_ros.c:5
size_t strlcpy(char *d, const char *s, size_t bufsize)
Definition: compat.c:3
#define UConst
Definition: ccomdefs.h:72
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
Definition: arc.h:48
Definition: fatfs.h:198
DIR *__cdecl opendir(const char *)
Definition: dirent.h:39
smooth NULL
Definition: ftsmooth.c:416
#define ERROR_NO_MORE_FILES
Definition: winerror.h:121
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
Definition: arc.h:50
_Check_return_opt_ _CRTIMP int __cdecl _findclose(_In_ intptr_t _FindHandle)
EXPORT int seterrno(int err)
Definition: seterrno.c:34
_CRTIMP int __cdecl _findnext(intptr_t _FindHandle, struct _finddata_t *_FindData)
#define PATH_MAX
Definition: types.h:280
Definition: cookie.c:170
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
int ret
__u8 attr
Definition: mkdosfs.c:359
GLenum GLsizei len
Definition: glext.h:6722
int __cdecl closedir(DIR *)
#define GetFileAttributes
Definition: winbase.h:3639
struct dirent *__cdecl readdir(DIR *)
_CRTIMP intptr_t __cdecl _findfirst(const char *_Filename, struct _finddata_t *_FindData)
Definition: services.c:325
Definition: arc.h:46
UINT32 uint32_t
Definition: types.h:75
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
struct dirent dd_dir
Definition: dirent.h:47
#define malloc
Definition: debug_ros.c:4
long dd_handle
Definition: dirent.h:50