public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
From: Sebastian Huber <sh@sourceware.org>
To: newlib-cvs@sourceware.org
Subject: [newlib-cygwin] Open a directory with the usual flags
Date: Thu, 11 Oct 2018 08:22:00 -0000	[thread overview]
Message-ID: <20181011082228.98189.qmail@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=61fc64ed975d0c47582c23e5ce47a4af51c66733

commit 61fc64ed975d0c47582c23e5ce47a4af51c66733
Author: Sebastian Huber <sebastian.huber@embedded-brains.de>
Date:   Mon Oct 8 08:56:09 2018 +0200

    Open a directory with the usual flags
    
    Use O_RDONLY since you are not supposed to write to a directory.
    
    Use O_DIRECTORY as mandated by POSIX (The Open Group Base Specifications
    Issue 7, 2018 edition IEEE Std 1003.1-2017):
    
    "If the type DIR is implemented using a file descriptor, the descriptor
    shall be obtained as if the O_DIRECTORY flag was passed to open()."
    
    Use O_CLOEXEC as mandated by POSIX:
    
    "When a file descriptor is used to implement the directory stream, it
    behaves as if the FD_CLOEXEC had been set for the file descriptor."
    
    Drop the fcntl() call in favour of O_CLOEXEC.
    
    Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>

Diff:
---
 newlib/libc/posix/opendir.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/newlib/libc/posix/opendir.c b/newlib/libc/posix/opendir.c
index 1416f10..650cbfe 100644
--- a/newlib/libc/posix/opendir.c
+++ b/newlib/libc/posix/opendir.c
@@ -49,17 +49,12 @@ static char sccsid[] = "@(#)opendir.c	5.11 (Berkeley) 2/23/91";
 DIR *
 opendir (const char *name)
 {
-	register DIR *dirp;
-	register int fd;
-	int rc = 0;
+	DIR *dirp;
+	int fd;
 
-	if ((fd = open(name, 0)) == -1)
+	if ((fd = open(name, O_RDONLY | O_DIRECTORY | O_CLOEXEC)) == -1)
 		return NULL;
-#ifdef HAVE_FCNTL
-	rc = fcntl(fd, F_SETFD, 1);
-#endif
-	if (rc == -1 ||
-	    (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
+	if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
 		close (fd);
 		return NULL;
 	}


                 reply	other threads:[~2018-10-11  8:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181011082228.98189.qmail@sourceware.org \
    --to=sh@sourceware.org \
    --cc=newlib-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).