Imported Upstream version 2.7
[ossec-hids.git] / src / sysinfo / statfs.c
1 #include <stdio.h>
2 #include <sys/param.h>
3 #include <sys/types.h>
4 #include <sys/mount.h>
5
6 #ifdef __linux__
7    #include <mntent.h>
8    #include <sys/vfs.h>
9 #endif
10
11 #include "statfs.h"
12
13
14 int getstatfspath()
15    {
16    /* For OpenBSD */
17    #ifdef __OpenBSD__
18      int mntsize=0;
19      int i=0;
20      struct statfs *fs;
21      mntsize = getmntinfo(&fs, MNT_NOWAIT);
22      if(mntsize == 0)
23       {
24       return(-1);
25       }
26
27      for(i=0;i<mntsize;i++)
28       getstatfs(fs[i].f_mntonname);
29
30     /* For Linux */
31    #elif defined( __linux__ )
32      struct  mntent *m;
33      FILE *f;
34      f = setmntent("/etc/mtab", "r");
35      while ((m = getmntent(f)))
36         getstatfs(m->mnt_dir);
37      endmntent(f);
38    #endif
39
40    return(0);
41    }
42
43
44
45 int getstatfs(char *path)
46 {
47     struct statfs fs;
48     int percentbfree=0;
49     int percentnfree=0;
50
51     if(statfs(path, &fs) != 0)
52         return(-1);
53
54     if((fs.f_bfree == 0)||(fs.f_ffree == 0))
55             return(-1);
56     percentbfree = (int)(100*fs.f_bfree)/fs.f_blocks;
57     percentnfree = (int)(100*fs.f_ffree)/fs.f_files;
58     printf("file system for %s has %d free blocks out of a total of %d - %d. Total of %d%% FREE \n",path,fs.f_ffree,fs.f_blocks,percentbfree,percentnfree);
59    return(0);
60 }