mntent.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <malloc.h>
  10. #include <mntent.h>
  11. #include <bits/uClibc_mutex.h>
  12. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  13. libc_hidden_proto(getmntent_r)
  14. libc_hidden_proto(setmntent)
  15. libc_hidden_proto(endmntent)
  16. libc_hidden_proto(strstr)
  17. libc_hidden_proto(strtok_r)
  18. libc_hidden_proto(atoi)
  19. libc_hidden_proto(fopen)
  20. libc_hidden_proto(fclose)
  21. libc_hidden_proto(fseek)
  22. libc_hidden_proto(fgets)
  23. libc_hidden_proto(abort)
  24. libc_hidden_proto(fprintf)
  25. libc_hidden_proto(__uc_malloc)
  26. /* Reentrant version of getmntent. */
  27. struct mntent *getmntent_r (FILE *filep,
  28. struct mntent *mnt, char *buff, int bufsize)
  29. {
  30. char *cp, *ptrptr;
  31. const char *sep = " \t\n";
  32. if (!filep || !mnt || !buff)
  33. return NULL;
  34. /* Loop on the file, skipping comment lines. - FvK 03/07/93 */
  35. while ((cp = fgets(buff, bufsize, filep)) != NULL) {
  36. if (buff[0] == '#' || buff[0] == '\n')
  37. continue;
  38. break;
  39. }
  40. /* At the EOF, the buffer should be unchanged. We should
  41. * check the return value from fgets ().
  42. */
  43. if (cp == NULL)
  44. return NULL;
  45. ptrptr = 0;
  46. mnt->mnt_fsname = strtok_r(buff, sep, &ptrptr);
  47. if (mnt->mnt_fsname == NULL)
  48. return NULL;
  49. mnt->mnt_dir = strtok_r(NULL, sep, &ptrptr);
  50. if (mnt->mnt_dir == NULL)
  51. return NULL;
  52. mnt->mnt_type = strtok_r(NULL, sep, &ptrptr);
  53. if (mnt->mnt_type == NULL)
  54. return NULL;
  55. mnt->mnt_opts = strtok_r(NULL, sep, &ptrptr);
  56. if (mnt->mnt_opts == NULL)
  57. mnt->mnt_opts = "";
  58. cp = strtok_r(NULL, sep, &ptrptr);
  59. mnt->mnt_freq = (cp != NULL) ? atoi(cp) : 0;
  60. cp = strtok_r(NULL, sep, &ptrptr);
  61. mnt->mnt_passno = (cp != NULL) ? atoi(cp) : 0;
  62. return mnt;
  63. }
  64. libc_hidden_def(getmntent_r)
  65. struct mntent *getmntent(FILE * filep)
  66. {
  67. struct mntent *tmp;
  68. static char *buff = NULL;
  69. static struct mntent mnt;
  70. __UCLIBC_MUTEX_LOCK(mylock);
  71. if (!buff) {
  72. buff = __uc_malloc(BUFSIZ);
  73. }
  74. tmp = getmntent_r(filep, &mnt, buff, BUFSIZ);
  75. __UCLIBC_MUTEX_UNLOCK(mylock);
  76. return(tmp);
  77. }
  78. int addmntent(FILE * filep, const struct mntent *mnt)
  79. {
  80. if (fseek(filep, 0, SEEK_END) < 0)
  81. return 1;
  82. return (fprintf (filep, "%s %s %s %s %d %d\n", mnt->mnt_fsname, mnt->mnt_dir,
  83. mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 0 ? 1 : 0);
  84. }
  85. char *hasmntopt(const struct mntent *mnt, const char *opt)
  86. {
  87. return strstr(mnt->mnt_opts, opt);
  88. }
  89. FILE *setmntent(const char *name, const char *mode)
  90. {
  91. return fopen(name, mode);
  92. }
  93. libc_hidden_def(setmntent)
  94. int endmntent(FILE * filep)
  95. {
  96. if (filep != NULL)
  97. fclose(filep);
  98. return 1;
  99. }
  100. libc_hidden_def(endmntent)