xref: /original-bsd/lib/libc/gen/getmntinfo.c (revision c8089215)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)getmntinfo.c	6.4 (Berkeley) 02/23/91";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <sys/mount.h>
14 #include <stdlib.h>
15 
16 /*
17  * Return information about mounted filesystems.
18  */
19 int
20 getmntinfo(mntbufp, flags)
21 	struct statfs **mntbufp;
22 	int flags;
23 {
24 	static struct statfs *mntbuf;
25 	static int mntsize;
26 	static long bufsize;
27 
28 	if (mntsize <= 0 && (mntsize = getfsstat(0, 0, MNT_NOWAIT)) < 0)
29 		return (0);
30 	if (bufsize > 0 && (mntsize = getfsstat(mntbuf, bufsize, flags)) < 0)
31 		return (0);
32 	while (bufsize <= mntsize * sizeof(struct statfs)) {
33 		if (mntbuf)
34 			free(mntbuf);
35 		bufsize = (mntsize + 1) * sizeof(struct statfs);
36 		if ((mntbuf = (struct statfs *)malloc(bufsize)) == 0)
37 			return (0);
38 		if ((mntsize = getfsstat(mntbuf, bufsize, flags)) < 0)
39 			return (0);
40 	}
41 	*mntbufp = mntbuf;
42 	return (mntsize);
43 }
44