mntent.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. ptrptr = 0;
  29. mnt->mnt_dir = strtok_r(NULL, sep, &ptrptr);
  30. if (mnt->mnt_dir == NULL)
  31. return NULL;
  32. ptrptr = 0;
  33. mnt->mnt_type = strtok_r(NULL, sep, &ptrptr);
  34. if (mnt->mnt_type == NULL)
  35. return NULL;
  36. ptrptr = 0;
  37. mnt->mnt_opts = strtok_r(NULL, sep, &ptrptr);
  38. if (mnt->mnt_opts == NULL)
  39. mnt->mnt_opts = "";
  40. ptrptr = 0;
  41. cp = strtok_r(NULL, sep, &ptrptr);
  42. mnt->mnt_freq = (cp != NULL) ? atoi(cp) : 0;
  43. ptrptr = 0;
  44. cp = strtok_r(NULL, sep, &ptrptr);
  45. mnt->mnt_passno = (cp != NULL) ? atoi(cp) : 0;
  46. return mnt;
  47. }
  48. struct mntent *getmntent(FILE * filep)
  49. {
  50. static char buff[BUFSIZ];
  51. static struct mntent mnt;
  52. return(getmntent_r(filep, &mnt, buff, sizeof buff));
  53. }
  54. int addmntent(FILE * filep, const struct mntent *mnt)
  55. {
  56. if (fseek(filep, 0, SEEK_END) < 0)
  57. return 1;
  58. if (fprintf (filep, "%s %s %s %s %d %d\n", mnt->mnt_fsname, mnt->mnt_dir,
  59. mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 1)
  60. return 1;
  61. return 0;
  62. }
  63. char *hasmntopt(const struct mntent *mnt, const char *opt)
  64. {
  65. return strstr(mnt->mnt_opts, opt);
  66. }
  67. FILE *setmntent(const char *name, const char *mode)
  68. {
  69. return fopen(name, mode);
  70. }
  71. int endmntent(FILE * filep)
  72. {
  73. if (filep != NULL)
  74. fclose(filep);
  75. return 1;
  76. }