mntent.c 2.3 KB

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