mntent.c 2.5 KB

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