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