getmntinfo.c revision 297790
12708Swollman/* 22708Swollman * Copyright (c) 1989, 1993 32708Swollman * The Regents of the University of California. All rights reserved. 4130461Sstefanf * 517209Swollman * Redistribution and use in source and binary forms, with or without 617209Swollman * modification, are permitted provided that the following conditions 7192625Sedwin * are met: 892991Sobrien * 1. Redistributions of source code must retain the above copyright 992991Sobrien * notice, this list of conditions and the following disclaimer. 1017209Swollman * 2. Redistributions in binary form must reproduce the above copyright 112708Swollman * notice, this list of conditions and the following disclaimer in the 122741Swollman * documentation and/or other materials provided with the distribution. 132741Swollman * 4. Neither the name of the University nor the names of its contributors 142741Swollman * may be used to endorse or promote products derived from this software 152741Swollman * without specific prior written permission. 162741Swollman * 172741Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 182741Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1917209Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2017209Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2117209Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 222741Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 232741Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 242741Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 252708Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 262708Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 272708Swollman * SUCH DAMAGE. 282708Swollman */ 292708Swollman 302708Swollman#if defined(LIBC_SCCS) && !defined(lint) 312708Swollmanstatic char sccsid[] = "@(#)getmntinfo.c 8.1 (Berkeley) 6/4/93"; 322708Swollman#endif /* LIBC_SCCS and not lint */ 332708Swollman#include <sys/cdefs.h> 342708Swollman__FBSDID("$FreeBSD: head/lib/libc/gen/getmntinfo.c 297790 2016-04-10 19:33:58Z pfg $"); 352708Swollman 362708Swollman#include <sys/param.h> 372708Swollman#include <sys/ucred.h> 382708Swollman#include <sys/mount.h> 3917209Swollman#include <stdlib.h> 40192625Sedwin 4117209Swollman/* 422708Swollman * Return information about mounted filesystems. 432708Swollman */ 442708Swollmanint 45192625Sedwingetmntinfo(struct statfs **mntbufp, int flags) 46192625Sedwin{ 472708Swollman static struct statfs *mntbuf; 489936Swollman static int mntsize; 499936Swollman static long bufsize; 502708Swollman 512708Swollman if (mntsize <= 0 && (mntsize = getfsstat(0, 0, MNT_NOWAIT)) < 0) 529936Swollman return (0); 539936Swollman if (bufsize > 0 && (mntsize = getfsstat(mntbuf, bufsize, flags)) < 0) 549936Swollman return (0); 552708Swollman while (bufsize <= mntsize * sizeof(struct statfs)) { 5617209Swollman if (mntbuf) 5717209Swollman free(mntbuf); 5817209Swollman bufsize = (mntsize + 1) * sizeof(struct statfs); 5917209Swollman if ((mntbuf = malloc(bufsize)) == NULL) 60130461Sstefanf return (0); 61130461Sstefanf if ((mntsize = getfsstat(mntbuf, bufsize, flags)) < 0) 62130461Sstefanf return (0); 63130461Sstefanf } 649936Swollman *mntbufp = mntbuf; 659936Swollman return (mntsize); 669936Swollman} 679936Swollman