mntent.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <mntent.h>
  5. /* Reentrant version of the above function. */
  6. struct mntent *getmntent_r (FILE *filep,
  7. struct mntent *mnt, char *buff, int bufsize)
  8. {
  9. char *cp, *sep = " \t\n";
  10. static char *ptrptr = 0;
  11. if (!filep || !mnt || !buff)
  12. return NULL;
  13. /* Loop on the file, skipping comment lines. - FvK 03/07/93 */
  14. while ((cp = fgets(buff, bufsize, filep)) != NULL) {
  15. if (buff[0] == '#' || buff[0] == '\n')
  16. continue;
  17. break;
  18. }
  19. /* At the EOF, the buffer should be unchanged. We should
  20. * check the return value from fgets ().
  21. */
  22. if (cp == NULL)
  23. return NULL;
  24. ptrptr = 0;
  25. mnt->mnt_fsname = strtok_r(buff, sep, &ptrptr);
  26. if (mnt->mnt_fsname == NULL)
  27. return NULL;
  28. mnt->mnt_dir = strtok_r(NULL, sep, &ptrptr);
  29. if (mnt->mnt_dir == NULL)
  30. return NULL;
  31. mnt->mnt_type = strtok_r(NULL, sep, &ptrptr);
  32. if (mnt->mnt_type == NULL)
  33. return NULL;
  34. mnt->mnt_opts = strtok_r(NULL, sep, &ptrptr);
  35. if (mnt->mnt_opts == NULL)
  36. mnt->mnt_opts = "";
  37. cp = strtok_r(NULL, sep, &ptrptr);
  38. mnt->mnt_freq = (cp != NULL) ? atoi(cp) : 0;
  39. cp = strtok_r(NULL, sep, &ptrptr);
  40. mnt->mnt_passno = (cp != NULL) ? atoi(cp) : 0;
  41. return mnt;
  42. }
  43. struct mntent *getmntent(FILE * filep)
  44. {
  45. static char buff[BUFSIZ];
  46. static struct mntent mnt;
  47. return(getmntent_r(filep, &mnt, buff, sizeof buff));
  48. }
  49. int addmntent(FILE * filep, const struct mntent *mnt)
  50. {
  51. if (fseek(filep, 0, SEEK_END) < 0)
  52. return 1;
  53. if (fprintf (filep, "%s %s %s %s %d %d\n", mnt->mnt_fsname, mnt->mnt_dir,
  54. mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 1)
  55. return 1;
  56. return 0;
  57. }
  58. char *hasmntopt(const struct mntent *mnt, const char *opt)
  59. {
  60. return strstr(mnt->mnt_opts, opt);
  61. }
  62. FILE *setmntent(const char *name, const char *mode)
  63. {
  64. return fopen(name, mode);
  65. }
  66. int endmntent(FILE * filep)
  67. {
  68. if (filep != NULL)
  69. fclose(filep);
  70. return 1;
  71. }