mntent.c 2.5 KB

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