3
0

mtab.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include <mntent.h>
  10. #include "libbb.h"
  11. #if ENABLE_FEATURE_MTAB_SUPPORT
  12. void erase_mtab(const char *name)
  13. {
  14. struct mntent *entries = NULL;
  15. int i, count = 0;
  16. FILE *mountTable;
  17. struct mntent *m;
  18. mountTable = setmntent(bb_path_mtab_file, "r");
  19. /* Bummer. Fall back on trying the /proc filesystem */
  20. if (!mountTable) mountTable = setmntent("/proc/mounts", "r");
  21. if (!mountTable) {
  22. bb_perror_msg(bb_path_mtab_file);
  23. return;
  24. }
  25. while ((m = getmntent(mountTable)) != 0) {
  26. i = count++;
  27. entries = xrealloc(entries, count * sizeof(entries[0]));
  28. entries[i].mnt_fsname = xstrdup(m->mnt_fsname);
  29. entries[i].mnt_dir = xstrdup(m->mnt_dir);
  30. entries[i].mnt_type = xstrdup(m->mnt_type);
  31. entries[i].mnt_opts = xstrdup(m->mnt_opts);
  32. entries[i].mnt_freq = m->mnt_freq;
  33. entries[i].mnt_passno = m->mnt_passno;
  34. }
  35. endmntent(mountTable);
  36. mountTable = setmntent(bb_path_mtab_file, "w");
  37. if (mountTable) {
  38. for (i = 0; i < count; i++) {
  39. if (strcmp(entries[i].mnt_fsname, name) != 0
  40. && strcmp(entries[i].mnt_dir, name) != 0)
  41. addmntent(mountTable, &entries[i]);
  42. }
  43. endmntent(mountTable);
  44. } else if (errno != EROFS)
  45. bb_perror_msg(bb_path_mtab_file);
  46. }
  47. #endif