xref: /freebsd/crypto/openssh/openbsd-compat/bsd-statvfs.c (revision 09a53ad8f1318c5daae6cfb19d97f4f6459f0013)
1 /* $Id: bsd-statvfs.c,v 1.2 2014/01/17 07:10:59 dtucker Exp $ */
2 
3 /*
4  * Copyright (c) 2008,2014 Darren Tucker <dtucker@zip.com.au>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "includes.h"
20 
21 #if !defined(HAVE_STATVFS) || !defined(HAVE_FSTATVFS)
22 
23 #include <sys/param.h>
24 #ifdef HAVE_SYS_MOUNT_H
25 # include <sys/mount.h>
26 #endif
27 
28 #include <errno.h>
29 
30 static void
31 copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from)
32 {
33 	to->f_bsize = from->f_bsize;
34 	to->f_frsize = from->f_bsize;	/* no exact equivalent */
35 	to->f_blocks = from->f_blocks;
36 	to->f_bfree = from->f_bfree;
37 	to->f_bavail = from->f_bavail;
38 	to->f_files = from->f_files;
39 	to->f_ffree = from->f_ffree;
40 	to->f_favail = from->f_ffree;	/* no exact equivalent */
41 	to->f_fsid = 0;			/* XXX fix me */
42 	to->f_flag = from->f_flags;
43 	to->f_namemax = MNAMELEN;
44 }
45 
46 # ifndef HAVE_STATVFS
47 int statvfs(const char *path, struct statvfs *buf)
48 {
49 #  ifdef HAVE_STATFS
50 	struct statfs fs;
51 
52 	memset(&fs, 0, sizeof(fs));
53 	if (statfs(path, &fs) == -1)
54 		return -1;
55 	copy_statfs_to_statvfs(buf, &fs);
56 	return 0;
57 #  else
58 	errno = ENOSYS;
59 	return -1;
60 #  endif
61 }
62 # endif
63 
64 # ifndef HAVE_FSTATVFS
65 int fstatvfs(int fd, struct statvfs *buf)
66 {
67 #  ifdef HAVE_FSTATFS
68 	struct statfs fs;
69 
70 	memset(&fs, 0, sizeof(fs));
71 	if (fstatfs(fd, &fs) == -1)
72 		return -1;
73 	copy_statfs_to_statvfs(buf, &fs);
74 	return 0;
75 #  else
76 	errno = ENOSYS;
77 	return -1;
78 #  endif
79 }
80 # endif
81 
82 #endif
83