}
}
+/* Open the directory DIR if possible, and return a file
+ descriptor. Return -1 and set errno on failure. It doesn't matter
+ whether the file descriptor has read or write access. */
+
+static int
+internal_function
+diropen (char const *dir)
+{
+ int fd = open (dir, O_RDONLY | O_DIRECTORY);
+ if (fd < 0)
+ fd = open (dir, O_WRONLY | O_DIRECTORY);
+ return fd;
+}
+
FTS *
fts_open(argv, options, compar)
char * const *argv;
* descriptor we run anyway, just more slowly.
*/
if (!ISSET(FTS_NOCHDIR)
- && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0)
+ && (sp->fts_rfd = diropen (".")) < 0)
SET(FTS_NOCHDIR);
return (sp);
(p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
p->fts_info = fts_stat(sp, p, true);
if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
- if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) {
+ if ((p->fts_symfd = diropen (".")) < 0) {
p->fts_errno = errno;
p->fts_info = FTS_ERR;
} else
if (p->fts_instr == FTS_FOLLOW) {
p->fts_info = fts_stat(sp, p, true);
if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
- if ((p->fts_symfd =
- open(".", O_RDONLY, 0)) < 0) {
+ if ((p->fts_symfd = diropen (".")) < 0) {
p->fts_errno = errno;
p->fts_info = FTS_ERR;
} else
ISSET(FTS_NOCHDIR))
return (sp->fts_child = fts_build(sp, instr));
- if ((fd = open(".", O_RDONLY, 0)) < 0)
+ if ((fd = diropen (".")) < 0)
return (sp->fts_child = NULL);
sp->fts_child = fts_build(sp, instr);
if (fchdir(fd)) {
}
/*
- * Change to dir specified by fd or p->fts_accpath without getting
+ * Change to dir specified by fd or path without getting
* tricked by someone changing the world out from underneath us.
* Assumes p->fts_dev and p->fts_ino are filled in.
*/
newfd = fd;
if (ISSET(FTS_NOCHDIR))
return (0);
- if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0)
+ if (fd < 0 && (newfd = diropen (path)) < 0)
return (-1);
if (fstat(newfd, &sb)) {
ret = -1;