summaryrefslogtreecommitdiff
path: root/ace/OS_Dirent.inl
blob: 0ffc2dcfe7f20232f9aa516e7edfb9cf9c0975c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* -*- C++ -*- */
// $Id$

#if defined (ACE_HAS_PACE)
#include /**/ "pace/dirent.h"
#endif /* ACE_HAS_PACE */
ACE_INLINE DIR *
ACE_OS_Dirent::opendir (const ACE_TCHAR *filename)
{
#if defined (ACE_HAS_PACE) && !defined (ACE_WIN32)
  return pace_opendir (filename);
#elif defined (ACE_HAS_DIRENT)
#  if defined (ACE_PSOS)

  // The pointer to the DIR buffer must be passed to ACE_OS_Dirent::closedir
  // in order to free it and avoid a memory leak.
  DIR *dir;
  u_long result;
  ACE_NEW_RETURN (dir, DIR, 0);
#    if defined (ACE_PSOS_DIAB_PPC)
  result = ::open_dir (ACE_const_cast (char *, filename), &(dir->xdir));
#    else
  result = ::open_dir (ACE_const_cast (char *, filename), dir);
#    endif /* defined ACE_PSOS_DIAB_PPC */
  if (result == 0)
    return dir;
  else
    {
      errno = result;
      return 0;
    }
#  else /* ! defined (ACE_PSOS) */
#    if defined (ACE_WIN32)
  return ::ACE_OS_Dirent::opendir_emulation (filename);
#    else /* ACE_WIN32 */
  // VxWorks' ::opendir () is declared with a non-const argument.
  return ::opendir (ACE_const_cast (char *, filename));
#    endif /* ACE_WIN32 */
#  endif /* ! defined (ACE_PSOS) */
#else
  ACE_UNUSED_ARG (filename);
  ACE_NOTSUP_RETURN (0);
#endif /* ACE_HAS_PACE */
}

ACE_INLINE void
ACE_OS_Dirent::closedir (DIR *d)
{
#if defined (ACE_HAS_PACE) && !defined (ACE_WIN32)
  pace_closedir (d);
#elif defined (ACE_HAS_DIRENT)
# if defined (ACE_PSOS)

  u_long result;

#   if defined (ACE_PSOS_DIAB_PPC)
  result = ::close_dir (&(d->xdir));
#   else
  result = ::close_dir (d);
#   endif /* defined ACE_PSOS_DIAB_PPC */

  delete d;
  if (result != 0)
    errno = result;

# else /* ! defined (ACE_PSOS) */

#   if defined (ACE_WIN32)
  ACE_OS_Dirent::closedir_emulation (d);
  delete d->directory_name_;
  delete d;
#   else /* ACE_WIN32 */
  ::closedir (d);
#   endif /* ACE_WIN32 */

# endif /* !ACE_PSOS */
#else /* ACE_HAS_DIRENT */
  ACE_UNUSED_ARG (d);
#endif /* ACE_HAS_PACE */
}

ACE_INLINE struct dirent *
ACE_OS_Dirent::readdir (DIR *d)
{
#if defined (ACE_HAS_PACE) && !defined (ACE_WIN32)
  return pace_readdir (d);
#elif defined (ACE_HAS_DIRENT)
#  if defined (ACE_PSOS)
  // The returned pointer to the dirent struct must be deleted by the
  // caller to avoid a memory leak.
  struct dirent *dir_ent;
  u_long result;

  ACE_NEW_RETURN (dir_ent,
                  dirent,
                  0);
#if defined (ACE_PSOS_DIAB_PPC)
  result = ::read_dir (&(d->xdir), dir_ent);
#else
  result = ::read_dir (d, dir_ent);
#endif /* defined ACE_PSOS_DIAB_PPC) */

  if (0 == result)
    return dir_ent;
  else
    {
      errno = result;
      return 0;
    }

#  else /* ! defined (ACE_PSOS) */
#    if defined (ACE_WIN32)
  return ACE_OS_Dirent::readdir_emulation (d);
#    else /* defined (ACE_WIN32) */
  return ::readdir (d);
#    endif /* defined (ACE_WIN32) */
#  endif /* ! defined (ACE_PSOS) */
#else
  ACE_UNUSED_ARG (d);
  ACE_NOTSUP_RETURN (0);
#endif /* ACE_HAS_PACE */
}

