1
0

mntent.c 1.6 KB

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