mntent.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. * Copyright (C) 1995-2020 Free Software Foundation, Inc.
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <mntent.h>
  11. #include <bits/uClibc_mutex.h>
  12. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  13. /* Reentrant version of getmntent. */
  14. struct mntent *getmntent_r (FILE *filep,
  15. struct mntent *mnt, char *buff, int bufsize)
  16. {
  17. static const char sep[] = " \t\n";
  18. char *cp, *ptrptr;
  19. if (!filep || !mnt || !buff)
  20. return NULL;
  21. /* Loop on the file, skipping comment lines. - FvK 03/07/93 */
  22. while ((cp = fgets(buff, bufsize, filep)) != NULL) {
  23. if (buff[0] == '#' || buff[0] == '\n')
  24. continue;
  25. break;
  26. }
  27. /* At the EOF, the buffer should be unchanged. We should
  28. * check the return value from fgets ().
  29. */
  30. if (cp == NULL)
  31. return NULL;
  32. ptrptr = 0;
  33. mnt->mnt_fsname = strtok_r(buff, sep, &ptrptr);
  34. if (mnt->mnt_fsname == NULL)
  35. return NULL;
  36. mnt->mnt_dir = strtok_r(NULL, sep, &ptrptr);
  37. if (mnt->mnt_dir == NULL)
  38. return NULL;
  39. mnt->mnt_type = strtok_r(NULL, sep, &ptrptr);
  40. if (mnt->mnt_type == NULL)
  41. return NULL;
  42. mnt->mnt_opts = strtok_r(NULL, sep, &ptrptr);
  43. if (mnt->mnt_opts == NULL)
  44. mnt->mnt_opts = "";
  45. cp = strtok_r(NULL, sep, &ptrptr);
  46. mnt->mnt_freq = (cp != NULL) ? atoi(cp) : 0;
  47. cp = strtok_r(NULL, sep, &ptrptr);
  48. mnt->mnt_passno = (cp != NULL) ? atoi(cp) : 0;
  49. return mnt;
  50. }
  51. libc_hidden_def(getmntent_r)
  52. struct mntent *getmntent(FILE * filep)
  53. {
  54. struct mntent *tmp;
  55. static char *buff = NULL;
  56. static struct mntent mnt;
  57. __UCLIBC_MUTEX_LOCK(mylock);
  58. if (!buff) {
  59. buff = malloc(BUFSIZ);
  60. if (!buff)
  61. abort();
  62. }
  63. tmp = getmntent_r(filep, &mnt, buff, BUFSIZ);
  64. __UCLIBC_MUTEX_UNLOCK(mylock);
  65. return(tmp);
  66. }
  67. int addmntent(FILE * filep, const struct mntent *mnt)
  68. {
  69. if (fseek(filep, 0, SEEK_END) < 0)
  70. return 1;
  71. return (fprintf (filep, "%s %s %s %s %d %d\n", mnt->mnt_fsname, mnt->mnt_dir,
  72. mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 0 ? 1 : 0);
  73. }
  74. /* Search MNT->mnt_opts for an option matching OPT.
  75. Returns the address of the substring, or null if none found. */
  76. char *hasmntopt (const struct mntent *mnt, const char *opt)
  77. {
  78. const size_t optlen = strlen(opt);
  79. char *rest = mnt->mnt_opts, *p;
  80. while ((p = strstr(rest, opt)) != NULL) {
  81. if ((p == rest || p[-1] == ',')
  82. && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
  83. return p;
  84. rest = strchr(p, ',');
  85. if (rest == NULL)
  86. break;
  87. ++rest;
  88. }
  89. return NULL;
  90. }
  91. FILE *setmntent(const char *name, const char *mode)
  92. {
  93. return fopen(name, mode);
  94. }
  95. libc_hidden_def(setmntent)
  96. int endmntent(FILE * filep)
  97. {
  98. if (filep != NULL)
  99. fclose(filep);
  100. return 1;
  101. }
  102. libc_hidden_def(endmntent)