]> Savannah Git Hosting - gnulib.git/commitdiff
getloadavg: don’t depend on fopen-gnu
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 27 Jun 2020 16:44:11 +0000 (09:44 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 27 Jun 2020 16:56:00 +0000 (09:56 -0700)
This is for Emacs, which does not need fopen-gnu for anything else,
and which would need it only on a NetBSD platform where getloadavg
does not work (does that even happen?).
* lib/getloadavg.c (getloadavg) [__NetBSD__]: Use open, not fopen.
* modules/getloadavg (Depends-on): Remove fopen-gnu.

ChangeLog
lib/getloadavg.c
modules/getloadavg

index cd194e0c4dfbc617e22b662611c3692c1212c443..19e609e31c6915b2c1ba105918d09dc26fcbfce1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2020-06-27  Paul Eggert  <eggert@cs.ucla.edu>
 
+       getloadavg: don’t depend on fopen-gnu
+       This is for Emacs, which does not need fopen-gnu for anything else,
+       and which would need it only on a NetBSD platform where getloadavg
+       does not work (does that even happen?).
+       * lib/getloadavg.c (getloadavg) [__NetBSD__]: Use open, not fopen.
+       * modules/getloadavg (Depends-on): Remove fopen-gnu.
+
        * tests/test-getloadavg.c (main): Fix typo.
 
 2020-06-27  Bruno Haible  <bruno@clisp.org>
index aeb7070cc7953b62ba8f90456e2ec0e5c2e4bdee..468e25067099fdc64301765424a60db3d984af89 100644 (file)
@@ -567,15 +567,22 @@ getloadavg (double loadavg[], int nelem)
 
   unsigned long int load_ave[3], scale;
   int count;
-  FILE *fp;
-
-  fp = fopen (NETBSD_LDAV_FILE, "re");
-  if (fp == NULL)
-    return -1;
-  count = fscanf (fp, "%lu %lu %lu %lu\n",
+  char readbuf[4 * INT_BUFSIZE_BOUND (unsigned long int) + 1];
+  int fd = open (NETBSD_LDAV_FILE, O_RDONLY | O_CLOEXEC);
+  if (fd < 0)
+    return fd;
+  int nread = read (fd, readbuf, sizeof readbuf - 1);
+  int err = errno;
+  close (fd);
+  if (nread < 0)
+    {
+      errno = err;
+      return -1;
+    }
+  readbuf[nread] = '\0';
+  count = sscanf (readbuf, "%lu %lu %lu %lu\n",
                   &load_ave[0], &load_ave[1], &load_ave[2],
                   &scale);
-  (void) fclose (fp);
   if (count != 4)
     {
       errno = ENOTSUP;
index a5a3c4e03cf45b08f67adb8b18e25cf2b243dc72..8adb9a78487f7d2ba7473c443703cf7ba1f2bc4f 100644 (file)
@@ -7,7 +7,6 @@ m4/getloadavg.m4
 
 Depends-on:
 extensions
-fopen-gnu
 intprops
 open
 stdbool