.
[glibc/history.git] / misc / fstab.c
blobab5581e9b74cdbf3a4b3fb167a65e6433b99ef2c
1 /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2008
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <fstab.h>
21 #include <mntent.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
27 #define BUFFER_SIZE 0x1fc0
29 struct fstab_state
31 FILE *fs_fp;
32 char *fs_buffer;
33 struct mntent fs_mntres;
34 struct fstab fs_ret;
37 static struct fstab_state *fstab_init (int opt_rewind);
38 static struct mntent *fstab_fetch (struct fstab_state *state);
39 static struct fstab *fstab_convert (struct fstab_state *state);
41 static struct fstab_state fstab_state;
44 int
45 setfsent (void)
47 return fstab_init (1) != NULL;
51 struct fstab *
52 getfsent (void)
54 struct fstab_state *state;
56 state = fstab_init (0);
57 if (state == NULL)
58 return NULL;
59 if (fstab_fetch (state) == NULL)
60 return NULL;
61 return fstab_convert (state);
65 struct fstab *
66 getfsspec (name)
67 const char *name;
69 struct fstab_state *state;
70 struct mntent *m;
72 state = fstab_init (1);
73 if (state == NULL)
74 return NULL;
75 while ((m = fstab_fetch (state)) != NULL)
76 if (strcmp (m->mnt_fsname, name) == 0)
77 return fstab_convert (state);
78 return NULL;
82 struct fstab *
83 getfsfile (name)
84 const char *name;
86 struct fstab_state *state;
87 struct mntent *m;
89 state = fstab_init (1);
90 if (state == NULL)
91 return NULL;
92 while ((m = fstab_fetch (state)) != NULL)
93 if (strcmp (m->mnt_dir, name) == 0)
94 return fstab_convert (state);
95 return NULL;
99 void
100 endfsent ()
102 struct fstab_state *state;
104 state = &fstab_state;
105 if (state->fs_fp != NULL)
107 (void) __endmntent (state->fs_fp);
108 state->fs_fp = NULL;
113 static struct fstab_state *
114 fstab_init (int opt_rewind)
116 struct fstab_state *state;
117 char *buffer;
118 FILE *fp;
120 state = &fstab_state;
122 buffer = state->fs_buffer;
123 if (buffer == NULL)
125 buffer = (char *) malloc (BUFFER_SIZE);
126 if (buffer == NULL)
127 return NULL;
128 state->fs_buffer = buffer;
131 fp = state->fs_fp;
132 if (fp != NULL)
134 if (opt_rewind)
135 rewind (fp);
137 else
139 fp = __setmntent (_PATH_FSTAB, "r");
140 if (fp == NULL)
141 return NULL;
142 state->fs_fp = fp;
145 return state;
149 static struct mntent *
150 fstab_fetch (struct fstab_state *state)
152 return __getmntent_r (state->fs_fp, &state->fs_mntres,
153 state->fs_buffer, BUFFER_SIZE);
157 static struct fstab *
158 fstab_convert (struct fstab_state *state)
160 struct mntent *m;
161 struct fstab *f;
163 m = &state->fs_mntres;
164 f = &state->fs_ret;
166 f->fs_spec = m->mnt_fsname;
167 f->fs_file = m->mnt_dir;
168 f->fs_vfstype = m->mnt_type;
169 f->fs_mntops = m->mnt_opts;
170 f->fs_type = (__hasmntopt (m, FSTAB_RW) ? FSTAB_RW :
171 __hasmntopt (m, FSTAB_RQ) ? FSTAB_RQ :
172 __hasmntopt (m, FSTAB_RO) ? FSTAB_RO :
173 __hasmntopt (m, FSTAB_SW) ? FSTAB_SW :
174 __hasmntopt (m, FSTAB_XX) ? FSTAB_XX :
175 "??");
176 f->fs_freq = m->mnt_freq;
177 f->fs_passno = m->mnt_passno;
178 return f;
182 /* Make sure the memory is freed if the programs ends while in
183 memory-debugging mode and something actually was allocated. */
184 libc_freeres_fn (fstab_free)
186 char *buffer;
188 buffer = fstab_state.fs_buffer;
189 free ((void *) buffer);