mntent_r.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* Utilities for reading/writing fstab, mtab, etc.
  2. Copyright (C) 1995-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <alloca.h>
  16. #include <mntent.h>
  17. #include <stdio.h>
  18. #include <stdio_ext.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #define flockfile(s) _IO_flockfile (s)
  22. #define funlockfile(s) _IO_funlockfile (s)
  23. #undef __setmntent
  24. #undef __endmntent
  25. #undef __getmntent_r
  26. /* Prepare to begin reading and/or writing mount table entries from the
  27. beginning of FILE. MODE is as for `fopen'. */
  28. FILE *
  29. __setmntent (const char *file, const char *mode)
  30. {
  31. /* Extend the mode parameter with "c" to disable cancellation in the
  32. I/O functions and "e" to set FD_CLOEXEC. */
  33. size_t modelen = strlen (mode);
  34. char newmode[modelen + 3];
  35. memcpy (mempcpy (newmode, mode, modelen), "ce", 3);
  36. FILE *result = fopen (file, newmode);
  37. if (result != NULL)
  38. /* We do the locking ourselves. */
  39. __fsetlocking (result, FSETLOCKING_BYCALLER);
  40. return result;
  41. }
  42. libc_hidden_def (__setmntent)
  43. weak_alias (__setmntent, setmntent)
  44. /* Close a stream opened with `setmntent'. */
  45. int
  46. __endmntent (FILE *stream)
  47. {
  48. if (stream) /* SunOS 4.x allows for NULL stream */
  49. fclose (stream);
  50. return 1; /* SunOS 4.x says to always return 1 */
  51. }
  52. libc_hidden_def (__endmntent)
  53. weak_alias (__endmntent, endmntent)
  54. /* Since the values in a line are separated by spaces, a name cannot
  55. contain a space. Therefore some programs encode spaces in names
  56. by the strings "\040". We undo the encoding when reading an entry.
  57. The decoding happens in place. */
  58. static char *
  59. decode_name (char *buf)
  60. {
  61. char *rp = buf;
  62. char *wp = buf;
  63. do
  64. if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
  65. {
  66. /* \040 is a SPACE. */
  67. *wp++ = ' ';
  68. rp += 3;
  69. }
  70. else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1')
  71. {
  72. /* \011 is a TAB. */
  73. *wp++ = '\t';
  74. rp += 3;
  75. }
  76. else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
  77. {
  78. /* \012 is a NEWLINE. */
  79. *wp++ = '\n';
  80. rp += 3;
  81. }
  82. else if (rp[0] == '\\' && rp[1] == '\\')
  83. {
  84. /* We have to escape \\ to be able to represent all characters. */
  85. *wp++ = '\\';
  86. rp += 1;
  87. }
  88. else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4')
  89. {
  90. /* \134 is also \\. */
  91. *wp++ = '\\';
  92. rp += 3;
  93. }
  94. else
  95. *wp++ = *rp;
  96. while (*rp++ != '\0');
  97. return buf;
  98. }
  99. /* Read one mount table entry from STREAM. Returns a pointer to storage
  100. reused on the next call, or null for EOF or error (use feof/ferror to
  101. check). */
  102. struct mntent *
  103. __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
  104. {
  105. char *cp;
  106. char *head;
  107. flockfile (stream);
  108. do
  109. {
  110. char *end_ptr;
  111. if (__fgets_unlocked (buffer, bufsiz, stream) == NULL)
  112. {
  113. funlockfile (stream);
  114. return NULL;
  115. }
  116. end_ptr = strchr (buffer, '\n');
  117. if (end_ptr != NULL) /* chop newline */
  118. {
  119. /* Do not walk past the start of buffer if it's all whitespace. */
  120. while (end_ptr != buffer
  121. && (end_ptr[-1] == ' ' || end_ptr[-1] == '\t'))
  122. end_ptr--;
  123. *end_ptr = '\0';
  124. }
  125. else
  126. {
  127. /* Not the whole line was read. Do it now but forget it. */
  128. char tmp[1024];
  129. while (__fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
  130. if (strchr (tmp, '\n') != NULL)
  131. break;
  132. }
  133. head = buffer + strspn (buffer, " \t");
  134. /* skip empty lines and comment lines: */
  135. }
  136. while (head[0] == '\0' || head[0] == '#');
  137. cp = __strsep (&head, " \t");
  138. mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
  139. if (head)
  140. head += strspn (head, " \t");
  141. cp = __strsep (&head, " \t");
  142. mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
  143. if (head)
  144. head += strspn (head, " \t");
  145. cp = __strsep (&head, " \t");
  146. mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
  147. if (head)
  148. head += strspn (head, " \t");
  149. cp = __strsep (&head, " \t");
  150. mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
  151. switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
  152. {
  153. case 0:
  154. mp->mnt_freq = 0;
  155. case 1:
  156. mp->mnt_passno = 0;
  157. case 2:
  158. break;
  159. }
  160. funlockfile (stream);
  161. return mp;
  162. }
  163. libc_hidden_def (__getmntent_r)
  164. weak_alias (__getmntent_r, getmntent_r)
  165. /* We have to use an encoding for names if they contain spaces or tabs.
  166. To be able to represent all characters we also have to escape the
  167. backslash itself. This "function" must be a macro since we use
  168. `alloca'. */
  169. #define encode_name(name) \
  170. do { \
  171. const char *rp = name; \
  172. \
  173. while (*rp != '\0') \
  174. if (*rp == ' ' || *rp == '\t' || *rp == '\n' || *rp == '\\') \
  175. break; \
  176. else \
  177. ++rp; \
  178. \
  179. if (*rp != '\0') \
  180. { \
  181. /* In the worst case the length of the string can increase to \
  182. four times the current length. */ \
  183. char *wp; \
  184. \
  185. rp = name; \
  186. name = wp = (char *) alloca (strlen (name) * 4 + 1); \
  187. \
  188. do \
  189. if (*rp == ' ') \
  190. { \
  191. *wp++ = '\\'; \
  192. *wp++ = '0'; \
  193. *wp++ = '4'; \
  194. *wp++ = '0'; \
  195. } \
  196. else if (*rp == '\t') \
  197. { \
  198. *wp++ = '\\'; \
  199. *wp++ = '0'; \
  200. *wp++ = '1'; \
  201. *wp++ = '1'; \
  202. } \
  203. else if (*rp == '\n') \
  204. { \
  205. *wp++ = '\\'; \
  206. *wp++ = '0'; \
  207. *wp++ = '1'; \
  208. *wp++ = '2'; \
  209. } \
  210. else if (*rp == '\\') \
  211. { \
  212. *wp++ = '\\'; \
  213. *wp++ = '\\'; \
  214. } \
  215. else \
  216. *wp++ = *rp; \
  217. while (*rp++ != '\0'); \
  218. } \
  219. } while (0)
  220. /* Write the mount table entry described by MNT to STREAM.
  221. Return zero on success, nonzero on failure. */
  222. int
  223. __addmntent (FILE *stream, const struct mntent *mnt)
  224. {
  225. struct mntent mntcopy = *mnt;
  226. if (fseek (stream, 0, SEEK_END))
  227. return 1;
  228. /* Encode spaces and tabs in the names. */
  229. encode_name (mntcopy.mnt_fsname);
  230. encode_name (mntcopy.mnt_dir);
  231. encode_name (mntcopy.mnt_type);
  232. encode_name (mntcopy.mnt_opts);
  233. return (fprintf (stream, "%s %s %s %s %d %d\n",
  234. mntcopy.mnt_fsname,
  235. mntcopy.mnt_dir,
  236. mntcopy.mnt_type,
  237. mntcopy.mnt_opts,
  238. mntcopy.mnt_freq,
  239. mntcopy.mnt_passno) < 0
  240. || fflush (stream) != 0);
  241. }
  242. weak_alias (__addmntent, addmntent)
  243. /* Search MNT->mnt_opts for an option matching OPT.
  244. Returns the address of the substring, or null if none found. */
  245. char *
  246. __hasmntopt (const struct mntent *mnt, const char *opt)
  247. {
  248. const size_t optlen = strlen (opt);
  249. char *rest = mnt->mnt_opts, *p;
  250. while ((p = strstr (rest, opt)) != NULL)
  251. {
  252. if ((p == rest || p[-1] == ',')
  253. && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
  254. return p;
  255. rest = strchr (p, ',');
  256. if (rest == NULL)
  257. break;
  258. ++rest;
  259. }
  260. return NULL;
  261. }
  262. libc_hidden_def (__hasmntopt)
  263. weak_alias (__hasmntopt, hasmntopt)