/* 

        Copyright (C) 1995
        Free Software Foundation, Inc.

   This file is part of GNU cfengine - written and maintained 
   by Mark Burgess, Dept of Computing and Engineering, Oslo College,
   Dept. of Theoretical physics, University of Oslo
 
   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version. 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

*/

/*******************************************************************/
/*                                                                 */
/* Disk usage module, Demosthenes Skipitaris                       */
/*                                                                 */
/*******************************************************************/

#include "cf.defs.h"
#include "cf.extern.h"
#include "../pub/getopt.h"

#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif

#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif

#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif

#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif

GetDiskUsage (file)

char *file;

{
#if defined SOLARIS || OSF1
    struct statvfs buf;
#elif defined ULTRIX
    struct fs_data buf;
#else
    struct statfs buf;
#endif
    u_long blocksize = 1024, total = 0, used = 0, avail = 0;
    float capacity = 0.0;

    bzero (&buf, sizeof (buf));

#if defined ULTRIX
    if (getmnt (NULL, &buf, sizeof (struct fs_data), STAT_ONE, file) == -1)
       {
       printf("cfengine:%s: Couldn't get filesystem info for %s\n",VPREFIX,file);
       return cfinfinity;
       }
#elif defined SOLARIS || defined OSF1
    if (statvfs (file, &buf) != 0)
       {
       printf("cfengine:%s: Couldn't get filesystem info for %s\n",VPREFIX,file);
       return cfinfinity;
       }
#elif defined IRIX || defined CFCRAY
    if (statfs (file, &buf, sizeof (struct statfs), 0) != 0)
       {
       printf("cfengine:%s: Couldn't get filesystem info for %s\n",VPREFIX,file);
       return cfinfinity;
       }
#else
    if (statfs (file, &buf) != 0)
       {
       printf("cfengine:%s: Couldn't get filesystem info for %s\n",VPREFIX,file);
       return cfinfinity;
       }
#endif

#if defined ULTRIX
    total = buf.fd_btot;
    used = buf.fd_btot - buf.fd_bfree;
    avail = buf.fd_bfreen;
#endif

#if defined SOLARIS
    total = buf.f_blocks * (buf.f_frsize / blocksize);
    used = (buf.f_blocks - buf.f_bfree) * (buf.f_frsize / blocksize);
    avail = buf.f_bavail * (buf.f_frsize / blocksize);
#endif

#if defined NETBSD || defined LINUX || defined SUNOS || defined HPuUX
    total = buf.f_blocks;
    used = buf.f_blocks - buf.f_bfree;
    avail = buf.f_bavail;
#endif

#if defined OSF1
    total = buf.f_blocks / (blocksize / buf.f_frsize);
    used = (buf.f_blocks - buf.f_bfree) / (blocksize / buf.f_frsize);
    avail = buf.f_bavail / (blocksize / buf.f_frsize);
#endif

#if defined AIX4
    total = buf.f_blocks * (buf.f_bsize / blocksize);
    used = (buf.f_blocks - buf.f_bfree) * (buf.f_bsize / blocksize);
    avail = buf.f_bfree * (buf.f_bsize / blocksize);
#endif

#if defined CFCRAY
    total = buf.f_blocks * (buf.f_bsize / blocksize);
    used = (buf.f_blocks - buf.f_bfree) * (buf.f_bsize / blocksize);
    avail = buf.f_bfree * (buf.f_bsize / blocksize);
#endif

#if defined IRIX
    total = buf.f_blocks / (blocksize / buf.f_bsize);
    used = (buf.f_blocks - buf.f_bfree) / (blocksize / buf.f_bsize);
    avail = buf.f_bfree / (blocksize / buf.f_bsize);
#endif

#if defined CFCRAY
    capacity = (float) buf.f_bfree * 100.0 / (float) buf.f_blocks;
#else
    capacity = (float) (used) / (float) (avail + used) * 100;
#endif


Debug2("GetDiskUsage(%s)\n",file);
    
/* Free kilobytes */
    
return avail;
}
