mntent.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #define strtok_r __strtok_r
  2. #define strstr __strstr
  3. #define atoi __atoi
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <mntent.h>
  8. #ifdef __UCLIBC_HAS_THREADS__
  9. # include <pthread.h>
  10. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  11. #endif
  12. #define LOCK __pthread_mutex_lock(&mylock)
  13. #define UNLOCK __pthread_mutex_unlock(&mylock)
  14. /* Reentrant version of getmntent. */
  15. struct mntent attribute_hidden *__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. strong_alias(__getmntent_r,getmntent_r)
  53. struct mntent *getmntent(FILE * filep)
  54. {
  55. struct mntent *tmp;
  56. static char *buff = NULL;
  57. static struct mntent mnt;
  58. LOCK;
  59. if (!buff) {
  60. buff = malloc(BUFSIZ);
  61. if (!buff)
  62. abort();
  63. }
  64. tmp = __getmntent_r(filep, &mnt, buff, BUFSIZ);
  65. UNLOCK;
  66. return(tmp);
  67. }
  68. int addmntent(FILE * filep, const struct mntent *mnt)
  69. {
  70. if (fseek(filep, 0, SEEK_END) < 0)
  71. return 1;
  72. if (fprintf (filep, "%s %s %s %s %d %d\n", mnt->mnt_fsname, mnt->mnt_dir,
  73. mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 1)
  74. return 1;
  75. return 0;
  76. }
  77. char *hasmntopt(const struct mntent *mnt, const char *opt)
  78. {
  79. return strstr(mnt->mnt_opts, opt);
  80. }
  81. FILE attribute_hidden *__setmntent(const char *name, const char *mode)
  82. {
  83. return fopen(name, mode);
  84. }
  85. strong_alias(__setmntent,setmntent)
  86. int attribute_hidden __endmntent(FILE * filep)
  87. {
  88. if (filep != NULL)
  89. fclose(filep);
  90. return 1;
  91. }
  92. strong_alias(__endmntent,endmntent)