find_mount_point.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "libbb.h"
  24. #include <mntent.h>
  25. /*
  26. * Given a block device, find the mount table entry if that block device
  27. * is mounted.
  28. *
  29. * Given any other file (or directory), find the mount table entry for its
  30. * filesystem.
  31. */
  32. extern struct mntent *find_mount_point(const char *name, const char *table)
  33. {
  34. struct stat s;
  35. dev_t mountDevice;
  36. FILE *mountTable;
  37. struct mntent *mountEntry;
  38. if (stat(name, &s) != 0)
  39. return 0;
  40. if ((s.st_mode & S_IFMT) == S_IFBLK)
  41. mountDevice = s.st_rdev;
  42. else
  43. mountDevice = s.st_dev;
  44. if ((mountTable = setmntent(table, "r")) == 0)
  45. return 0;
  46. while ((mountEntry = getmntent(mountTable)) != 0) {
  47. if (strcmp(name, mountEntry->mnt_dir) == 0
  48. || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
  49. break;
  50. if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
  51. break;
  52. if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
  53. break;
  54. }
  55. endmntent(mountTable);
  56. return mountEntry;
  57. }
  58. /* END CODE */
  59. /*
  60. Local Variables:
  61. c-file-style: "linux"
  62. c-basic-offset: 4
  63. tab-width: 4
  64. End:
  65. */