1 /* $OpenBSD: ffs_df.c,v 1.10 2004/09/14 22:46:04 deraadt Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
41 #endif /* not lint */
42
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
47
48 #include <unistd.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <fcntl.h>
52
53 int ffs_df(int, char *, struct statfs *);
54
55 extern int bread(int, off_t, void *, int);
56 extern char *getmntpt(char *);
57
58 static union {
59 struct fs iu_fs;
60 char dummy[SBSIZE];
61 } sb;
62 #define sblock sb.iu_fs
63
64 int
ffs_df(int rfd,char * file,struct statfs * sfsp)65 ffs_df(int rfd, char *file, struct statfs *sfsp)
66 {
67 char *mntpt;
68
69 if (bread(rfd, (off_t)SBOFF, &sblock, SBSIZE) == 0) {
70 return (-1);
71 }
72 if (sblock.fs_magic != FS_MAGIC) {
73 return (-1);
74 }
75 sfsp->f_flags = 0;
76 sfsp->f_bsize = sblock.fs_fsize;
77 sfsp->f_iosize = sblock.fs_bsize;
78 sfsp->f_blocks = sblock.fs_dsize;
79 sfsp->f_bfree = sblock.fs_cstotal.cs_nbfree * sblock.fs_frag +
80 sblock.fs_cstotal.cs_nffree;
81 sfsp->f_bavail = ((int64_t)sblock.fs_dsize * (100 -
82 sblock.fs_minfree) / 100) - (sblock.fs_dsize - sfsp->f_bfree);
83 sfsp->f_files = sblock.fs_ncg * sblock.fs_ipg - ROOTINO;
84 sfsp->f_ffree = sblock.fs_cstotal.cs_nifree;
85 sfsp->f_fsid.val[0] = 0;
86 sfsp->f_fsid.val[1] = 0;
87 if ((mntpt = getmntpt(file)) == 0)
88 mntpt = "";
89 memmove(&sfsp->f_mntonname[0], mntpt, MNAMELEN);
90 memmove(&sfsp->f_mntfromname[0], file, MNAMELEN);
91 strlcpy(sfsp->f_fstypename, MOUNT_FFS, MFSNAMELEN);
92 return (0);
93 }
94