/*-1* gloadavg.c - get load average for Linux2* Copyright (C) 1993 Thomas Koenig3*4* SPDX-License-Identifier: BSD-2-Clause5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. The name of the author(s) may not be used to endorse or promote12* products derived from this software without specific prior written13* permission.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT20* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,21* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY22* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF24* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/2627#ifndef __FreeBSD__28#define _POSIX_SOURCE 12930/* System Headers */3132#include <stdio.h>33#else34#include <stdlib.h>35#endif3637/* Local headers */3839#include "gloadavg.h"4041/* Global functions */4243void perr(const char *fmt, ...);4445double46gloadavg(void)47/* return the current load average as a floating point number, or <0 for48* error49*/50{51double result;52#ifndef __FreeBSD__53FILE *fp;5455if((fp=fopen(PROC_DIR "loadavg","r")) == NULL)56result = -1.0;57else58{59if(fscanf(fp,"%lf",&result) != 1)60result = -1.0;61fclose(fp);62}63#else64if (getloadavg(&result, 1) != 1)65perr("error in getloadavg");66#endif67return result;68}697071