ACE_INLINE int
ACE_OS_Dirent::readdir_r (DIR *dirp,
                   struct dirent *entry,
                   struct dirent **result)
{
#if defined (ACE_HAS_PACE) && !defined (ACE_WIN32)
  return pace_readdir_r (dirp, entry, result);
# elif !defined (ACE_HAS_REENTRANT_FUNCTIONS)
  ACE_UNUSED_ARG (entry);
  // <result> has better not be 0!
  *result = ACE_OS_Dirent::readdir (dirp);
  return 0;
# elif defined (ACE_HAS_DIRENT)  &&  !defined (ACE_LACKS_READDIR_R)
# if (defined (sun) && (defined (_POSIX_PTHREAD_SEMANTICS) || \
                        (_FILE_OFFSET_BITS == 64))) || \
      (!defined (sun) && (defined (ACE_HAS_PTHREADS_STD) || \
                         defined (ACE_HAS_PTHREADS_DRAFT7) || \
                         defined (__USE_POSIX) || \
                         defined (HPUX_11)))
# if defined (__GNUG__) && defined (DIGITAL_UNIX)
  return readdir_r (dirp, entry, result);
# else
  return ::readdir_r (dirp, entry, result);
# endif /* defined (__GNUG__) && defined (DIGITAL_UNIX) */
# else  /* ! POSIX.1c - this is draft 4 or draft 6 */
#   if defined (HPUX_10)   /* But HP 10.x doesn't follow the draft either */
    *result = entry;
    return ::readdir_r (dirp, entry);
#   else
    // <result> had better not be 0!
    *result = ::readdir_r (dirp, entry);
    return 0;
#   endif /* HPUX_10 */
# endif /* ! POSIX.1c */
#else  /* ! ACE_HAS_DIRENT  ||  ACE_LACKS_READDIR_R */
  ACE_UNUSED_ARG (dirp);
  ACE_UNUSED_ARG (entry);
  ACE_UNUSED_ARG (result);
  ACE_NOTSUP_RETURN (0);

#endif /* ACE_HAS_PACE */
}

ACE_INLINE long
ACE_OS_Dirent::telldir (DIR *d)
{
#if defined (ACE_HAS_DIRENT)  &&  !defined (ACE_LACKS_TELLDIR)
  return ::telldir (d);
#else  /* ! ACE_HAS_DIRENT  ||  ACE_LACKS_TELLDIR */
  ACE_UNUSED_ARG (d);
  ACE_NOTSUP_RETURN (-1);
#endif /* ! ACE_HAS_DIRENT  ||  ACE_LACKS_TELLDIR */
}

ACE_INLINE void
ACE_OS_Dirent::seekdir (DIR *d, long loc)
{
#if defined (ACE_HAS_DIRENT)  &&  !defined (ACE_LACKS_SEEKDIR)
  ::seekdir (d, loc);
#else  /* ! ACE_HAS_DIRENT  ||  ACE_LACKS_SEEKDIR */
  ACE_UNUSED_ARG (d);
  ACE_UNUSED_ARG (loc);
#endif /* ! ACE_HAS_DIRENT  ||  ACE_LACKS_SEEKDIR */
}

ACE_INLINE void
ACE_OS_Dirent::rewinddir (DIR *d)
{
#if defined (ACE_HAS_PACE) && !defined (ACE_WIN32)
  pace_rewinddir (d);
#elif defined (ACE_HAS_DIRENT)
# if defined (ACE_LACKS_SEEKDIR)
#  if defined (ACE_LACKS_REWINDDIR)
  ACE_UNUSED_ARG (d);
#  else /* ! defined (ACE_LACKS_REWINDDIR) */
   ::rewinddir (d);
#  endif /* ! defined (ACE_LACKS_REWINDDIR) */
# else  /* ! ACE_LACKS_SEEKDIR */
    // We need to implement <rewinddir> using <seekdir> since it's often
    // defined as a macro...
   ::seekdir (d, long (0));
# endif /* ! ACE_LACKS_SEEKDIR */
#else
  ACE_UNUSED_ARG (d);
#endif /* ACE_HAS_DIRENT */
}