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