Skip to content

Commit

Permalink
Fix a compilation error with uClibc
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGarfield committed Jan 6, 2025
1 parent 53c9698 commit c01f289
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/sysctl.h>
#endif
Expand Down Expand Up @@ -96,3 +98,53 @@ pid_t get_pid_max(void)
#error "Platform not supported"
#endif
}

#if defined(__linux__) && defined(__UCLIBC__)
int getloadavg(double *loadavg, int nelem)
{
FILE *fp;
char buffer[65], *ptr;
int i;

if (nelem < 0)
{
return -1;
}
else if (nelem == 0)
{
return 0;
}
else if (nelem > 3)
{
nelem = 3;
}

if ((fp = fopen("/proc/loadavg", "r")) == NULL)
{
return -1;
}

if (fgets(buffer, sizeof(buffer), fp) == NULL)
{
fclose(fp);
return -1;
}
fclose(fp);

ptr = buffer;

for (i = 0; i < nelem; i++)
{
char *endptr;
errno = 0;
loadavg[i] = strtod(ptr, &endptr);
if (errno != 0 || ptr == endptr)
{
return -1;
}
ptr = endptr;
}

return nelem;
}
#endif
15 changes: 15 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,19 @@ int get_ncpu(void);
*/
pid_t get_pid_max(void);

#if defined(__linux__) && defined(__UCLIBC__)
/**
* Retrieves up to nelem load averages for system processes over the
* last 1, 5, and 15 minutes.
*
* @param loadavg Pointer to an array for storing the load averages.
* It must have enough space for nelem samples.
* @param nelem Number of samples to retrieve (1 to 3).
*
* @return The number of samples retrieved, or -1 if the load
* average could not be obtained.
*/
int getloadavg(double *loadavg, int nelem);
#endif

#endif

0 comments on commit c01f289

Please sign in to comment.