mntent.c 2.8 KB

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