mntent.c 2.4 KB

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