1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * mntent_compat.c
4 * Copyright (C) Kevin DeKorte 2006 <kdekorte@gmail.com>
5 *
6 * mntent_compat.c is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * mntent_compat.c is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with mntent_compat.c. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #ifdef HAVE_MNTENT_H
30 #else
31 #include "mntent_compat.h"
32 #include <sys/param.h>
33 #ifdef HAVE_FSTAB_H
34 #include <fstab.h>
35 #endif
36
37 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 299000900)
38 #include <sys/statvfs.h>
39 #define statfs statvfs
40 #define f_flags f_flag
41 #endif
42
43 struct statfs *getmntent_mntbufp;
44 int getmntent_mntcount = 0;
45 int getmntent_mntpos = 0;
46 char mntent_global_opts[256];
47 struct mntent mntent_global_mntent;
48
setmntent(char * filep,char * type)49 FILE *setmntent(char *filep, char *type)
50 {
51 getmntent_mntpos = 0;
52 #ifdef HAVE_SYS_MOUNT_H
53 getmntent_mntcount = getmntinfo(&getmntent_mntbufp, MNT_WAIT);
54 #endif
55 return (FILE *) 1; // dummy
56 }
57
getmntent_addopt(char ** c,const char * s)58 void getmntent_addopt(char **c, const char *s)
59 {
60 int i = strlen(s);
61 *(*c)++ = ',';
62 strcpy(*c, s);
63 *c += i;
64 }
65
getmntent(FILE * filep)66 struct mntent *getmntent(FILE * filep)
67 {
68 #ifdef HAVE_SYS_MOUNT_H
69 char *c = mntent_global_opts + 2;
70 struct fstab *fst;
71 if (getmntent_mntpos >= getmntent_mntcount)
72 return 0;
73 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_RDONLY)
74 strcpy(mntent_global_opts, "ro");
75 else
76 strcpy(mntent_global_opts, "rw");
77
78 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_SYNCHRONOUS)
79 getmntent_addopt(&c, "sync");
80 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_NOEXEC)
81 getmntent_addopt(&c, "noexec");
82 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_NOSUID)
83 getmntent_addopt(&c, "nosuid");
84 #ifdef MNT_NODEV
85 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_NODEV)
86 getmntent_addopt(&c, "nodev");
87 #endif
88 #ifdef MNT_UNION
89 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_UNION)
90 getmntent_addopt(&c, "union");
91 #endif
92 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_ASYNC)
93 getmntent_addopt(&c, "async");
94 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_NOATIME)
95 getmntent_addopt(&c, "noatime");
96 #ifdef MNT_NOCLUSTERR
97 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_NOCLUSTERR)
98 getmntent_addopt(&c, "noclusterr");
99 #endif
100 #ifdef MNT_NOCLUSTERW
101 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_NOCLUSTERW)
102 getmntent_addopt(&c, "noclusterw");
103 #endif
104 #ifdef MNT_NOSYMFOLLOW
105 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_NOSYMFOLLOW)
106 getmntent_addopt(&c, "nosymfollow");
107 #endif
108 #ifdef MNT_SUIDDIR
109 if (getmntent_mntbufp[getmntent_mntpos].f_flags & MNT_SUIDDIR)
110 getmntent_addopt(&c, "suiddir");
111 #endif
112 mntent_global_mntent.mnt_fsname = getmntent_mntbufp[getmntent_mntpos].f_mntfromname;
113 mntent_global_mntent.mnt_dir = getmntent_mntbufp[getmntent_mntpos].f_mntonname;
114 mntent_global_mntent.mnt_type = getmntent_mntbufp[getmntent_mntpos].f_fstypename;
115 mntent_global_mntent.mnt_opts = mntent_global_opts;
116 if ((fst = getfsspec(getmntent_mntbufp[getmntent_mntpos].f_mntfromname))) {
117 mntent_global_mntent.mnt_freq = fst->fs_freq;
118 mntent_global_mntent.mnt_passno = fst->fs_passno;
119 } else if ((fst = getfsfile(getmntent_mntbufp[getmntent_mntpos].f_mntonname))) {
120 mntent_global_mntent.mnt_freq = fst->fs_freq;
121 mntent_global_mntent.mnt_passno = fst->fs_passno;
122 } else if (strcmp(getmntent_mntbufp[getmntent_mntpos].f_fstypename, "ufs") == 0) {
123 if (strcmp(getmntent_mntbufp[getmntent_mntpos].f_mntonname, "/") == 0) {
124 mntent_global_mntent.mnt_freq = 1;
125 mntent_global_mntent.mnt_passno = 1;
126 } else {
127 mntent_global_mntent.mnt_freq = 2;
128 mntent_global_mntent.mnt_passno = 2;
129 }
130 } else {
131 mntent_global_mntent.mnt_freq = 0;
132 mntent_global_mntent.mnt_passno = 0;
133 }
134 ++getmntent_mntpos;
135 return &mntent_global_mntent;
136 #else
137 return NULL;
138 #endif
139 }
140
endmntent(FILE * filep)141 int endmntent(FILE * filep)
142 {
143 return 0;
144 }
145 #endif
146