xref: /titanic_51/usr/src/lib/libast/common/comp/statvfs.c (revision a31148363f598def767ac48c5d82e1572e44b935)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 #include <ast.h>
25 #include <ls.h>
26 
27 #if _lib_statvfs
28 
29 NoN(statvfs)
30 
31 #else
32 
33 #include <error.h>
34 
35 #define HUH	(-1)
36 
37 #if _lib_statfs && _mem_f_files_statfs && ( _sys_statfs || _sys_vfs || _sys_mount )
38 
39 #if _sys_statfs
40 #include <sys/statfs.h>
41 #else
42 #if _sys_vfs
43 #include <sys/vfs.h>
44 #else
45 #if _sys_mount
46 #if _lib_getmntinfo
47 #include <sys/param.h>		/* expect some macro redefinitions here */
48 #endif
49 #include <sys/mount.h>
50 #endif
51 #endif
52 #endif
53 
54 #if _lib_statfs4
55 #define FSTATFS(a,b)	fstatfs(a,b,sizeof(struct statfs),0)
56 #define STATFS(a,b)	statfs(a,b,sizeof(struct statfs),0)
57 #else
58 #define FSTATFS(a,b)	fstatfs(a,b)
59 #define STATFS(a,b)	statfs(a,b)
60 #endif
61 
62 #if defined(__EXPORT__)
63 #define extern	__EXPORT__
64 #endif
65 
66 static void
67 us2v(register struct statfs* ufs, register struct stat* st, register struct statvfs* vfs)
68 {
69 	memset(vfs, 0, sizeof(*vfs));
70 	vfs->f_bsize = vfs->f_frsize = ufs->f_bsize;
71 	vfs->f_blocks = ufs->f_blocks;
72 	vfs->f_bfree = ufs->f_bfree;
73 	vfs->f_bavail =
74 #if _mem_f_bavail_statfs
75 		ufs->f_bavail;
76 #else
77 		ufs->f_bfree;
78 #endif
79 	vfs->f_files = ufs->f_files;
80 	vfs->f_ffree = ufs->f_ffree;
81 	vfs->f_favail = (ufs->f_ffree > 10) ? (ufs->f_ffree - 10) : 0;
82 	vfs->f_fsid = st->st_dev;
83 	strncpy(vfs->f_basetype, FS_default, sizeof(vfs->f_basetype) - 1);
84 	vfs->f_namemax = 14;
85 	strncpy(vfs->f_fstr, vfs->f_basetype, sizeof(vfs->f_fstr) - 1);
86 }
87 
88 extern int
89 fstatvfs(int fd, struct statvfs* vfs)
90 {
91 	struct statfs	ufs;
92 	struct stat	st;
93 
94 	if (FSTATFS(fd, &ufs) || fstat(fd, &st))
95 		return(-1);
96 	us2v(&ufs, &st, vfs);
97 	return(0);
98 }
99 
100 extern int
101 statvfs(const char* path, struct statvfs* vfs)
102 {
103 	struct statfs	ufs;
104 	struct stat	st;
105 
106 	if (STATFS(path, &ufs) || stat(path, &st))
107 		return(-1);
108 	us2v(&ufs, &st, vfs);
109 	return(0);
110 }
111 
112 #else
113 
114 #if defined(__EXPORT__)
115 #define extern	__EXPORT__
116 #endif
117 
118 static void
119 s2v(register struct stat* st, register struct statvfs* vfs)
120 {
121 	memset(vfs, 0, sizeof(*vfs));
122 	vfs->f_bsize = vfs->f_frsize =
123 #if _mem_st_blksize_stat
124 		st->st_blksize;
125 #else
126 		512;
127 #endif
128 	vfs->f_blocks = HUH;
129 	vfs->f_bfree = HUH;
130 	vfs->f_files = HUH;
131 	vfs->f_ffree = HUH;
132 	vfs->f_favail = HUH;
133 	vfs->f_fsid = st->st_dev;
134 	strncpy(vfs->f_basetype, FS_default, sizeof(vfs->f_basetype) - 1);
135 	vfs->f_namemax = 14;
136 	strncpy(vfs->f_fstr, vfs->f_basetype, sizeof(vfs->f_fstr) - 1);
137 }
138 
139 extern int
140 fstatvfs(int fd, struct statvfs* vfs)
141 {
142 	struct stat	st;
143 
144 	if (fstat(fd, &st))
145 		return(-1);
146 	s2v(&st, vfs);
147 	return(0);
148 }
149 
150 extern int
151 statvfs(const char* path, struct statvfs* vfs)
152 {
153 	struct stat	st;
154 
155 	if (stat(path, &st))
156 		return(-1);
157 	s2v(&st, vfs);
158 	return(0);
159 }
160 
161 #endif
162 
163 #endif
164