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