bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: trimming getloadavg's dependencies


From: Paul Eggert
Subject: Re: trimming getloadavg's dependencies
Date: Mon, 07 Feb 2011 01:42:47 -0800
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101208 Thunderbird/3.1.7

On 02/06/2011 11:54 PM, Jim Meyering wrote:
> Please do.  There is so little precision in load average
> numbers (and probably never an exponent), that nothing would be lost.

OK, here's a first cut, which I have not tested or pushed
but am circulating for kneejerk reactions if you have the time.

This brings the new files (from Emacs's point of view) down to:

  lib/stdint.in.h
  lib/stdlib.in.h
  lib/wchar.in.h
  m4/longlong.m4
  m4/stdint.m4
  m4/stdlib_h.m4
  m4/wchar_h.m4
  m4/wint_t.m4

which still seems a bit large but perhaps I could trim further
later.  Some of the following code runs only on (obsolescent?)
platforms that I lack easy access to so it will be hard to test.

diff --git a/modules/getloadavg b/modules/getloadavg
index 7865b8f..fcbce4f 100644
--- a/modules/getloadavg
+++ b/modules/getloadavg
@@ -6,10 +6,7 @@ lib/getloadavg.c
 m4/getloadavg.m4
 
 Depends-on:
-c-strtod
-cloexec
 extensions
-fcntl-safer
 intprops
 stdbool
 stdlib
diff --git a/lib/getloadavg.c b/lib/getloadavg.c
index 18a5960..3b4f527 100644
--- a/lib/getloadavg.c
+++ b/lib/getloadavg.c
@@ -108,8 +108,6 @@
 #  include <sys/param.h>
 # endif
 
-# include "c-strtod.h"
-# include "cloexec.h"
 # include "intprops.h"
 
 /* The existing Emacs configuration files define a macro called
@@ -371,8 +369,9 @@
 #     include <nlist.h>
 #    endif /* NLIST_STRUCT */
 
+#    include <fcntl.h>
+
 #    ifdef SUNOS_5
-#     include <fcntl.h>
 #     include <kvm.h>
 #     include <kstat.h>
 #    endif
@@ -461,7 +460,6 @@
 #  include <sys/dg_sys_info.h>
 # endif
 
-# include "fcntl--.h"
 
 /* Avoid static vars inside a function since in HPUX they dump as pure.  */
 
@@ -618,19 +616,28 @@ getloadavg (double loadavg[], int nelem)
 
   for (elem = 0; elem < nelem; elem++)
     {
-      char *endptr;
-      double d;
+      double numerator = 0;
+      double denominator = 1;
+
+      while (*ptr == ' ')
+        ptr++;
 
-      errno = 0;
-      d = c_strtod (ptr, &endptr);
-      if (ptr == endptr || (d == 0 && errno != 0))
+      if (! ('0' <= *ptr && *ptr <= '9'))
         {
           if (elem == 0)
             return -1;
           break;
         }
-      loadavg[elem] = d;
-      ptr = endptr;
+
+      do
+        numerator = 10 * numerator + (*ptr++ - '0');
+      while ('0' <= *ptr && *ptr <= '9');
+
+      if (*ptr == '.')
+        for (ptr++; '0' <= *ptr && *ptr <= '9'; ptr++)
+          numerator = 10 * numerator + (*ptr - '0'), denominator *= 10;
+
+      loadavg[elem++] = numerator / denominator;
     }
 
   return elem;
@@ -940,13 +947,27 @@ getloadavg (double loadavg[], int nelem)
   if (!getloadavg_initialized)
     {
 #  ifndef SUNOS_5
-      channel = open ("/dev/kmem", O_RDONLY);
-      if (channel >= 0)
+      /* Set the channel to close on exec, so it does not
+         litter any child's descriptor table.  */
+#   ifndef O_CLOEXEC
+#    define O_CLOEXEC 0
+#   endif
+      int fd = open ("/dev/kmem", O_RDONLY | O_CLOEXEC);
+      if (0 <= fd)
         {
-          /* Set the channel to close on exec, so it does not
-             litter any child's descriptor table.  */
-          set_cloexec_flag (channel, true);
-          getloadavg_initialized = true;
+          if (fd <= STDERR_FILENO)
+            {
+#   ifdef F_DUPFD_CLOEXEC
+              int fd1 = fcntl (fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
+              close (fd);
+              fd = fd1;
+#   endif
+            }
+          if (0 <= fd)
+            {
+              channel = fd;
+              getloadavg_initialized = true;
+            }
         }
 #  else /* SUNOS_5 */
       /* We pass 0 for the kernel, corefile, and swapfile names



reply via email to

[Prev in Thread] Current Thread [Next in Thread]