~mingw-w64/mingw-w64/experimental

« back to all changes in this revision

Viewing changes to ros-privexp/mingw-w64-crt/misc/dirent.c

  • Committer: NightStrike
  • Date: 2010-08-11 22:20:57 UTC
  • Revision ID: svn-v4:4407c894-4637-0410-b4f5-ada5f102cad1:experimental:3266
Branch for adding option for supporting ros

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef WIN32_LEAN_AND_MEAN
 
2
#define WIN32_LEAN_AND_MEAN
 
3
#endif
 
4
 
 
5
#include <stdio.h>
 
6
#include <stdlib.h>
 
7
#include <errno.h>
 
8
#include <string.h>
 
9
#include <io.h>
 
10
#include <direct.h>
 
11
#include <dirent.h>
 
12
 
 
13
#include <windows.h>
 
14
 
 
15
#include <tchar.h>
 
16
#define SUFFIX _T("*")
 
17
#define SLASH _T("\\")
 
18
 
 
19
/* Helper for opendir().  */
 
20
static inline unsigned _tGetFileAttributes (const _TCHAR * tPath)
 
21
{
 
22
#ifdef _UNICODE
 
23
  /* GetFileAttributesW does not work on W9x, so convert to ANSI */
 
24
  if (_osver & 0x8000)
 
25
    {
 
26
      char aPath [MAX_PATH];
 
27
      WideCharToMultiByte (CP_ACP, 0, tPath, -1, aPath, MAX_PATH, NULL,
 
28
                           NULL);
 
29
      return GetFileAttributesA (aPath);
 
30
    }
 
31
  return GetFileAttributesW (tPath);
 
32
#else
 
33
  return GetFileAttributesA (tPath);
 
34
#endif
 
35
}
 
36
 
 
37
_TDIR *
 
38
_topendir (const _TCHAR *szPath)
 
39
{
 
40
  _TDIR *nd;
 
41
  unsigned int rc;
 
42
  _TCHAR szFullPath[MAX_PATH];
 
43
 
 
44
  errno = 0;
 
45
 
 
46
  if (!szPath)
 
47
    {
 
48
      errno = EFAULT;
 
49
      return (_TDIR *) 0;
 
50
    }
 
51
 
 
52
  if (szPath[0] == _T('\0'))
 
53
    {
 
54
      errno = ENOTDIR;
 
55
      return (_TDIR *) 0;
 
56
    }
 
57
 
 
58
  rc = _tGetFileAttributes (szPath);
 
59
  if (rc == (unsigned int)-1)
 
60
    {
 
61
 
 
62
      errno = ENOENT;
 
63
      return (_TDIR *) 0;
 
64
    }
 
65
  if (!(rc & FILE_ATTRIBUTE_DIRECTORY))
 
66
    {
 
67
 
 
68
      errno = ENOTDIR;
 
69
      return (_TDIR *) 0;
 
70
    }
 
71
 
 
72
  _tfullpath (szFullPath,szPath,MAX_PATH);
 
73
 
 
74
  nd = (_TDIR *) malloc (sizeof (_TDIR) + (_tcslen (szFullPath)
 
75
                                           + _tcslen (SLASH)
 
76
                                           + _tcslen (SUFFIX) + 1)
 
77
                                          * sizeof (_TCHAR));
 
78
 
 
79
  if (!nd)
 
80
    {
 
81
 
 
82
      errno = ENOMEM;
 
83
      return (_TDIR *) 0;
 
84
    }
 
85
 
 
86
  _tcscpy (nd->dd_name,szFullPath);
 
87
 
 
88
  if (nd->dd_name[0] != _T('\0')
 
89
      && _tcsrchr (nd->dd_name,_T('/')) != nd->dd_name
 
90
                                            + _tcslen (nd->dd_name) - 1
 
91
      && _tcsrchr (nd->dd_name,_T('\\')) != nd->dd_name
 
92
                                             + _tcslen (nd->dd_name) - 1)
 
93
    {
 
94
      _tcscat (nd->dd_name,SLASH);
 
95
    }
 
96
 
 
97
  _tcscat (nd->dd_name,SUFFIX);
 
98
 
 
99
  nd->dd_handle = -1;
 
100
 
 
101
  nd->dd_stat = 0;
 
102
 
 
103
  nd->dd_dir.d_ino = 0;
 
104
  nd->dd_dir.d_reclen = 0;
 
105
  nd->dd_dir.d_namlen = 0;
 
106
  memset (nd->dd_dir.d_name,0,FILENAME_MAX);
 
107
 
 
108
  return nd;
 
109
}
 
