3
0

mountpoint.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 or later, see the file LICENSE in this tarball.
  8. *
  9. * Based on sysvinit's mountpoint
  10. */
  11. #include "libbb.h"
  12. int mountpoint_main(int argc, char **argv);
  13. int mountpoint_main(int argc, char **argv)
  14. {
  15. struct stat st;
  16. char *arg;
  17. int opt = getopt32(argv, "qdx");
  18. #define OPT_q (1)
  19. #define OPT_d (2)
  20. #define OPT_x (4)
  21. if (optind != argc - 1)
  22. bb_show_usage();
  23. arg = argv[optind];
  24. if ( (opt & OPT_x && stat(arg, &st) == 0) || (lstat(arg, &st) == 0) ) {
  25. if (opt & OPT_x) {
  26. if (S_ISBLK(st.st_mode)) {
  27. printf("%u:%u\n", major(st.st_rdev),
  28. minor(st.st_rdev));
  29. return EXIT_SUCCESS;
  30. } else {
  31. if (opt & OPT_q)
  32. putchar('\n');
  33. else
  34. bb_error_msg("%s: not a block device", arg);
  35. }
  36. return EXIT_FAILURE;
  37. } else
  38. if (S_ISDIR(st.st_mode)) {
  39. dev_t st_dev = st.st_dev;
  40. ino_t st_ino = st.st_ino;
  41. char *p = xasprintf("%s/..", arg);
  42. if (stat(p, &st) == 0) {
  43. int ret = (st_dev != st.st_dev) ||
  44. (st_dev == st.st_dev && st_ino == st.st_ino);
  45. if (opt & OPT_d)
  46. printf("%u:%u\n", major(st_dev), minor(st_dev));
  47. else if (!(opt & OPT_q))
  48. printf("%s is %sa mountpoint\n", arg, ret?"":"not ");
  49. return !ret;
  50. }
  51. } else {
  52. if (!(opt & OPT_q))
  53. bb_error_msg("%s: not a directory", arg);
  54. return EXIT_FAILURE;
  55. }
  56. }
  57. if (!(opt & OPT_q))
  58. bb_perror_msg("%s", arg);
  59. return EXIT_FAILURE;
  60. }