3
0

mountpoint.c 1.4 KB

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