mntent.c 2.2 KB

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