1 /* dir.cc: Posix directory-related routines
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
16 #define _COMPILING_NEWLIB
32 if (efault.faulted (EFAULT))
34 if (dir->__d_cookie != __DIRENT_COOKIE)
37 syscall_printf ("-1 = dirfd (%p)", dir);
40 return dir->__d_dirent->d_fd;
43 /* opendir: POSIX 5.1.2.1 */
45 opendir (const char *name)
50 fh = build_fh_name (name, NULL, PC_SYM_FOLLOW);
53 else if (fh->exists ())
69 readdir_worker (DIR *dir, dirent *de)
72 if (efault.faulted ())
75 if (dir->__d_cookie != __DIRENT_COOKIE)
77 syscall_printf ("%p = readdir (%p)", NULL, dir);
81 int res = ((fhandler_base *) dir->__fh)->readdir (dir, de);
85 if (!(dir->__flags & dirent_saw_dot))
87 strcpy (de->d_name, ".");
88 dir->__flags |= dirent_saw_dot;
92 else if (!(dir->__flags & dirent_saw_dot_dot))
94 strcpy (de->d_name, "..");
95 dir->__flags |= dirent_saw_dot_dot;
103 /* Compute d_ino by combining filename hash with the directory hash
104 (which was stored in dir->__d_dirhash when opendir was called). */
105 if (de->d_name[0] == '.')
107 if (de->d_name[1] == '\0')
109 de->d_ino = dir->__d_dirhash;
110 dir->__flags |= dirent_saw_dot;
112 else if (de->d_name[1] != '.' || de->d_name[2] != '\0')
116 dir->__flags |= dirent_saw_dot_dot;
117 char *p, up[strlen (dir->__d_dirname) + 1];
118 strcpy (up, dir->__d_dirname);
119 if (!(p = strrchr (up, '\\')))
122 if (!(p = strrchr (up, '\\')))
123 de->d_ino = hash_path_name (0, ".");
127 de->d_ino = hash_path_name (0, up);
134 __ino64_t dino = hash_path_name (dir->__d_dirhash, "\\");
135 de->d_ino = hash_path_name (dino, de->d_name);
137 de->__ino32 = de->d_ino; // for legacy applications
142 /* readdir: POSIX 5.1.2.1 */
143 extern "C" struct dirent *
146 int res = readdir_worker (dir, dir->__d_dirent);
148 return dir->__d_dirent;
155 readdir_r (DIR *dir, dirent *de, dirent **ode)
157 int res = readdir_worker (dir, de);
173 if (efault.faulted (EFAULT))
176 if (dir->__d_cookie != __DIRENT_COOKIE)
178 return ((fhandler_base *) dir->__fh)->telldir (dir);
185 return telldir64 (dir);
189 seekdir64 (DIR *dir, _off64_t loc)
192 if (efault.faulted (EFAULT))
195 if (dir->__d_cookie != __DIRENT_COOKIE)
197 dir->__flags &= dirent_isroot;
198 return ((fhandler_base *) dir->__fh)->seekdir (dir, loc);
203 seekdir (DIR *dir, _off_t loc)
205 seekdir64 (dir, (_off64_t)loc);
208 /* rewinddir: POSIX 5.1.2.1 */
213 if (efault.faulted (EFAULT))
216 if (dir->__d_cookie != __DIRENT_COOKIE)
218 dir->__flags &= dirent_isroot;
219 return ((fhandler_base *) dir->__fh)->rewinddir (dir);
222 /* closedir: POSIX 5.1.2.1 */
227 if (efault.faulted (EFAULT))
230 if (dir->__d_cookie != __DIRENT_COOKIE)
233 syscall_printf ("-1 = closedir (%p)", dir);
237 /* Reset the marker in case the caller tries to use `dir' again. */
240 int res = ((fhandler_base *) dir->__fh)->closedir (dir);
242 cygheap->fdtab.release (dir->__d_dirent->d_fd);
244 free (dir->__d_dirname);
245 free (dir->__d_dirent);
247 syscall_printf ("%d = closedir (%p)", res);
251 /* mkdir: POSIX 5.4.1.1 */
253 mkdir (const char *dir, mode_t mode)
256 fhandler_base *fh = NULL;
259 if (efault.faulted (EFAULT))
262 if (has_dot_last_component (dir))
268 if (!(fh = build_fh_name (dir, NULL, PC_SYM_NOFOLLOW)))
269 goto done; /* errno already set */;
273 debug_printf ("got %d error from build_fh_name", fh->error ());
274 set_errno (fh->error ());
276 else if (!fh->mkdir (mode))
281 syscall_printf ("%d = mkdir (%s, %d)", res, dir, mode);
285 /* rmdir: POSIX 5.5.2.1 */
287 rmdir (const char *dir)
290 fhandler_base *fh = NULL;
293 if (efault.faulted (EFAULT))
296 if (has_dot_last_component (dir))
302 if (!(fh = build_fh_name (dir, NULL, PC_SYM_NOFOLLOW)))
303 goto done; /* errno already set */;
307 debug_printf ("got %d error from build_fh_name", fh->error ());
308 set_errno (fh->error ());
310 else if (!fh->rmdir ())
315 syscall_printf ("%d = rmdir (%s)", res, dir);