110
 
 
111
struct _tdirent *
 
112
_treaddir (_TDIR * dirp)
 
113
{
 
114
  errno = 0;
 
115
 
 
116
  if (!dirp)
 
117
    {
 
118
      errno = EFAULT;
 
119
      return (struct _tdirent *) 0;
 
120
    }
 
121
 
 
122
  if (dirp->dd_stat < 0)
 
123
    {
 
124
 
 
125
      return (struct _tdirent *) 0;
 
126
    }
 
127
  else if (dirp->dd_stat == 0)
 
128
    {
 
129
 
 
130
      dirp->dd_handle = _tfindfirst (dirp->dd_name,&(dirp->dd_dta));
 
131
 
 
132
      if (dirp->dd_handle == -1)
 
133
        {
 
134
 
 
135
          dirp->dd_stat = -1;
 
136
        }
 
137
      else
 
138
        {
 
139
          dirp->dd_stat = 1;
 
140
        }
 
141
    }
 
142
  else
 
143
    {
 
144
 
 
145
      if (_tfindnext (dirp->dd_handle,&(dirp->dd_dta)))
 
146
        {
 
147
 
 
148
          DWORD winerr = GetLastError ();
 
149
          if (winerr == ERROR_NO_MORE_FILES)
 
150
            errno = 0;
 
151
          _findclose (dirp->dd_handle);
 
152
          dirp->dd_handle = -1;
 
153
          dirp->dd_stat = -1;
 
154
        }
 
155
      else
 
156
        {
 
157
 
 
158
          dirp->dd_stat++;
 
159
        }
 
160
    }
 
161
 
 
162
  if (dirp->dd_stat > 0)
 
163
    {
 
164
 
 
165
      dirp->dd_dir.d_namlen = _tcslen (dirp->dd_dta.name);
 
166
      _tcscpy (dirp->dd_dir.d_name,dirp->dd_dta.name);
 
167
      return &dirp->dd_dir;
 
168
    }
 
169
 
 
170
  return (struct _tdirent *) 0;
 
171
}
 
172
 
 
173
int
 
174
_tclosedir (_TDIR * dirp)
 
175
{
 
176
  int rc;
 
177
 
 
178
  errno = 0;
 
179
  rc = 0;
 
180
 
 
181
  if (!dirp)
 
182
    {
 
183
      errno = EFAULT;
 
184
      return -1;
 
185
    }
 
186
 
 
187
  if (dirp->dd_handle != -1)
 
188
    {
 
189
      rc = _findclose (dirp->dd_handle);
 
190
    }
 
191
 
 
192
  free (dirp);
 
193
 
 
194
  return rc;
 
195
}
 
196
 
 
197
void
 
198
_trewinddir (_TDIR * dirp)
 
199
{
 
200
  errno = 0;
 
201
 
 
202
  if (!dirp)
 
203
    {
 
204
      errno = EFAULT;
 
205
      return;
 
206
    }
 
207
 
 
208
  if (dirp->dd_handle != -1)
 
209
    {
 
210
      _findclose (dirp->dd_handle);
 
211
    }
 
212
 
 
213
  dirp->dd_handle = -1;
 
214
  dirp->dd_stat = 0;
 
215
}
 
216
 
 
217
long
 
218
_ttelldir (_TDIR * dirp)
 
219
{
 
220
  errno = 0;
 
221
 
 
222
  if (!dirp)
 
223
    {
 
224
      errno = EFAULT;
 
225
      return -1;
 
226
    }
 
227
  return dirp->dd_stat;
 
228
}
 
229
 
 
230
void
 
231
_tseekdir (_TDIR * dirp,long lPos)
 
232
{
 
233
  errno = 0;
 
234
 
 
235
  if (!dirp)
 
236
    {
 
237
      errno = EFAULT;
 
238
      return;
 
239
    }
 
240
 
 
241
  if (lPos < -1)
 
242
    {
 
243
 
 
244
      errno = EINVAL;
 
245
      return;
 
246
    }
 
247
  else if (lPos == -1)
 
248
    {
 
249
 
 
250
      if (dirp->dd_handle != -1)
 
251
        {
 
252
          _findclose (dirp->dd_handle);
 
253
        }
 
254
      dirp->dd_handle = -1;
 
255
      dirp->dd_stat = -1;
 
256
    }
 
257
  else
 
258
    {
 
259
 
 
260
      _trewinddir (dirp);
 
261
 
 
262
      while ((dirp->dd_stat < lPos) && _treaddir (dirp));
 
263
    }
 
264
}