vm: fix a null dereference on out-of-memory
[minix.git] / lib / libc / gen / minix / getloadavg.c
blob33c2c46db55521629ba26933c91e6ca2af68d7d0
1 #include <sys/cdefs.h>
2 #include "namespace.h"
4 #include <sys/types.h>
5 #include <paths.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <lib.h>
11 #ifdef __weak_alias
12 __weak_alias(getloadavg, _getloadavg)
13 #endif
15 /* Retrieve system load average information. */
16 int getloadavg(double *loadavg, int nelem)
18 FILE *fp;
19 int i;
21 if(nelem < 1) {
22 errno = ENOSPC;
23 return -1;
26 if((fp = fopen(_PATH_PROC "loadavg", "r")) == NULL)
27 return -1;
29 for(i = 0; i < nelem; i++)
30 if(fscanf(fp, "%lf", &loadavg[i]) != 1)
31 break;
33 fclose(fp);
35 if (i == 0) {
36 errno = ENOENT;
37 return -1;
40 return i;