Update copyright notices with scripts/update-copyrights.
[jlayton/glibc.git] / misc / fstab.c
1 /* Copyright (C) 1995-2013 Free Software Foundation, Inc.
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
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.
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
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 #include <fstab.h>
19 #include <mntent.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <bits/libc-lock.h>
24
25 #define BUFFER_SIZE 0x1fc0
26
27 struct fstab_state
28 {
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
41
42 int
43 setfsent (void)
44 {
45   return fstab_init (1) != NULL;
46 }
47
48
49 struct fstab *
50 getfsent (void)
51 {
52   struct fstab_state *state;
53
54   state = fstab_init (0);
55   if (state == NULL)
56     return NULL;
57   if (fstab_fetch (state) == NULL)
58     return NULL;
59   return fstab_convert (state);
60 }
61
62
63 struct fstab *
64 getfsspec (name)
65      const char *name;
66 {
67   struct fstab_state *state;
68   struct mntent *m;
69
70   state = fstab_init (1);
71   if (state == NULL)
72     return NULL;
73   while ((m = fstab_fetch (state)) != NULL)
74     if (strcmp (m->mnt_fsname, name) == 0)
75       return fstab_convert (state);
76   return NULL;
77 }
78
79
80 struct fstab *
81 getfsfile (name)
82      const char *name;
83 {
84   struct fstab_state *state;
85   struct mntent *m;
86
87   state = fstab_init (1);
88   if (state == NULL)
89     return NULL;
90   while ((m = fstab_fetch (state)) != NULL)
91     if (strcmp (m->mnt_dir, name) == 0)
92       return fstab_convert (state);
93   return NULL;
94 }
95
96
97 void
98 endfsent ()
99 {
100   struct fstab_state *state;
101
102   state = &fstab_state;
103   if (state->fs_fp != NULL)
104     {
105       (void) __endmntent (state->fs_fp);
106       state->fs_fp = NULL;
107     }
108 }
109
110
111 static struct fstab_state *
112 fstab_init (int opt_rewind)
113 {
114   struct fstab_state *state;
115   char *buffer;
116   FILE *fp;
117
118   state = &fstab_state;
119
120   buffer = state->fs_buffer;
121   if (buffer == NULL)
122     {
123       buffer = (char *) malloc (BUFFER_SIZE);
124       if (buffer == NULL)
125         return NULL;
126       state->fs_buffer = buffer;
127     }
128
129   fp = state->fs_fp;
130   if (fp != NULL)
131     {
132       if (opt_rewind)
133         rewind (fp);
134     }
135   else
136     {
137       fp = __setmntent (_PATH_FSTAB, "r");
138       if (fp == NULL)
139         return NULL;
140       state->fs_fp = fp;
141     }
142
143   return state;
144 }
145
146
147 static struct mntent *
148 fstab_fetch (struct fstab_state *state)
149 {
150   return __getmntent_r (state->fs_fp, &state->fs_mntres,
151                         state->fs_buffer, BUFFER_SIZE);
152 }
153
154
155 static struct fstab *
156 fstab_convert (struct fstab_state *state)
157 {
158   struct mntent *m;
159   struct fstab *f;
160
161   m = &state->fs_mntres;
162   f = &state->fs_ret;
163
164   f->fs_spec = m->mnt_fsname;
165   f->fs_file = m->mnt_dir;
166   f->fs_vfstype = m->mnt_type;
167   f->fs_mntops = m->mnt_opts;
168   f->fs_type = (__hasmntopt (m, FSTAB_RW) ? FSTAB_RW :
169                 __hasmntopt (m, FSTAB_RQ) ? FSTAB_RQ :
170                 __hasmntopt (m, FSTAB_RO) ? FSTAB_RO :
171                 __hasmntopt (m, FSTAB_SW) ? FSTAB_SW :
172                 __hasmntopt (m, FSTAB_XX) ? FSTAB_XX :
173                 "??");
174   f->fs_freq = m->mnt_freq;
175   f->fs_passno = m->mnt_passno;
176   return f;
177 }
178
179
180 /* Make sure the memory is freed if the programs ends while in
181    memory-debugging mode and something actually was allocated.  */
182 libc_freeres_fn (fstab_free)
183 {
184   char *buffer;
185
186   buffer = fstab_state.fs_buffer;
187   free ((void *) buffer);
188 }