Mercurial > hg > octave-kai > gnulib-hg
annotate lib/fsusage.c @ 439:a9015d648b2e
No longer include safe-l?stat.h.
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Fri, 19 May 1995 15:23:18 +0000 |
parents | defe7888e2f3 |
children | 3b80433a4e28 |
rev | line source |
---|---|
5 | 1 /* fsusage.c -- return space usage of mounted filesystems |
2 Copyright (C) 1991, 1992 Free Software Foundation, Inc. | |
3 | |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2, or (at your option) | |
7 any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program; if not, write to the Free Software | |
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
17 | |
105 | 18 #ifdef HAVE_CONFIG_H |
19 #include <config.h> | |
20 #endif | |
21 | |
5 | 22 #include <sys/types.h> |
338 | 23 #include <sys/stat.h> |
5 | 24 #include "fsusage.h" |
25 | |
26 int statfs (); | |
27 | |
323 | 28 #ifdef HAVE_SYS_PARAM_H |
29 #include <sys/param.h> | |
76 | 30 #endif |
31 | |
323 | 32 #ifdef HAVE_SYS_MOUNT_H |
5 | 33 #include <sys/mount.h> |
34 #endif | |
35 | |
323 | 36 #ifdef HAVE_SYS_VFS_H |
37 #include <sys/vfs.h> | |
5 | 38 #endif |
39 | |
323 | 40 #ifdef HAVE_SYS_FILSYS_H |
41 #include <sys/filsys.h> /* SVR2. */ | |
42 #endif | |
43 | |
44 #ifdef HAVE_FCNTL_H | |
5 | 45 #include <fcntl.h> |
46 #endif | |
47 | |
323 | 48 #ifdef HAVE_SYS_STATFS_H |
5 | 49 #include <sys/statfs.h> |
50 #endif | |
51 | |
323 | 52 #ifdef HAVE_DUSTAT_H /* AIX PS/2. */ |
5 | 53 #include <sys/dustat.h> |
54 #endif | |
55 | |
323 | 56 #ifdef HAVE_SYS_STATVFS_H /* SVR4. */ |
5 | 57 #include <sys/statvfs.h> |
58 int statvfs (); | |
59 #endif | |
60 | |
385
f4cb6bbc01d4
(get_fs_usage, statfs): Use safe_read, and safe_stat rather than
Jim Meyering <jim@meyering.net>
parents:
339
diff
changeset
|
61 int safe_read (); |
f4cb6bbc01d4
(get_fs_usage, statfs): Use safe_read, and safe_stat rather than
Jim Meyering <jim@meyering.net>
parents:
339
diff
changeset
|
62 |
197
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
63 /* Return the number of TOSIZE-byte blocks used by |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
64 BLOCKS FROMSIZE-byte blocks, rounding away from zero. |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
65 TOSIZE must be positive. Return -1 if FROMSIZE is not positive. */ |
5 | 66 |
197
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
67 static long |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
68 adjust_blocks (blocks, fromsize, tosize) |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
69 long blocks; |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
70 int fromsize, tosize; |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
71 { |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
72 if (tosize <= 0) |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
73 abort (); |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
74 if (fromsize <= 0) |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
75 return -1; |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
76 |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
77 if (fromsize == tosize) /* E.g., from 512 to 512. */ |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
78 return blocks; |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
79 else if (fromsize > tosize) /* E.g., from 2048 to 512. */ |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
80 return blocks * (fromsize / tosize); |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
81 else /* E.g., from 256 to 512. */ |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
82 return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize); |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
83 } |
5 | 84 |
85 /* Fill in the fields of FSP with information about space usage for | |
86 the filesystem on which PATH resides. | |
87 DISK is the device on which PATH is mounted, for space-getting | |
88 methods that need to know it. | |
89 Return 0 if successful, -1 if not. */ | |
90 | |
91 int | |
92 get_fs_usage (path, disk, fsp) | |
93 char *path, *disk; | |
94 struct fs_usage *fsp; | |
95 { | |
120 | 96 #if defined (STAT_STATFS3_OSF1) |
76 | 97 struct statfs fsd; |
98 | |
99 if (statfs (path, &fsd, sizeof (struct statfs)) != 0) | |
196 | 100 return -1; |
197
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
101 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512) |
120 | 102 #endif /* STAT_STATFS3_OSF1 */ |
76 | 103 |
5 | 104 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */ |
105 struct fs_data fsd; | |
106 | |
107 if (statfs (path, &fsd) != 1) | |
108 return -1; | |
197
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
109 #define CONVERT_BLOCKS(b) adjust_blocks ((b), 1024, 512) |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
110 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot); |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
111 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree); |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
112 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen); |
5 | 113 fsp->fsu_files = fsd.fd_req.gtot; |
114 fsp->fsu_ffree = fsd.fd_req.gfree; | |
115 #endif | |
116 | |
323 | 117 #ifdef STAT_READ_FILSYS /* SVR2. */ |
5 | 118 #ifndef SUPERBOFF |
119 #define SUPERBOFF (SUPERB * 512) | |
120 #endif | |
121 struct filsys fsd; | |
122 int fd; | |
123 | |
124 fd = open (disk, O_RDONLY); | |
125 if (fd < 0) | |
126 return -1; | |
127 lseek (fd, (long) SUPERBOFF, 0); | |
385
f4cb6bbc01d4
(get_fs_usage, statfs): Use safe_read, and safe_stat rather than
Jim Meyering <jim@meyering.net>
parents:
339
diff
changeset
|
128 if (safe_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd) |
5 | 129 { |
130 close (fd); | |
131 return -1; | |
132 } | |
133 close (fd); | |
197
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
134 #define CONVERT_BLOCKS(b) adjust_blocks ((b), (fsd.s_type == Fs2b ? 1024 : 512), 512) |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
135 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.s_fsize); |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
136 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.s_tfree); |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
137 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.s_tfree); |
5 | 138 fsp->fsu_files = (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1); |
139 fsp->fsu_ffree = fsd.s_tinode; | |
140 #endif | |
141 | |
142 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX. */ | |
143 struct statfs fsd; | |
144 | |
145 if (statfs (path, &fsd) < 0) | |
146 return -1; | |
197
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
147 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512) |
5 | 148 #endif |
149 | |
150 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */ | |
151 struct statfs fsd; | |
152 | |
153 if (statfs (path, &fsd) < 0) | |
154 return -1; | |
197
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
155 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512) |
5 | 156 #endif |
157 | |
339 | 158 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX. */ |
5 | 159 struct statfs fsd; |
160 | |
161 if (statfs (path, &fsd, sizeof fsd, 0) < 0) | |
162 return -1; | |
163 /* Empirically, the block counts on most SVR3 and SVR3-derived | |
164 systems seem to always be in terms of 512-byte blocks, | |
165 no matter what value f_bsize has. */ | |
339 | 166 # if _AIX |
167 # define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512) | |
168 # else | |
169 # define CONVERT_BLOCKS(b) (b) | |
170 # ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */ | |
171 # ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */ | |
172 # define f_bavail f_bfree | |
173 # endif | |
174 # endif | |
175 # endif | |
85 | 176 #endif |
5 | 177 |
325 | 178 #ifdef STAT_STATVFS /* SVR4. */ |
5 | 179 struct statvfs fsd; |
180 | |
181 if (statvfs (path, &fsd) < 0) | |
182 return -1; | |
183 /* f_frsize isn't guaranteed to be supported. */ | |
197
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
184 #define CONVERT_BLOCKS(b) \ |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
185 adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512) |
5 | 186 #endif |
187 | |
323 | 188 #if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2. */ |
197
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
189 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks); |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
190 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree); |
6658e575c60c
(adjust_blocks): Code it as a function again.
Jim Meyering <jim@meyering.net>
parents:
196
diff
changeset
|
191 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail); |
5 | 192 fsp->fsu_files = fsd.f_files; |
193 fsp->fsu_ffree = fsd.f_ffree; | |
194 #endif | |
195 | |
196 return 0; | |
197 } | |
198 | |
199 #if defined(_AIX) && defined(_I386) | |
200 /* AIX PS/2 does not supply statfs. */ | |
201 | |
202 int | |
203 statfs (path, fsb) | |
204 char *path; | |
205 struct statfs *fsb; | |
206 { | |
207 struct stat stats; | |
208 struct dustat fsd; | |
209 | |
426
defe7888e2f3
(statfs): Use stat, not safe_stat.
Jim Meyering <jim@meyering.net>
parents:
385
diff
changeset
|
210 if (stat (path, &stats)) |
5 | 211 return -1; |
212 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd))) | |
213 return -1; | |
214 fsb->f_type = 0; | |
215 fsb->f_bsize = fsd.du_bsize; | |
216 fsb->f_blocks = fsd.du_fsize - fsd.du_isize; | |
217 fsb->f_bfree = fsd.du_tfree; | |
218 fsb->f_bavail = fsd.du_tfree; | |
219 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb; | |
220 fsb->f_ffree = fsd.du_tinode; | |
221 fsb->f_fsid.val[0] = fsd.du_site; | |
222 fsb->f_fsid.val[1] = fsd.du_pckno; | |
223 return 0; | |
224 } | |
225 #endif /* _AIX && _I386 */ |