diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2020-06-27 09:44:11 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2020-06-27 09:56:00 -0700 |
commit | 7eeb7bff6efdd659aa33517dc743f2203d9af35a (patch) | |
tree | a5b9ab0c59bc29485e0e8ba1ca45fc9a37515383 /lib/getloadavg.c | |
parent | 7bd236ff938d10ca3da225cb18badea91aadeb91 (diff) | |
download | gnulib-7eeb7bff6efdd659aa33517dc743f2203d9af35a.tar.gz |
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.
Diffstat (limited to 'lib/getloadavg.c')
-rw-r--r-- | lib/getloadavg.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/lib/getloadavg.c b/lib/getloadavg.c index aeb7070cc7..468e250670 100644 --- a/lib/getloadavg.c +++ b/lib/getloadavg.c @@ -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; |