mntent.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <mntent.h>
  4. #include <errno.h>
  5. FILE *setmntent(const char *name, const char *mode)
  6. {
  7. return fopen(name, mode);
  8. }
  9. int endmntent(FILE *f)
  10. {
  11. if (f) fclose(f);
  12. return 1;
  13. }
  14. struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
  15. {
  16. int cnt, n[8];
  17. mnt->mnt_freq = 0;
  18. mnt->mnt_passno = 0;
  19. do {
  20. fgets(linebuf, buflen, f);
  21. if (feof(f) || ferror(f)) return 0;
  22. if (!strchr(linebuf, '\n')) {
  23. fscanf(f, "%*[^\n]%*[\n]");
  24. errno = ERANGE;
  25. return 0;
  26. }
  27. cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
  28. n, n+1, n+2, n+3, n+4, n+5, n+6, n+7,
  29. &mnt->mnt_freq, &mnt->mnt_passno);
  30. } while (cnt < 2 || linebuf[n[0]] == '#');
  31. linebuf[n[1]] = 0;
  32. linebuf[n[3]] = 0;
  33. linebuf[n[5]] = 0;
  34. linebuf[n[7]] = 0;
  35. mnt->mnt_fsname = linebuf+n[0];
  36. mnt->mnt_dir = linebuf+n[2];
  37. mnt->mnt_type = linebuf+n[4];
  38. mnt->mnt_opts = linebuf+n[6];
  39. return mnt;
  40. }
  41. struct mntent *getmntent(FILE *f)
  42. {
  43. static char linebuf[256];
  44. static struct mntent mnt;
  45. return getmntent_r(f, &mnt, linebuf, sizeof linebuf);
  46. }
  47. int addmntent(FILE *f, const struct mntent *mnt)
  48. {
  49. if (fseek(f, 0, SEEK_END)) return 1;
  50. return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n",
  51. mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts,
  52. mnt->mnt_freq, mnt->mnt_passno) < 0;
  53. }
  54. char *hasmntopt(const struct mntent *mnt, const char *opt)
  55. {
  56. return strstr(mnt->mnt_opts, opt);
  57. }