mountpoint.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * mountpoint implementation for busybox
  4. *
  5. * Copyright (C) 2005 Bernhard Fischer
  6. *
  7. * Licensed under the GPL v2, see the file LICENSE in this tarball.
  8. *
  9. * Based on sysvinit's mountpoint
  10. */
  11. #include <sys/stat.h>
  12. #include <errno.h> /* errno */
  13. #include <string.h> /* strerror */
  14. #include <getopt.h> /* optind */
  15. #include "busybox.h"
  16. int mountpoint_main(int argc, char **argv)
  17. {
  18. int opt = bb_getopt_ulflags(argc, argv, "qdx");
  19. #define OPT_q (1)
  20. #define OPT_d (2)
  21. #define OPT_x (4)
  22. if (optind != argc - 1)
  23. bb_show_usage();
  24. {
  25. char *arg = argv[optind];
  26. struct stat st;
  27. if ( (opt & OPT_x && stat(arg, &st) == 0) || (lstat(arg, &st) == 0) ) {
  28. if (opt & OPT_x) {
  29. if (S_ISBLK(st.st_mode))
  30. {
  31. bb_printf("%u:%u\n", major(st.st_rdev),
  32. minor(st.st_rdev));
  33. return EXIT_SUCCESS;
  34. } else {
  35. if (opt & OPT_q)
  36. putchar('\n');
  37. else
  38. bb_error_msg("%s: not a block device", arg);
  39. }
  40. return EXIT_FAILURE;
  41. } else
  42. if (S_ISDIR(st.st_mode)) {
  43. dev_t st_dev = st.st_dev;
  44. ino_t st_ino = st.st_ino;
  45. char *p = bb_xasprintf("%s/..", arg);
  46. if (stat(p, &st) == 0) {
  47. short ret = (st_dev != st.st_dev) ||
  48. (st_dev == st.st_dev && st_ino == st.st_ino);
  49. if (opt & OPT_d)
  50. bb_printf("%u:%u\n", major(st_dev), minor(st_dev));
  51. else if (!(opt & OPT_q))
  52. bb_printf("%s is %sa mountpoint\n", arg, ret?"":"not ");
  53. return !ret;
  54. }
  55. } else {
  56. if (!(opt & OPT_q))
  57. bb_error_msg("%s: not a directory", arg);
  58. return EXIT_FAILURE;
  59. }
  60. }
  61. if (!(opt & OPT_q))
  62. bb_perror_msg("%s", arg);
  63. return EXIT_FAILURE;
  64. }
  65. }