mntent.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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[BUFSIZ];
  56. static struct mntent mnt;
  57. LOCK;
  58. tmp = getmntent_r(filep, &mnt, buff, sizeof buff);
  59. UNLOCK;
  60. return(tmp);
  61. }
  62. int addmntent(FILE * filep, const struct mntent *mnt)
  63. {
  64. if (fseek(filep, 0, SEEK_END) < 0)
  65. return 1;
  66. if (fprintf (filep, "%s %s %s %s %d %d\n", mnt->mnt_fsname, mnt->mnt_dir,
  67. mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 1)
  68. return 1;
  69. return 0;
  70. }
  71. char *hasmntopt(const struct mntent *mnt, const char *opt)
  72. {
  73. return strstr(mnt->mnt_opts, opt);
  74. }
  75. FILE *setmntent(const char *name, const char *mode)
  76. {
  77. return fopen(name, mode);
  78. }
  79. int endmntent(FILE * filep)
  80. {
  81. if (filep != NULL)
  82. fclose(filep);
  83. return 1;
  84. }