1
2
3
4
5
6
7
8
9
10
11
12#include "libbb.h"
13
14int mountpoint_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15int mountpoint_main(int argc UNUSED_PARAM, char **argv)
16{
17 struct stat st;
18 const char *msg;
19 char *arg;
20 int rc, opt;
21
22 opt_complementary = "=1";
23 opt = getopt32(argv, "qdxn");
24#define OPT_q (1)
25#define OPT_d (2)
26#define OPT_x (4)
27#define OPT_n (8)
28 arg = argv[optind];
29 msg = "%s";
30
31 rc = (opt & OPT_x) ? stat(arg, &st) : lstat(arg, &st);
32 if (rc != 0)
33 goto err;
34
35 if (opt & OPT_x) {
36 if (S_ISBLK(st.st_mode)) {
37 printf("%u:%u\n", major(st.st_rdev),
38 minor(st.st_rdev));
39 return EXIT_SUCCESS;
40 }
41 errno = 0;
42 msg = "%s: not a block device";
43 goto err;
44 }
45
46 errno = ENOTDIR;
47 if (S_ISDIR(st.st_mode)) {
48 dev_t st_dev = st.st_dev;
49 ino_t st_ino = st.st_ino;
50 char *p = xasprintf("%s/..", arg);
51
52 if (stat(p, &st) == 0) {
53
54 int is_not_mnt = (st_dev == st.st_dev) && (st_ino != st.st_ino);
55
56 if (opt & OPT_d)
57 printf("%u:%u\n", major(st_dev), minor(st_dev));
58 if (opt & OPT_n) {
59 const char *d = find_block_device(arg);
60
61
62 if (!d) {
63 d = "UNKNOWN";
64
65
66 }
67 printf("%s %s\n", d, arg);
68 }
69 if (!(opt & (OPT_q | OPT_d | OPT_n)))
70 printf("%s is %sa mountpoint\n", arg, is_not_mnt ? "not " : "");
71 return is_not_mnt;
72 }
73 arg = p;
74
75 }
76
77 err:
78 if (!(opt & OPT_q))
79 bb_perror_msg(msg, arg);
80 return EXIT_FAILURE;
81}
82