1598eaa6cSDarren Tucker /*
2a5cf1e22SDarren Tucker  * Copyright (c) 2008,2014 Darren Tucker <dtucker@zip.com.au>
3598eaa6cSDarren Tucker  *
4598eaa6cSDarren Tucker  * Permission to use, copy, modify, and distribute this software for any
5598eaa6cSDarren Tucker  * purpose with or without fee is hereby granted, provided that the above
6598eaa6cSDarren Tucker  * copyright notice and this permission notice appear in all copies.
7598eaa6cSDarren Tucker  *
8598eaa6cSDarren Tucker  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9598eaa6cSDarren Tucker  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10598eaa6cSDarren Tucker  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11598eaa6cSDarren Tucker  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12598eaa6cSDarren Tucker  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13598eaa6cSDarren Tucker  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14598eaa6cSDarren Tucker  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15598eaa6cSDarren Tucker  */
16598eaa6cSDarren Tucker 
17598eaa6cSDarren Tucker #include "includes.h"
18598eaa6cSDarren Tucker 
19a5cf1e22SDarren Tucker #if !defined(HAVE_STATVFS) || !defined(HAVE_FSTATVFS)
20a5cf1e22SDarren Tucker 
21a5cf1e22SDarren Tucker #include <sys/param.h>
22a5cf1e22SDarren Tucker #ifdef HAVE_SYS_MOUNT_H
23a5cf1e22SDarren Tucker # include <sys/mount.h>
24a5cf1e22SDarren Tucker #endif
25a5cf1e22SDarren Tucker 
26598eaa6cSDarren Tucker #include <errno.h>
27598eaa6cSDarren Tucker 
2811057564SDarren Tucker #ifndef MNAMELEN
2911057564SDarren Tucker # define MNAMELEN 32
3011057564SDarren Tucker #endif
3111057564SDarren Tucker 
32*d561b0b2SDarren Tucker #ifdef HAVE_STRUCT_STATFS_F_FILES
33*d561b0b2SDarren Tucker # define HAVE_STRUCT_STATFS
34*d561b0b2SDarren Tucker #endif
35*d561b0b2SDarren Tucker 
36*d561b0b2SDarren Tucker #ifdef HAVE_STRUCT_STATFS
37a5cf1e22SDarren Tucker static void
copy_statfs_to_statvfs(struct statvfs * to,struct statfs * from)38a5cf1e22SDarren Tucker copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from)
39a5cf1e22SDarren Tucker {
40a5cf1e22SDarren Tucker 	to->f_bsize = from->f_bsize;
41a5cf1e22SDarren Tucker 	to->f_frsize = from->f_bsize;	/* no exact equivalent */
42a5cf1e22SDarren Tucker 	to->f_blocks = from->f_blocks;
43a5cf1e22SDarren Tucker 	to->f_bfree = from->f_bfree;
44a5cf1e22SDarren Tucker 	to->f_bavail = from->f_bavail;
45a5cf1e22SDarren Tucker 	to->f_files = from->f_files;
46a5cf1e22SDarren Tucker 	to->f_ffree = from->f_ffree;
47a5cf1e22SDarren Tucker 	to->f_favail = from->f_ffree;	/* no exact equivalent */
48a5cf1e22SDarren Tucker 	to->f_fsid = 0;			/* XXX fix me */
4911057564SDarren Tucker #ifdef HAVE_STRUCT_STATFS_F_FLAGS
50a5cf1e22SDarren Tucker 	to->f_flag = from->f_flags;
5111057564SDarren Tucker #else
5211057564SDarren Tucker 	to->f_flag = 0;
5311057564SDarren Tucker #endif
54a5cf1e22SDarren Tucker 	to->f_namemax = MNAMELEN;
55a5cf1e22SDarren Tucker }
56*d561b0b2SDarren Tucker #endif
57a5cf1e22SDarren Tucker 
58598eaa6cSDarren Tucker # ifndef HAVE_STATVFS
statvfs(const char * path,struct statvfs * buf)59598eaa6cSDarren Tucker int statvfs(const char *path, struct statvfs *buf)
60598eaa6cSDarren Tucker {
61*d561b0b2SDarren Tucker #  if defined(HAVE_STATFS) && defined(HAVE_STRUCT_STATFS)
62a5cf1e22SDarren Tucker 	struct statfs fs;
63a5cf1e22SDarren Tucker 
64a5cf1e22SDarren Tucker 	memset(&fs, 0, sizeof(fs));
65a5cf1e22SDarren Tucker 	if (statfs(path, &fs) == -1)
66a5cf1e22SDarren Tucker 		return -1;
67a5cf1e22SDarren Tucker 	copy_statfs_to_statvfs(buf, &fs);
68a5cf1e22SDarren Tucker 	return 0;
69a5cf1e22SDarren Tucker #  else
70598eaa6cSDarren Tucker 	errno = ENOSYS;
71598eaa6cSDarren Tucker 	return -1;
72a5cf1e22SDarren Tucker #  endif
73598eaa6cSDarren Tucker }
74598eaa6cSDarren Tucker # endif
75598eaa6cSDarren Tucker 
76598eaa6cSDarren Tucker # ifndef HAVE_FSTATVFS
fstatvfs(int fd,struct statvfs * buf)77598eaa6cSDarren Tucker int fstatvfs(int fd, struct statvfs *buf)
78598eaa6cSDarren Tucker {
79*d561b0b2SDarren Tucker #  if defined(HAVE_FSTATFS) && defined(HAVE_STRUCT_STATFS)
80a5cf1e22SDarren Tucker 	struct statfs fs;
81a5cf1e22SDarren Tucker 
82a5cf1e22SDarren Tucker 	memset(&fs, 0, sizeof(fs));
83a5cf1e22SDarren Tucker 	if (fstatfs(fd, &fs) == -1)
84a5cf1e22SDarren Tucker 		return -1;
85a5cf1e22SDarren Tucker 	copy_statfs_to_statvfs(buf, &fs);
86a5cf1e22SDarren Tucker 	return 0;
87a5cf1e22SDarren Tucker #  else
88598eaa6cSDarren Tucker 	errno = ENOSYS;
89598eaa6cSDarren Tucker 	return -1;
90a5cf1e22SDarren Tucker #  endif
91598eaa6cSDarren Tucker }
92598eaa6cSDarren Tucker # endif
93a5cf1e22SDarren Tucker 
94a5cf1e22SDarren Tucker #endif
95