Just a little correction at the it.po file.
[midnight-commander.git] / src / fsusage.c
blobe90bdfe678df74ed1a074d88ceb0b6d046313e3b
1 /* fsusage.c -- return space usage of mounted filesystems
2 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
22 #include <sys/types.h>
24 #ifdef HAVE_SYS_PARAM_H
25 #include <sys/param.h>
26 #endif
28 #ifdef HAVE_SYS_MOUNT_H
29 #include <sys/mount.h>
30 #endif
32 #ifdef HAVE_SYS_VFS_H
33 #include <sys/vfs.h>
34 #endif
36 #ifdef HAVE_SYS_FILSYS_H
37 #include <sys/filsys.h> /* SVR2. */
38 #endif
40 #include <stdlib.h>
42 #ifdef HAVE_SYS_STATFS_H
43 #include <sys/statfs.h>
44 #endif
46 #ifdef HAVE_DUSTAT_H /* AIX PS/2. */
47 #include <sys/dustat.h>
48 #endif
50 #ifdef HAVE_SYS_STATVFS_H /* SVR4. */
51 #include <sys/statvfs.h>
52 #endif
54 #include "global.h"
55 #include "fsusage.h"
58 #ifdef HAVE_INFOMOUNT
60 /* Return the number of TOSIZE-byte blocks used by
61 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
62 TOSIZE must be positive. Return -1 if FROMSIZE is not positive. */
64 static long
65 fs_adjust_blocks (long blocks, int fromsize, int tosize)
67 if (tosize <= 0)
68 abort ();
69 if (fromsize <= 0)
70 return -1;
72 if (fromsize == tosize) /* E.g., from 512 to 512. */
73 return blocks;
74 else if (fromsize > tosize) /* E.g., from 2048 to 512. */
75 return blocks * (fromsize / tosize);
76 else /* E.g., from 256 to 512. */
77 return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize);
80 #if defined(_AIX) && defined(_I386)
81 /* AIX PS/2 does not supply statfs. */
82 static int aix_statfs (char *path, struct statfs *fsb)
84 struct stat stats;
85 struct dustat fsd;
87 if (stat (path, &stats))
88 return -1;
89 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
90 return -1;
91 fsb->f_type = 0;
92 fsb->f_bsize = fsd.du_bsize;
93 fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
94 fsb->f_bfree = fsd.du_tfree;
95 fsb->f_bavail = fsd.du_tfree;
96 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
97 fsb->f_ffree = fsd.du_tinode;
98 fsb->f_fsid.val[0] = fsd.du_site;
99 fsb->f_fsid.val[1] = fsd.du_pckno;
100 return 0;
102 #define statfs(path,fsb) aix_statfs(path,fsb)
103 #endif /* _AIX && _I386 */
105 /* Fill in the fields of FSP with information about space usage for
106 the filesystem on which PATH resides.
107 Return 0 if successful, -1 if not. */
109 int get_fs_usage (char *path, struct fs_usage *fsp)
111 #ifdef STAT_STATFS3_OSF1
112 struct statfs fsd;
114 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
115 return -1;
116 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
117 #endif /* STAT_STATFS3_OSF1 */
119 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
120 struct fs_data fsd;
122 if (statfs (path, &fsd) != 1)
123 return -1;
124 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), 1024, 512)
125 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
126 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
127 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
128 fsp->fsu_files = fsd.fd_req.gtot;
129 fsp->fsu_ffree = fsd.fd_req.gfree;
130 #endif
132 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX. */
133 struct statfs fsd;
135 if (statfs (path, &fsd) < 0)
136 return -1;
137 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
138 #endif
140 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
141 struct statfs fsd;
143 if (statfs (path, &fsd) < 0)
144 return -1;
145 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
146 #endif
148 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX. */
149 struct statfs fsd;
151 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
152 return -1;
153 /* Empirically, the block counts on most SVR3 and SVR3-derived
154 systems seem to always be in terms of 512-byte blocks,
155 no matter what value f_bsize has. */
156 #if _AIX
157 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
158 #else
159 #define CONVERT_BLOCKS(b) (b)
160 #ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */
161 #ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
162 #define f_bavail f_bfree
163 #endif
164 #endif
165 #endif
166 #endif
168 #ifdef STAT_STATVFS /* SVR4. */
169 struct statvfs fsd;
171 if (statvfs (path, &fsd) < 0)
172 return -1;
173 /* f_frsize isn't guaranteed to be supported. */
174 #define CONVERT_BLOCKS(b) \
175 fs_adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
176 #endif
178 #if defined(CONVERT_BLOCKS) && !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2. */
179 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
180 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
181 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
182 fsp->fsu_files = fsd.f_files;
183 fsp->fsu_ffree = fsd.f_ffree;
184 #endif
186 return 0;
189 #endif /* HAVE_INFOMOUNT */