[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-gnulib] Re: Bug in getloadavg
From: |
Paul Eggert |
Subject: |
Re: [Bug-gnulib] Re: Bug in getloadavg |
Date: |
Tue, 30 Mar 2004 11:32:07 -0800 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) |
Bruno Haible <address@hidden> writes:
> Since setlocale can return NULL (or, at least, the replacement stub for
> absent setlocale is NULL) and xstrdup (NULL) is invalid, I would rewrite
> this
Thanks for catching that: I installed the fix.
> I have a problem with functions returning 'bool' and errno. Namely,
> the calling convention is just the opposite of so many Unix
> functions, like close(), shmdt() etc.
It's a valid point.
I do prefer 'bool' for functions that don't set errno on failure.
And once one takes this approach, it's a short distance to having
functions return bool for every function that returns a failure
indication, regardless of what other things the function does.
However, there's a good consistency argument that functions that are
"like" Unix system calls (in particular, functions that return a
failure flag and set errno on failure) should use the Unix convention,
so perhaps I went overboard here.
Jim Meyering <address@hidden> writes:
> I sometimes have to re-read code that does this:
> if (stat (...)) { /* handle failure */ ... }
Yes, and I prefer this:
if (stat (...) != 0) { /* handle failure */ ... }
particularly if some routines return bool for failure, as it makes it
clearer that 'stat' uses the Unix convention.
Anyway, here's a proposed patch to gnulib. If it's OK with people
I'll send a separate patch for this for coreutils, since coreutils
needs some other changes in this area too (for the Emacs changes, and
to accommodate the change to cloexec.h).
2004-03-30 Paul Eggert <address@hidden>
* lib/cloexec.h, lib/cloexec.c (set_cloexec_flag): Return int
not bool, to be more consistent with Unix conventions.
Suggested by Bruno Haible.
Index: lib/cloexec.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/cloexec.h,v
retrieving revision 1.1
diff -p -u -r1.1 cloexec.h
--- lib/cloexec.h 29 Mar 2004 23:57:36 -0000 1.1
+++ lib/cloexec.h 30 Mar 2004 19:26:51 -0000
@@ -1,2 +1,2 @@
#include <stdbool.h>
-bool set_cloexec_flag (int desc, bool value);
+int set_cloexec_flag (int desc, bool value);
Index: lib/cloexec.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/cloexec.c,v
retrieving revision 1.1
diff -p -u -r1.1 cloexec.c
--- lib/cloexec.c 29 Mar 2004 23:57:36 -0000 1.1
+++ lib/cloexec.c 30 Mar 2004 19:26:51 -0000
@@ -37,27 +37,29 @@
/* 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. */
+ Return 0 on success, or -1 on error with `errno' set. */
-bool
+int
set_cloexec_flag (int desc, bool value)
{
#if defined F_GETFD && defined F_SETFD
int flags = fcntl (desc, F_GETFD, 0);
- int newflags;
- if (flags < 0)
- return false;
+ if (0 <= flags)
+ {
+ int newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
+
+ if (flags == newflags
+ || fcntl (desc, F_SETFD, newflags) != -1)
+ return 0;
+ }
- newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
-
- return (flags == newflags
- || fcntl (desc, F_SETFD, newflags) != -1);
+ return -1;
#else
- return true;
+ return 0;
#endif
}