1157841Sflz/*-
298186Sgordon *  gloadavg.c - get load average for Linux
378344Sobrien *  Copyright (C) 1993  Thomas Koenig
4157473Sflz *
578344Sobrien * SPDX-License-Identifier: BSD-2-Clause
678344Sobrien *
778344Sobrien * Redistribution and use in source and binary forms, with or without
878344Sobrien * modification, are permitted provided that the following conditions
978344Sobrien * are met:
1078344Sobrien * 1. Redistributions of source code must retain the above copyright
1178344Sobrien *    notice, this list of conditions and the following disclaimer.
1278344Sobrien * 2. The name of the author(s) may not be used to endorse or promote
1378344Sobrien *    products derived from this software without specific prior written
1478344Sobrien *    permission.
1578344Sobrien *
1678344Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1778344Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1878344Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1978344Sobrien * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
2078344Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2178344Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2278344Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2378344Sobrien * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2478344Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2578344Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2678344Sobrien */
2778344Sobrien
2878344Sobrien#ifndef __FreeBSD__
2978344Sobrien#define _POSIX_SOURCE 1
3078344Sobrien
3178344Sobrien/* System Headers */
3278344Sobrien
3378344Sobrien#include <stdio.h>
3478344Sobrien#else
3578344Sobrien#include <stdlib.h>
3678344Sobrien#endif
3778344Sobrien
3878344Sobrien/* Local headers */
3978344Sobrien
4078344Sobrien#include "gloadavg.h"
4178344Sobrien
42157473Sflz/* Global functions */
43157473Sflz
4478344Sobrienvoid perr(const char *fmt, ...);
4598186Sgordon
4698186Sgordondouble
4798186Sgordongloadavg(void)
48131550Scperciva/* return the current load average as a floating point number, or <0 for
49131550Scperciva * error
50131550Scperciva */
51131550Scperciva{
5298186Sgordon    double result;
5398186Sgordon#ifndef __FreeBSD__
5498186Sgordon    FILE *fp;
55103018Sgordon
56124832Smtm    if((fp=fopen(PROC_DIR "loadavg","r")) == NULL)
57157710Sflz	result = -1.0;
58124832Smtm    else
5998186Sgordon    {
60103018Sgordon	if(fscanf(fp,"%lf",&result) != 1)
6198186Sgordon	    result = -1.0;
6298186Sgordon	fclose(fp);
6398186Sgordon    }
6498186Sgordon#else
6598186Sgordon    if (getloadavg(&result, 1) != 1)
6698186Sgordon	    perr("error in getloadavg");
6798186Sgordon#endif
6898186Sgordon    return result;
6998186Sgordon}
7078344Sobrien