From: Jim Meyering Date: Thu, 4 Mar 2004 08:42:20 +0000 (+0000) Subject: Include "cloexec.h" first, and before . X-Git-Tag: v5.2.1~62 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a1c30fd7d14b21443adf9ed9525474723a25f7e4;p=thirdparty%2Fcoreutils.git Include "cloexec.h" first, and before . (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. --- diff --git a/lib/cloexec.c b/lib/cloexec.c index 46a0d71512..7714a160e8 100644 --- a/lib/cloexec.c +++ b/lib/cloexec.c @@ -21,47 +21,43 @@ # include #endif -#if HAVE_FCNTL_H -# include -#endif +#include "cloexec.h" #if HAVE_UNISTD_H # include #endif -#include "cloexec.h" +#if HAVE_FCNTL_H +# include +#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 }