1262566Sdes/* $Id: bsd-statvfs.c,v 1.2 2014/01/17 07:10:59 dtucker Exp $ */
2180750Sdes
3180750Sdes/*
4262566Sdes * Copyright (c) 2008,2014 Darren Tucker <dtucker@zip.com.au>
5180750Sdes *
6180750Sdes * Permission to use, copy, modify, and distribute this software for any
7180750Sdes * purpose with or without fee is hereby granted, provided that the above
8180750Sdes * copyright notice and this permission notice appear in all copies.
9180750Sdes *
10180750Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11180750Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12180750Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13180750Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14180750Sdes * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15180750Sdes * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16180750Sdes * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17180750Sdes */
18180750Sdes
19180750Sdes#include "includes.h"
20180750Sdes
21262566Sdes#if !defined(HAVE_STATVFS) || !defined(HAVE_FSTATVFS)
22262566Sdes
23262566Sdes#include <sys/param.h>
24262566Sdes#ifdef HAVE_SYS_MOUNT_H
25262566Sdes# include <sys/mount.h>
26262566Sdes#endif
27262566Sdes
28180750Sdes#include <errno.h>
29180750Sdes
30262566Sdesstatic void
31262566Sdescopy_statfs_to_statvfs(struct statvfs *to, struct statfs *from)
32262566Sdes{
33262566Sdes	to->f_bsize = from->f_bsize;
34262566Sdes	to->f_frsize = from->f_bsize;	/* no exact equivalent */
35262566Sdes	to->f_blocks = from->f_blocks;
36262566Sdes	to->f_bfree = from->f_bfree;
37262566Sdes	to->f_bavail = from->f_bavail;
38262566Sdes	to->f_files = from->f_files;
39262566Sdes	to->f_ffree = from->f_ffree;
40262566Sdes	to->f_favail = from->f_ffree;	/* no exact equivalent */
41262566Sdes	to->f_fsid = 0;			/* XXX fix me */
42262566Sdes	to->f_flag = from->f_flags;
43262566Sdes	to->f_namemax = MNAMELEN;
44262566Sdes}
45262566Sdes
46262566Sdes# ifndef HAVE_STATVFS
47180750Sdesint statvfs(const char *path, struct statvfs *buf)
48180750Sdes{
49262566Sdes#  ifdef HAVE_STATFS
50262566Sdes	struct statfs fs;
51262566Sdes
52262566Sdes	memset(&fs, 0, sizeof(fs));
53262566Sdes	if (statfs(path, &fs) == -1)
54262566Sdes		return -1;
55262566Sdes	copy_statfs_to_statvfs(buf, &fs);
56262566Sdes	return 0;
57262566Sdes#  else
58180750Sdes	errno = ENOSYS;
59180750Sdes	return -1;
60262566Sdes#  endif
61180750Sdes}
62262566Sdes# endif
63180750Sdes
64262566Sdes# ifndef HAVE_FSTATVFS
65180750Sdesint fstatvfs(int fd, struct statvfs *buf)
66180750Sdes{
67262566Sdes#  ifdef HAVE_FSTATFS
68262566Sdes	struct statfs fs;
69262566Sdes
70262566Sdes	memset(&fs, 0, sizeof(fs));
71262566Sdes	if (fstatfs(fd, &fs) == -1)
72262566Sdes		return -1;
73262566Sdes	copy_statfs_to_statvfs(buf, &fs);
74262566Sdes	return 0;
75262566Sdes#  else
76180750Sdes	errno = ENOSYS;
77180750Sdes	return -1;
78262566Sdes#  endif
79180750Sdes}
80262566Sdes# endif
81262566Sdes
82180750Sdes#endif
83