]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1995-2015 Free Software Foundation, Inc. |
19361cb7 UD |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
19361cb7 UD |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
19361cb7 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 17 | |
7a770247 RM |
18 | #include <fstab.h> |
19 | #include <mntent.h> | |
20 | #include <stdio.h> | |
d111572f | 21 | #include <stdlib.h> |
ef52edfc | 22 | #include <string.h> |
ec999b8e | 23 | #include <libc-lock.h> |
28f540f4 | 24 | |
d111572f | 25 | #define BUFFER_SIZE 0x1fc0 |
845dcb57 | 26 | |
d111572f | 27 | struct fstab_state |
28f540f4 | 28 | { |
d111572f UD |
29 | FILE *fs_fp; |
30 | char *fs_buffer; | |
31 | struct mntent fs_mntres; | |
32 | struct fstab fs_ret; | |
33 | }; | |
34 | ||
35 | static struct fstab_state *fstab_init (int opt_rewind); | |
36 | static struct mntent *fstab_fetch (struct fstab_state *state); | |
37 | static struct fstab *fstab_convert (struct fstab_state *state); | |
38 | ||
39 | static struct fstab_state fstab_state; | |
40 | ||
28f540f4 | 41 | |
7a770247 RM |
42 | int |
43 | setfsent (void) | |
28f540f4 | 44 | { |
d111572f | 45 | return fstab_init (1) != NULL; |
28f540f4 RM |
46 | } |
47 | ||
28f540f4 RM |
48 | |
49 | struct fstab * | |
7a770247 | 50 | getfsent (void) |
28f540f4 | 51 | { |
d111572f | 52 | struct fstab_state *state; |
7a770247 | 53 | |
d111572f UD |
54 | state = fstab_init (0); |
55 | if (state == NULL) | |
7a770247 | 56 | return NULL; |
d111572f UD |
57 | if (fstab_fetch (state) == NULL) |
58 | return NULL; | |
59 | return fstab_convert (state); | |
28f540f4 RM |
60 | } |
61 | ||
d111572f | 62 | |
7a770247 | 63 | struct fstab * |
9d46370c | 64 | getfsspec (const char *name) |
28f540f4 | 65 | { |
d111572f | 66 | struct fstab_state *state; |
7a770247 | 67 | struct mntent *m; |
d111572f UD |
68 | |
69 | state = fstab_init (1); | |
70 | if (state == NULL) | |
71 | return NULL; | |
72 | while ((m = fstab_fetch (state)) != NULL) | |
73 | if (strcmp (m->mnt_fsname, name) == 0) | |
74 | return fstab_convert (state); | |
7a770247 | 75 | return NULL; |
28f540f4 RM |
76 | } |
77 | ||
d111572f | 78 | |
7a770247 | 79 | struct fstab * |
9d46370c | 80 | getfsfile (const char *name) |
28f540f4 | 81 | { |
d111572f | 82 | struct fstab_state *state; |
7a770247 | 83 | struct mntent *m; |
d111572f UD |
84 | |
85 | state = fstab_init (1); | |
86 | if (state == NULL) | |
87 | return NULL; | |
88 | while ((m = fstab_fetch (state)) != NULL) | |
89 | if (strcmp (m->mnt_dir, name) == 0) | |
90 | return fstab_convert (state); | |
7a770247 | 91 | return NULL; |
28f540f4 RM |
92 | } |
93 | ||
d111572f | 94 | |
7a770247 | 95 | void |
60d2f8f3 | 96 | endfsent (void) |
28f540f4 | 97 | { |
d111572f UD |
98 | struct fstab_state *state; |
99 | ||
100 | state = &fstab_state; | |
101 | if (state->fs_fp != NULL) | |
7a770247 | 102 | { |
50304ef0 | 103 | (void) __endmntent (state->fs_fp); |
d111572f | 104 | state->fs_fp = NULL; |
7a770247 | 105 | } |
28f540f4 | 106 | } |
d111572f UD |
107 | |
108 | ||
109 | static struct fstab_state * | |
110 | fstab_init (int opt_rewind) | |
111 | { | |
112 | struct fstab_state *state; | |
113 | char *buffer; | |
114 | FILE *fp; | |
115 | ||
116 | state = &fstab_state; | |
117 | ||
118 | buffer = state->fs_buffer; | |
119 | if (buffer == NULL) | |
120 | { | |
121 | buffer = (char *) malloc (BUFFER_SIZE); | |
122 | if (buffer == NULL) | |
123 | return NULL; | |
124 | state->fs_buffer = buffer; | |
125 | } | |
126 | ||
127 | fp = state->fs_fp; | |
128 | if (fp != NULL) | |
129 | { | |
130 | if (opt_rewind) | |
131 | rewind (fp); | |
132 | } | |
133 | else | |
134 | { | |
50304ef0 | 135 | fp = __setmntent (_PATH_FSTAB, "r"); |
d111572f UD |
136 | if (fp == NULL) |
137 | return NULL; | |
138 | state->fs_fp = fp; | |
139 | } | |
140 | ||
141 | return state; | |
142 | } | |
143 | ||
144 | ||
145 | static struct mntent * | |
146 | fstab_fetch (struct fstab_state *state) | |
147 | { | |
148 | return __getmntent_r (state->fs_fp, &state->fs_mntres, | |
149 | state->fs_buffer, BUFFER_SIZE); | |
150 | } | |
151 | ||
152 | ||
153 | static struct fstab * | |
154 | fstab_convert (struct fstab_state *state) | |
155 | { | |
156 | struct mntent *m; | |
157 | struct fstab *f; | |
158 | ||
159 | m = &state->fs_mntres; | |
160 | f = &state->fs_ret; | |
161 | ||
162 | f->fs_spec = m->mnt_fsname; | |
163 | f->fs_file = m->mnt_dir; | |
164 | f->fs_vfstype = m->mnt_type; | |
165 | f->fs_mntops = m->mnt_opts; | |
50304ef0 UD |
166 | f->fs_type = (__hasmntopt (m, FSTAB_RW) ? FSTAB_RW : |
167 | __hasmntopt (m, FSTAB_RQ) ? FSTAB_RQ : | |
168 | __hasmntopt (m, FSTAB_RO) ? FSTAB_RO : | |
169 | __hasmntopt (m, FSTAB_SW) ? FSTAB_SW : | |
170 | __hasmntopt (m, FSTAB_XX) ? FSTAB_XX : | |
d111572f UD |
171 | "??"); |
172 | f->fs_freq = m->mnt_freq; | |
173 | f->fs_passno = m->mnt_passno; | |
174 | return f; | |
175 | } | |
176 | ||
177 | ||
178 | /* Make sure the memory is freed if the programs ends while in | |
179 | memory-debugging mode and something actually was allocated. */ | |
c877418f | 180 | libc_freeres_fn (fstab_free) |
d111572f UD |
181 | { |
182 | char *buffer; | |
183 | ||
184 | buffer = fstab_state.fs_buffer; | |
62605cbf | 185 | free ((void *) buffer); |
d111572f | 186 | } |