fstab.c 3.8 KB

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