death to trailing whitespace
[openafs.git] / src / util / fstab.c
1 /*-
2  * Copyright (c) 1980, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <afsconfig.h>
31 #include <afs/param.h>
32
33 #if defined(AFS_DARWIN_ENV)
34 /*
35  * Reworked from FreeBSD umount
36  */
37
38 #include <sys/param.h>
39 #include <sys/mount.h>
40
41 #include <err.h>
42 #include <errno.h>
43 #include <fstab.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46
47 size_t
48 mntinfo(struct statfs **mntbuffer)
49 {
50     static struct statfs *origbuf;
51     size_t bufsize;
52     int mntsize;
53
54     mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
55     if (mntsize <= 0)
56         return (0);
57     bufsize = (mntsize + 1) * sizeof(struct statfs);
58     if ((origbuf = malloc(bufsize)) == NULL)
59         err(1, "malloc");
60     mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
61     *mntbuffer = origbuf;
62     return (mntsize);
63 }
64
65 static struct statfs *mntbuf = (struct statfs *)NULL;
66 static struct statfs *mntent;
67 static int mntcnt;
68 static struct fstab fstabent;
69
70 struct fstab *
71 getfsent(void)
72 {
73     if (((!mntbuf) && !setfsent()) || mntcnt == 0)
74         return ((struct fstab *)NULL);
75     fstabent.fs_file = mntent->f_mntonname;
76     fstabent.fs_freq = fstabent.fs_passno = 0;
77     fstabent.fs_mntops = mntent->f_fstypename;
78     fstabent.fs_spec = mntent->f_mntfromname;
79     if (mntent->f_flags & MNT_RDONLY)
80         fstabent.fs_type = FSTAB_RO;
81     else
82         fstabent.fs_type = FSTAB_RW;
83     fstabent.fs_vfstype = mntent->f_fstypename;
84
85     mntent++;
86     mntcnt--;
87     return &fstabent;
88 }
89
90 int
91 setfsent(void)
92 {
93     if (mntbuf)
94         free(mntbuf);
95     mntbuf = NULL;
96     mntent = 0;
97     if ((mntcnt = mntinfo(&mntbuf)) <= 0) {
98         err(1, "setfsent");
99         return 0;
100     }
101     mntent = mntbuf;
102     return 1;
103 }
104
105 void
106 endfsent(void)
107 {
108     if (mntbuf)
109         free(mntbuf);
110     mntbuf = NULL;
111     mntent = 0;
112     mntcnt = 0;
113 }
114 #endif