getmntinfo.c revision 302408
1200583Srdivacky/*
2200583Srdivacky * Copyright (c) 1989, 1993
3200583Srdivacky *	The Regents of the University of California.  All rights reserved.
4200583Srdivacky *
5200583Srdivacky * Redistribution and use in source and binary forms, with or without
6200583Srdivacky * modification, are permitted provided that the following conditions
7200583Srdivacky * are met:
8200583Srdivacky * 1. Redistributions of source code must retain the above copyright
9200583Srdivacky *    notice, this list of conditions and the following disclaimer.
10200583Srdivacky * 2. Redistributions in binary form must reproduce the above copyright
11200583Srdivacky *    notice, this list of conditions and the following disclaimer in the
12200583Srdivacky *    documentation and/or other materials provided with the distribution.
13200583Srdivacky * 4. Neither the name of the University nor the names of its contributors
14200583Srdivacky *    may be used to endorse or promote products derived from this software
15200583Srdivacky *    without specific prior written permission.
16200583Srdivacky *
17200583Srdivacky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18200583Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19200583Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20200583Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21200583Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22200583Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23200583Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24200583Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25200583Srdivacky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26200583Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27200583Srdivacky * SUCH DAMAGE.
28200583Srdivacky */
29200583Srdivacky
30200583Srdivacky#if defined(LIBC_SCCS) && !defined(lint)
31200583Srdivackystatic char sccsid[] = "@(#)getmntinfo.c	8.1 (Berkeley) 6/4/93";
32200583Srdivacky#endif /* LIBC_SCCS and not lint */
33200583Srdivacky#include <sys/cdefs.h>
34200583Srdivacky__FBSDID("$FreeBSD: stable/11/lib/libc/gen/getmntinfo.c 297790 2016-04-10 19:33:58Z pfg $");
35200583Srdivacky
36200583Srdivacky#include <sys/param.h>
37200583Srdivacky#include <sys/ucred.h>
38200583Srdivacky#include <sys/mount.h>
39200583Srdivacky#include <stdlib.h>
40200583Srdivacky
41200583Srdivacky/*
42200583Srdivacky * Return information about mounted filesystems.
43200583Srdivacky */
44200583Srdivackyint
45200583Srdivackygetmntinfo(struct statfs **mntbufp, int flags)
46200583Srdivacky{
47200583Srdivacky	static struct statfs *mntbuf;
48200583Srdivacky	static int mntsize;
49200583Srdivacky	static long bufsize;
50200583Srdivacky
51200583Srdivacky	if (mntsize <= 0 && (mntsize = getfsstat(0, 0, MNT_NOWAIT)) < 0)
52200583Srdivacky		return (0);
53200583Srdivacky	if (bufsize > 0 && (mntsize = getfsstat(mntbuf, bufsize, flags)) < 0)
54200583Srdivacky		return (0);
55200583Srdivacky	while (bufsize <= mntsize * sizeof(struct statfs)) {
56200583Srdivacky		if (mntbuf)
57200583Srdivacky			free(mntbuf);
58200583Srdivacky		bufsize = (mntsize + 1) * sizeof(struct statfs);
59200583Srdivacky		if ((mntbuf = malloc(bufsize)) == NULL)
60200583Srdivacky			return (0);
61200583Srdivacky		if ((mntsize = getfsstat(mntbuf, bufsize, flags)) < 0)
62200583Srdivacky			return (0);
63200583Srdivacky	}
64200583Srdivacky	*mntbufp = mntbuf;
65200583Srdivacky	return (mntsize);
66200583Srdivacky}
67200583Srdivacky