Merge changes to getloadavg.c from coreutils and Emacs; this
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 29 Mar 2004 23:57:36 +0000 (23:57 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 29 Mar 2004 23:57:36 +0000 (23:57 +0000)
adds a new module cloexec.

ChangeLog
lib/ChangeLog
lib/cloexec.c [new file with mode: 0644]
lib/cloexec.h [new file with mode: 0644]
lib/getloadavg.c
m4/ChangeLog
m4/cloexec.m4 [new file with mode: 0644]
modules/cloexec [new file with mode: 0644]
modules/getloadavg

index 0a80d7f771adb67498919c52cb16e96a06435005..67003cb6f61161623d4cb2629adb07452ba156aa 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,8 @@
-2004-03-09  Paul Eggert  <eggert@cs.ucla.edu>
+2004-03-29  Paul Eggert  <eggert@twinsun.com>
+
+       * modules/getloadvg: Depend on cloexec and xalloc.
+
+2004-03-09  Paul Eggert  <eggert@twinsun.com>
 
        * modules/getopt: Add lib/getopt_int.h.
 
index 986440e424810de22eedca5c6c7bbe909efa06a4..76d685ee4a41d34f5a9930a340515e8b373a9cb1 100644 (file)
@@ -1,4 +1,15 @@
-2004-03-18  Paul Eggert  <eggert@cs.ucla.edu>
+2004-03-29  Paul Eggert  <eggert@twinsun.com>
+
+       Merge changes to getloadavg.c from coreutils and Emacs.
+
+       * lib/getloadavg.c [!defined HAVE_SETLOCALE] (setlocale):
+       Define to an expression, not to the empty string.
+       Include cloexec.h and xalloc.h.
+       (getloadavg): Restore LC_NUMERIC locale after setting it temporarily.
+       Use set_cloexec_flag rather than rolling our own.
+       * lib/cloexec.c, lib/cloexec.h: New files.
+
+2004-03-18  Paul Eggert  <eggert@twinsun.com>
 
        * getopt.h: Sync with libc CVS.
 
@@ -9,7 +20,7 @@
        not on all platforms that have <wchar.h>.
        * mbswidth.c: Include <stdio.h> and <time.h> before <wchar.h>.
 
-2004-03-09  Paul Eggert  <eggert@cs.ucla.edu>
+2004-03-09  Paul Eggert  <eggert@twinsun.com>
 
        * argp-parse.c, getopt.c, getopt.h, getopt1.c:
        Sync with libc CVS.
diff --git a/lib/cloexec.c b/lib/cloexec.c
new file mode 100644 (file)
index 0000000..20f30db
--- /dev/null
@@ -0,0 +1,63 @@
+/* closexec.c - set or clear the close-on-exec descriptor flag
+   Copyright (C) 1991, 2004 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+   The code is taken from glibc/manual/llio.texi  */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "cloexec.h"
+
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#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 true,
+   or clear the flag if VALUE is false.
+   Return true on success, or false on error with `errno' set. */
+
+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 (flags < 0)
+    return false;
+
+  newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
+
+  return (flags == newflags
+         || fcntl (desc, F_SETFD, newflags) != -1);
+
+#else
+
+  return true;
+
+#endif
+}
diff --git a/lib/cloexec.h b/lib/cloexec.h
new file mode 100644 (file)
index 0000000..ecad8d7
--- /dev/null
@@ -0,0 +1,2 @@
+#include <stdbool.h>
+bool set_cloexec_flag (int desc, bool value);
index 6ac0a8c5d2b16f017120279bf4e8ca13ce216b5e..dce4117f087623eae5651f3393cb8c9bf5b5e869 100644 (file)
@@ -1,7 +1,7 @@
 /* Get the system load averages.
 
    Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994,
-   1995, 1997, 1999, 2000, 2003 Free Software Foundation, Inc.
+   1995, 1997, 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
 
    NOTE: The canonical source of this file is maintained with gnulib.
    Bugs can be reported to bug-gnulib@gnu.org.
@@ -111,9 +111,12 @@ extern int errno;
 # include <locale.h>
 #endif
 #ifndef HAVE_SETLOCALE
-# define setlocale(Category, Locale) /* empty */
+# define setlocale(Category, Locale) ((char *) NULL)
 #endif
 
+#include "cloexec.h"
+#include "xalloc.h"
+
 #ifndef HAVE_GETLOADAVG
 
 /* The existing Emacs configuration files define a macro called
@@ -592,6 +595,7 @@ getloadavg (double loadavg[], int nelem)
   char ldavgbuf[40];
   double load_ave[3];
   int fd, count;
+  char *old_locale;
 
   fd = open (LINUX_LDAV_FILE, O_RDONLY);
   if (fd == -1)
@@ -602,10 +606,12 @@ getloadavg (double loadavg[], int nelem)
     return -1;
 
   /* The following sscanf must use the C locale.  */
+  old_locale = xstrdup (setlocale (LC_NUMERIC, NULL));
   setlocale (LC_NUMERIC, "C");
   count = sscanf (ldavgbuf, "%lf %lf %lf",
                  &load_ave[0], &load_ave[1], &load_ave[2]);
-  setlocale (LC_NUMERIC, "");
+  setlocale (LC_NUMERIC, old_locale);
+  free (old_locale);
   if (count < 1)
     return -1;
 
@@ -924,12 +930,7 @@ getloadavg (double loadavg[], int nelem)
        {
          /* Set the channel to close on exec, so it does not
             litter any child's descriptor table.  */
-#   ifdef F_SETFD
-#    ifndef FD_CLOEXEC
-#     define FD_CLOEXEC 1
-#    endif
-         (void) fcntl (channel, F_SETFD, FD_CLOEXEC);
-#   endif
+         set_cloexec_flag (channel, true);
          getloadavg_initialized = 1;
        }
 #  else /* SUNOS_5 */
index 8abf642befd2fd3826981b1b2051b217ea54f1fc..f1c91cfcdbb02b95699ea048ba2ba0cd093f224d 100644 (file)
@@ -1,3 +1,7 @@
+2004-03-29  Paul Eggert  <eggert@twinsun.com>
+
+       * cloexec.m4: New file.
+
 2004-03-18  Paul Eggert  <eggert@twinsun.com>
             Bruno Haible  <bruno@clisp.org>
 
diff --git a/m4/cloexec.m4 b/m4/cloexec.m4
new file mode 100644 (file)
index 0000000..06b36d5
--- /dev/null
@@ -0,0 +1,13 @@
+# cloexec.m4 serial 1
+dnl Copyright (C) 2004 Free Software Foundation, Inc.
+dnl This file is free software, distributed under the terms of the GNU
+dnl General Public License.  As a special exception to the GNU General
+dnl Public License, this file may be distributed as part of a program
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+AC_DEFUN([gl_CLOEXEC],
+[
+  dnl Prerequisites of lib/cloexec.c.
+  AC_CHECK_HEADERS_ONCE(fcntl.h unistd.h)
+])
diff --git a/modules/cloexec b/modules/cloexec
new file mode 100644 (file)
index 0000000..9dc2c1a
--- /dev/null
@@ -0,0 +1,22 @@
+Description:
+Set or clear the close-on-exec descriptor flag.
+
+Files:
+lib/cloexec.c
+lib/cloexec.h
+m4/cloexec.m4
+
+Depends-on:
+stdbool
+
+configure.ac:
+gl_CLOEXEC
+
+Makefile.am:
+lib_SOURCES += cloexec.c cloexec.h
+
+Include:
+"cloexec.h"
+
+Maintainer:
+Jim Meyering
index 3a41e33523159f0a129c4c289a3a97a3af839a79..c3fd60a1aa72e929e54d32d294c56aa4688f1ed8 100644 (file)
@@ -6,6 +6,8 @@ lib/getloadavg.c
 m4/getloadavg.m4
 
 Depends-on:
+cloexec
+xalloc
 
 configure.ac:
 gl_FUNC_GETLOADAVG