Back to index

glibc  2.9
fstab.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2008
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <fstab.h>
00021 #include <mntent.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <bits/libc-lock.h>
00026 
00027 #define BUFFER_SIZE 0x1fc0
00028 
00029 struct fstab_state
00030 {
00031   FILE *fs_fp;
00032   char *fs_buffer;
00033   struct mntent fs_mntres;
00034   struct fstab fs_ret;
00035 };
00036 
00037 static struct fstab_state *fstab_init (int opt_rewind);
00038 static struct mntent *fstab_fetch (struct fstab_state *state);
00039 static struct fstab *fstab_convert (struct fstab_state *state);
00040 
00041 static struct fstab_state fstab_state;
00042 
00043 
00044 int
00045 setfsent (void)
00046 {
00047   return fstab_init (1) != NULL;
00048 }
00049 
00050 
00051 struct fstab *
00052 getfsent (void)
00053 {
00054   struct fstab_state *state;
00055 
00056   state = fstab_init (0);
00057   if (state == NULL)
00058     return NULL;
00059   if (fstab_fetch (state) == NULL)
00060     return NULL;
00061   return fstab_convert (state);
00062 }
00063 
00064 
00065 struct fstab *
00066 getfsspec (name)
00067      const char *name;
00068 {
00069   struct fstab_state *state;
00070   struct mntent *m;
00071 
00072   state = fstab_init (1);
00073   if (state == NULL)
00074     return NULL;
00075   while ((m = fstab_fetch (state)) != NULL)
00076     if (strcmp (m->mnt_fsname, name) == 0)
00077       return fstab_convert (state);
00078   return NULL;
00079 }
00080 
00081 
00082 struct fstab *
00083 getfsfile (name)
00084      const char *name;
00085 {
00086   struct fstab_state *state;
00087   struct mntent *m;
00088 
00089   state = fstab_init (1);
00090   if (state == NULL)
00091     return NULL;
00092   while ((m = fstab_fetch (state)) != NULL)
00093     if (strcmp (m->mnt_dir, name) == 0)
00094       return fstab_convert (state);
00095   return NULL;
00096 }
00097 
00098 
00099 void
00100 endfsent ()
00101 {
00102   struct fstab_state *state;
00103 
00104   state = &fstab_state;
00105   if (state->fs_fp != NULL)
00106     {
00107       (void) __endmntent (state->fs_fp);
00108       state->fs_fp = NULL;
00109     }
00110 }
00111 
00112 
00113 static struct fstab_state *
00114 fstab_init (int opt_rewind)
00115 {
00116   struct fstab_state *state;
00117   char *buffer;
00118   FILE *fp;
00119 
00120   state = &fstab_state;
00121 
00122   buffer = state->fs_buffer;
00123   if (buffer == NULL)
00124     {
00125       buffer = (char *) malloc (BUFFER_SIZE);
00126       if (buffer == NULL)
00127        return NULL;
00128       state->fs_buffer = buffer;
00129     }
00130 
00131   fp = state->fs_fp;
00132   if (fp != NULL)
00133     {
00134       if (opt_rewind)
00135        rewind (fp);
00136     }
00137   else
00138     {
00139       fp = __setmntent (_PATH_FSTAB, "r");
00140       if (fp == NULL)
00141        return NULL;
00142       state->fs_fp = fp;
00143     }
00144 
00145   return state;
00146 }
00147 
00148 
00149 static struct mntent *
00150 fstab_fetch (struct fstab_state *state)
00151 {
00152   return __getmntent_r (state->fs_fp, &state->fs_mntres,
00153                      state->fs_buffer, BUFFER_SIZE);
00154 }
00155 
00156 
00157 static struct fstab *
00158 fstab_convert (struct fstab_state *state)
00159 {
00160   struct mntent *m;
00161   struct fstab *f;
00162 
00163   m = &state->fs_mntres;
00164   f = &state->fs_ret;
00165 
00166   f->fs_spec = m->mnt_fsname;
00167   f->fs_file = m->mnt_dir;
00168   f->fs_vfstype = m->mnt_type;
00169   f->fs_mntops = m->mnt_opts;
00170   f->fs_type = (__hasmntopt (m, FSTAB_RW) ? FSTAB_RW :
00171               __hasmntopt (m, FSTAB_RQ) ? FSTAB_RQ :
00172               __hasmntopt (m, FSTAB_RO) ? FSTAB_RO :
00173               __hasmntopt (m, FSTAB_SW) ? FSTAB_SW :
00174               __hasmntopt (m, FSTAB_XX) ? FSTAB_XX :
00175               "??");
00176   f->fs_freq = m->mnt_freq;
00177   f->fs_passno = m->mnt_passno;
00178   return f;
00179 }
00180 
00181 
00182 /* Make sure the memory is freed if the programs ends while in
00183    memory-debugging mode and something actually was allocated.  */
00184 libc_freeres_fn (fstab_free)
00185 {
00186   char *buffer;
00187 
00188   buffer = fstab_state.fs_buffer;
00189   free ((void *) buffer);
00190 }