1 /* @(#)dirent.c 1.3 12/03/20 Copyright 2011 J. Schilling */
2 #include <schily/mconfig.h>
4 static UConst
char sccsid
[] =
5 "@(#)dirent.c 1.3 12/03/20 Copyright 2011 J. Schilling";
8 * Copyright (c) 2011 J. Schilling
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
16 * See the file CDDL.Schily.txt in this distribution for details.
18 * When distributing Covered Code, include this CDDL HEADER in each
19 * file and include the License file CDDL.Schily.txt from this distribution.
22 #include <schily/dirent.h>
23 #include <schily/maxpath.h>
24 #include <schily/string.h>
25 #include <schily/errno.h>
29 #if defined(__MINGW32__) || defined(_MSC_VER)
31 #include <schily/windows.h>
32 #include <schily/utypes.h>
33 #include <schily/schily.h>
35 EXPORT
DIR *opendir
__PR((const char *));
36 EXPORT
int closedir
__PR((DIR *));
37 EXPORT
struct dirent
*readdir
__PR((DIR *));
54 seterrno(ENAMETOOLONG
);
62 attr
= GetFileAttributes(dname
);
63 if (attr
== INVALID_FILE_ATTRIBUTES
||
64 (attr
& FILE_ATTRIBUTE_DIRECTORY
) == 0) {
69 _fullpath(path
, dname
, PATH_MAX
);
76 dp
= malloc(sizeof (*dp
) + len
+ 2); /* Add 2 for "/ *" */
81 strcpy(dp
->dd_dirname
, path
);
82 if (dp
->dd_dirname
[len
-1] != '/' &&
83 dp
->dd_dirname
[len
-1] != '\\') {
84 dp
->dd_dirname
[len
] = '\\';
87 dp
->dd_dirname
[len
] = '*';
92 dp
->dd_dir
.d_reclen
= 0;
93 dp
->dd_dir
.d_namlen
= 0;
94 zerobytes(dp
->dd_dir
.d_name
, sizeof (dp
->dd_dir
.d_name
));
109 if (dp
->dd_handle
!= -1) {
110 ret
= _findclose(dp
->dd_handle
);
117 EXPORT
struct dirent
*
123 return ((struct dirent
*)0);
125 if (dp
->dd_state
== (char)-1) {
126 return ((struct dirent
*)0);
127 } else if (dp
->dd_state
== (char)0) {
128 dp
->dd_handle
= _findfirst(dp
->dd_dirname
, &(dp
->dd_data
));
129 if (dp
->dd_handle
!= -1)
134 if (_findnext(dp
->dd_handle
, &(dp
->dd_data
))) {
135 uint32_t werrno
= GetLastError();
137 if (werrno
== ERROR_NO_MORE_FILES
)
139 _findclose(dp
->dd_handle
);
143 dp
->dd_state
= 1; /* state++ to support seekdir */
146 if (dp
->dd_state
> 0) {
147 strlcpy(dp
->dd_dir
.d_name
, dp
->dd_data
.name
,
148 sizeof (dp
->dd_dir
.d_name
));
150 return (&dp
->dd_dir
);
152 return ((struct dirent
*)0);
155 #endif /* defined(__MINGW32__) || defined(_MSC_VER) */
157 #endif /* NEED_READDIR */