xref: /netbsd-src/lib/libc/compat/gen/compat___getmntinfo13.c (revision 02cdd248ec8b17634bab40aa4f2e161a756d7fce)
1*02cdd248Schristos /*	$NetBSD: compat___getmntinfo13.c,v 1.1 2019/09/22 22:59:38 christos Exp $	*/
2*02cdd248Schristos 
3*02cdd248Schristos /*-
4*02cdd248Schristos  * Copyright (c) 2019 The NetBSD Foundation, Inc.
5*02cdd248Schristos  * All rights reserved.
6*02cdd248Schristos  *
7*02cdd248Schristos  * This code is derived from software contributed to The NetBSD Foundation
8*02cdd248Schristos  * by Christos Zoulas.
9*02cdd248Schristos  *
10*02cdd248Schristos  * Redistribution and use in source and binary forms, with or without
11*02cdd248Schristos  * modification, are permitted provided that the following conditions
12*02cdd248Schristos  * are met:
13*02cdd248Schristos  * 1. Redistributions of source code must retain the above copyright
14*02cdd248Schristos  *    notice, this list of conditions and the following disclaimer.
15*02cdd248Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16*02cdd248Schristos  *    notice, this list of conditions and the following disclaimer in the
17*02cdd248Schristos  *    documentation and/or other materials provided with the distribution.
18*02cdd248Schristos  *
19*02cdd248Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*02cdd248Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*02cdd248Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*02cdd248Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*02cdd248Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*02cdd248Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*02cdd248Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*02cdd248Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*02cdd248Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*02cdd248Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*02cdd248Schristos  * POSSIBILITY OF SUCH DAMAGE.
30*02cdd248Schristos  */
31*02cdd248Schristos 
32*02cdd248Schristos #include <sys/cdefs.h>
33*02cdd248Schristos #if defined(LIBC_SCCS) && !defined(lint)
34*02cdd248Schristos __RCSID("$NetBSD: compat___getmntinfo13.c,v 1.1 2019/09/22 22:59:38 christos Exp $");
35*02cdd248Schristos #endif /* LIBC_SCCS and not lint */
36*02cdd248Schristos 
37*02cdd248Schristos #define __LIBC12_SOURCE__
38*02cdd248Schristos 
39*02cdd248Schristos #include "namespace.h"
40*02cdd248Schristos 
41*02cdd248Schristos #include <sys/types.h>
42*02cdd248Schristos #include <sys/param.h>
43*02cdd248Schristos 
44*02cdd248Schristos #include <assert.h>
45*02cdd248Schristos #include <compat/sys/statvfs.h>
46*02cdd248Schristos #include <string.h>
47*02cdd248Schristos #include <stdlib.h>
48*02cdd248Schristos 
49*02cdd248Schristos __warn_references(__getmntinfo13,
50*02cdd248Schristos     "warning: reference to obsolete __getmntinfo13(); use statvfs()")
51*02cdd248Schristos 
__strong_alias(__getmntinfo13,__compat___getmntinfo13)52*02cdd248Schristos __strong_alias(__getmntinfo13, __compat___getmntinfo13)
53*02cdd248Schristos 
54*02cdd248Schristos /*
55*02cdd248Schristos  * Return information about mounted filesystems.
56*02cdd248Schristos  */
57*02cdd248Schristos int
58*02cdd248Schristos __compat___getmntinfo13(struct statvfs90 **mntbufp, int flags)
59*02cdd248Schristos {
60*02cdd248Schristos 	static struct statvfs90 *mntbuf;
61*02cdd248Schristos 	static int mntsize;
62*02cdd248Schristos 	static size_t bufsize;
63*02cdd248Schristos 
64*02cdd248Schristos 	_DIAGASSERT(mntbufp != NULL);
65*02cdd248Schristos 
66*02cdd248Schristos 	if (mntsize <= 0 &&
67*02cdd248Schristos 	    (mntsize = __compat_getvfsstat(NULL, 0L, MNT_NOWAIT)) == -1)
68*02cdd248Schristos 		return (0);
69*02cdd248Schristos 	if (bufsize > 0 &&
70*02cdd248Schristos 	    (mntsize = __compat_getvfsstat(mntbuf, bufsize, flags)) == -1)
71*02cdd248Schristos 		return (0);
72*02cdd248Schristos 	while (bufsize <= mntsize * sizeof(struct statvfs90)) {
73*02cdd248Schristos 		if (mntbuf)
74*02cdd248Schristos 			free(mntbuf);
75*02cdd248Schristos 		bufsize = (mntsize + 1) * sizeof(struct statvfs90);
76*02cdd248Schristos 		if ((mntbuf = malloc(bufsize)) == NULL)
77*02cdd248Schristos 			return (0);
78*02cdd248Schristos 		if ((mntsize = __compat_getvfsstat(mntbuf, bufsize,
79*02cdd248Schristos 		    flags)) == -1)
80*02cdd248Schristos 			return (0);
81*02cdd248Schristos 	}
82*02cdd248Schristos 	*mntbufp = mntbuf;
83*02cdd248Schristos 	return (mntsize);
84*02cdd248Schristos }
85