mntent.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #endif
  11. #define LOCK __pthread_mutex_lock(&mylock)
  12. #define UNLOCK __pthread_mutex_unlock(&mylock)
  13. /* Reentrant version of getmntent. */
  14. struct mntent *getmntent_r (FILE *filep,
  15. struct mntent *mnt, char *buff, int bufsize)
  16. {
  17. char *cp, *ptrptr;
  18. const char *sep = " \t\n";
  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. struct mntent *getmntent(FILE * filep)
  52. {
  53. struct mntent *tmp;
  54. static char *buff = NULL;
  55. static struct mntent mnt;
  56. LOCK;
  57. if (!buff) {
  58. buff = malloc(BUFSIZ);
  59. if (!buff)
  60. abort();
  61. }
  62. tmp = getmntent_r(filep, &mnt, buff, BUFSIZ);
  63. UNLOCK;
  64. return(tmp);
  65. }
  66. int addmntent(FILE * filep, const struct mntent *mnt)
  67. {
  68. if (fseek(filep, 0, SEEK_END) < 0)
  69. return 1;
  70. if (fprintf (filep, "%s %s %s %s %d %d\n", mnt->mnt_fsname, mnt->mnt_dir,
  71. mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 1)
  72. return 1;
  73. return 0;
  74. }
  75. char *hasmntopt(const struct mntent *mnt, const char *opt)
  76. {
  77. return strstr(mnt->mnt_opts, opt);
  78. }
  79. FILE *setmntent(const char *name, const char *mode)
  80. {
  81. return fopen(name, mode);
  82. }
  83. int endmntent(FILE * filep)
  84. {
  85. if (filep != NULL)
  86. fclose(filep);
  87. return 1;
  88. }