summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2004-03-04 08:42:20 +0000
committerJim Meyering <jim@meyering.net>2004-03-04 08:42:20 +0000
commita1c30fd7d14b21443adf9ed9525474723a25f7e4 (patch)
tree69fdc7175b74497c95b348d240dc5a86af59b94f /lib
parente819dd0dd9c2b76a57c78944c4835a7a3ea5ccfe (diff)
downloadcoreutils-a1c30fd7d14b21443adf9ed9525474723a25f7e4.tar.xz
Include "cloexec.h" first, and <unistd.h> before <fcntl.h>.
(set_cloexec_flag): Use bool for booleans. All uses changed. If F_GETFD returns a negative number (not just -1), report a failure. Don't use F_SETFD if the flags are already right. Don't report a failure with F_SETFD unless it returns -1.
Diffstat (limited to 'lib')
-rw-r--r--lib/cloexec.c36
1 files changed, 16 insertions, 20 deletions
diff --git a/lib/cloexec.c b/lib/cloexec.c
index 46a0d7151..7714a160e 100644
--- a/lib/cloexec.c
+++ b/lib/cloexec.c
@@ -21,47 +21,43 @@
# include <config.h>
#endif
-#if HAVE_FCNTL_H
-# include <fcntl.h>
-#endif
+#include "cloexec.h"
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
-#include "cloexec.h"
+#if HAVE_FCNTL_H
+# include <fcntl.h>
+#endif
#ifndef FD_CLOEXEC
# define FD_CLOEXEC 1
#endif
-/* Set the `FD_CLOEXEC' flag of DESC if VALUE is nonzero,
- or clear the flag if VALUE is 0.
- Return 0 on success, or -1 on error with `errno' set. */
+/* Set the `FD_CLOEXEC' flag of DESC if VALUE is true,
+ or clear the flag if VALUE is false.
+ Return true on success, or false on error with `errno' set. */
-int
-set_cloexec_flag (int desc, int value)
+bool
+set_cloexec_flag (int desc, bool value)
{
#if defined F_GETFD && defined F_SETFD
int flags = fcntl (desc, F_GETFD, 0);
+ int newflags;
- /* If reading the flags failed, return error indication. */
- if (flags == -1)
- return flags;
+ if (flags < 0)
+ return false;
- /* Set just the flag we want to set. */
- if (value != 0)
- flags |= FD_CLOEXEC;
- else
- flags &= ~FD_CLOEXEC;
+ newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
- /* Store modified flags in the descriptor. */
- return fcntl (desc, F_SETFD, flags);
+ return (flags == newflags
+ || fcntl (desc, F_SETFD, newflags) != -1);
#else
- return 0;
+ return false;
#endif
}