mntent.c 2.4 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. #endif
  11. #define LOCK __pthread_mutex_lock(&mylock)
  12. #define UNLOCK __pthread_mutex_unlock(&mylock)
  13. /* Reentrant version of getmntent. */
  14. struct mntent attribute_hidden *__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. strong_alias(__getmntent_r,getmntent_r)
  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 attribute_hidden *__setmntent(const char *name, const char *mode)
  81. {
  82. return fopen(name, mode);
  83. }
  84. strong_alias(__setmntent,setmntent)
  85. int attribute_hidden __endmntent(FILE * filep)
  86. {
  87. if (filep != NULL)
  88. fclose(filep);
  89. return 1;
  90. }
  91. strong_alias(__endmntent,endmntent)