mntent.c 1.5 KB

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