* file named AUTHORS, in the root directory of this distribution.
*/
/*
- * $Id: statfs.c,v 1.13 2005/09/30 19:13:27 martinea Exp $
+ * $Id: statfs.c,v 1.16 2006/08/24 17:05:35 martinea Exp $
*
* a generic statfs-like routine
*/
# define STATFS_FAVAIL(buf) (buf).f_ffree
# define STATFS_FFREE(buf) (buf).f_ffree
# define STATFS_SCALE(buf) (buf).f_bsize
-# define STATFS(path, buffer) statfs(path, &buffer, sizeof(STATFS_STRUCT), 0)
+# define STATFS(path, buffer) statfs(path, &buffer, SIZEOF(STATFS_STRUCT), 0)
# else
# if HAVE_SYS_MOUNT_H
/*
# define STATFS_SCALE(buf) (buf).f_bsize
# define STATFS(path, buffer) statfs(path, &buffer)
# ifdef STATFS_OSF1
-# define STATFS(path, buffer) statfs(path, &buffer, sizeof(STATFS_STRUCT))
+# define STATFS(path, buffer) statfs(path, &buffer, SIZEOF(STATFS_STRUCT))
# endif
# endif
# endif
# endif
#endif
-#define scale(r,s) ( (r) == -1? -1 : (int)((r)*(double)(s)/1024.0) )
-int get_fs_stats(dir, sp)
-char *dir;
-generic_fs_stats_t *sp;
+off_t scale(off_t r, off_t s);
+
+off_t
+scale(
+ off_t r,
+ off_t s)
+{
+ if (r == (off_t)-1)
+ return (off_t)-1;
+ if (s % 1024 == 0)
+ return r*(s/(off_t)1024);
+ return (off_t)(r*((double)s/1024.0));
+}
+
+int
+get_fs_stats(
+ char * dir,
+ generic_fs_stats_t *sp)
{
STATFS_STRUCT statbuf;
/* total, avail, free: converted to kbytes, rounded down */
- sp->total = scale(STATFS_TOTAL(statbuf), STATFS_SCALE(statbuf));
- sp->avail = scale(STATFS_AVAIL(statbuf), STATFS_SCALE(statbuf));
- sp->free = scale(STATFS_FREE(statbuf), STATFS_SCALE(statbuf));
+ sp->total = scale((off_t)STATFS_TOTAL(statbuf),
+ (off_t)STATFS_SCALE(statbuf));
+ sp->avail = scale((off_t)STATFS_AVAIL(statbuf),
+ (off_t)STATFS_SCALE(statbuf));
+ sp->free = scale((off_t)STATFS_FREE(statbuf),
+ (off_t)STATFS_SCALE(statbuf));
/* inode stats */
- sp->files = STATFS_FILES(statbuf);
- sp->favail = STATFS_FAVAIL(statbuf);
- sp->ffree = STATFS_FFREE(statbuf);
+ sp->files = (off_t)STATFS_FILES(statbuf);
+ sp->favail = (off_t)STATFS_FAVAIL(statbuf);
+ sp->ffree = (off_t)STATFS_FFREE(statbuf);
return 0;
}
#ifdef TEST
/* ----- test scaffolding ----- */
-int main(argc, argv)
-int argc;
-char **argv;
+int
+main(
+ int argc,
+ char ** argv)
{
generic_fs_stats_t statbuf;
set_pname(argv[0]);
+ dbopen(NULL);
+
if(argc < 2) {
fprintf(stderr, "Usage: %s files ...\n", get_pname());
return 1;
printf("statfs (%s)\n",STATFS_TYP);
printf(
-"name total free avail files ffree favail\n"
+"name total free avail files ffree favail\n"
);
printf(
-"------------------------------ ------- ------- ------- ------ ------ ------\n"
+"---------------------------- -------- -------- -------- ------- ------- -------\n"
);
do {
perror(*argv);
continue;
}
- printf("%-30.30s %7ld %7ld %7ld %6ld %6ld %6ld\n", *argv,
+ printf("%-28.28s %8ld %8ld %8ld %7ld %7ld %7ld\n", *argv,
statbuf.total, statbuf.free, statbuf.avail,
statbuf.files, statbuf.ffree, statbuf.favail);
} while(argc > 1);