summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/comp/statvfs.c
blob: 2da0f59778b692918dd10584725eebd55e9d1574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2009 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
#pragma prototyped

#include <ast.h>
#include <ls.h>

#if _lib_statvfs

NoN(statvfs)

#else

#include <error.h>

#define HUH	(-1)

#if _lib_statfs && _mem_f_files_statfs && ( _sys_statfs || _sys_vfs || _sys_mount )

#if _sys_statfs
#include <sys/statfs.h>
#else
#if _sys_vfs
#include <sys/vfs.h>
#else
#if _sys_mount
#if _lib_getmntinfo
#include <sys/param.h>		/* expect some macro redefinitions here */
#endif
#include <sys/mount.h>
#endif
#endif
#endif

#if _lib_statfs4
#define FSTATFS(a,b)	fstatfs(a,b,sizeof(struct statfs),0)
#define STATFS(a,b)	statfs(a,b,sizeof(struct statfs),0)
#else
#define FSTATFS(a,b)	fstatfs(a,b)
#define STATFS(a,b)	statfs(a,b)
#endif

#if defined(__EXPORT__)
#define extern	__EXPORT__
#endif

static void
us2v(register struct statfs* ufs, register struct stat* st, register struct statvfs* vfs)
{
	memset(vfs, 0, sizeof(*vfs));
	vfs->f_bsize = vfs->f_frsize = ufs->f_bsize;
	vfs->f_blocks = ufs->f_blocks;
	vfs->f_bfree = ufs->f_bfree;
	vfs->f_bavail =
#if _mem_f_bavail_statfs
		ufs->f_bavail;
#else
		ufs->f_bfree;
#endif
	vfs->f_files = ufs->f_files;
	vfs->f_ffree = ufs->f_ffree;
	vfs->f_favail = (ufs->f_ffree > 10) ? (ufs->f_ffree - 10) : 0;
	vfs->f_fsid = st->st_dev;
	strncpy(vfs->f_basetype, FS_default, sizeof(vfs->f_basetype) - 1);
	vfs->f_namemax = 14;
	strncpy(vfs->f_fstr, vfs->f_basetype, sizeof(vfs->f_fstr) - 1);
}

extern int
fstatvfs(int fd, struct statvfs* vfs)
{
	struct statfs	ufs;
	struct stat	st;

	if (FSTATFS(fd, &ufs) || fstat(fd, &st))
		return(-1);
	us2v(&ufs, &st, vfs);
	return(0);
}

extern int
statvfs(const char* path, struct statvfs* vfs)
{
	struct statfs	ufs;
	struct stat	st;

	if (STATFS(path, &ufs) || stat(path, &st))
		return(-1);
	us2v(&ufs, &st, vfs);
	return(0);
}

#else

#if defined(__EXPORT__)
#define extern	__EXPORT__
#endif

static void
s2v(register struct stat* st, register struct statvfs* vfs)
{
	memset(vfs, 0, sizeof(*vfs));
	vfs->f_bsize = vfs->f_frsize =
#if _mem_st_blksize_stat
		st->st_blksize;
#else
		512;
#endif
	vfs->f_blocks = HUH;
	vfs->f_bfree = HUH;
	vfs->f_files = HUH;
	vfs->f_ffree = HUH;
	vfs->f_favail = HUH;
	vfs->f_fsid = st->st_dev;
	strncpy(vfs->f_basetype, FS_default, sizeof(vfs->f_basetype) - 1);
	vfs->f_namemax = 14;
	strncpy(vfs->f_fstr, vfs->f_basetype, sizeof(vfs->f_fstr) - 1);
}

extern int
fstatvfs(int fd, struct statvfs* vfs)
{
	struct stat	st;

	if (fstat(fd, &st))
		return(-1);
	s2v(&st, vfs);
	return(0);
}

extern int
statvfs(const char* path, struct statvfs* vfs)
{
	struct stat	st;

	if (stat(path, &st))
		return(-1);
	s2v(&st, vfs);
	return(0);
}

#endif

#endif