blob: 6265a20e17b81854222fba6c6592ee4c382f5d88 [file] [log] [blame]
Rob Landleyd00b3a52005-08-20 05:07:08 +00001/* vi: set sw=4 ts=4: */
2/*
3 * mountpoint implementation for busybox
4 *
5 * Copyright (C) 2005 Bernhard Fischer
6 *
Mike Frysingerf284c762006-04-16 20:38:26 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Rob Landleyd00b3a52005-08-20 05:07:08 +00008 *
9 * Based on sysvinit's mountpoint
10 */
11
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000012#include "busybox.h"
Rob Landleyd00b3a52005-08-20 05:07:08 +000013#include <sys/stat.h>
14#include <errno.h> /* errno */
15#include <string.h> /* strerror */
16#include <getopt.h> /* optind */
Rob Landleyd00b3a52005-08-20 05:07:08 +000017
18int mountpoint_main(int argc, char **argv)
19{
20 int opt = bb_getopt_ulflags(argc, argv, "qdx");
21#define OPT_q (1)
22#define OPT_d (2)
23#define OPT_x (4)
24
25 if (optind != argc - 1)
26 bb_show_usage();
27 {
28 char *arg = argv[optind];
29 struct stat st;
30
31 if ( (opt & OPT_x && stat(arg, &st) == 0) || (lstat(arg, &st) == 0) ) {
32 if (opt & OPT_x) {
33 if (S_ISBLK(st.st_mode))
34 {
35 bb_printf("%u:%u\n", major(st.st_rdev),
36 minor(st.st_rdev));
37 return EXIT_SUCCESS;
38 } else {
39 if (opt & OPT_q)
40 putchar('\n');
41 else
42 bb_error_msg("%s: not a block device", arg);
43 }
44 return EXIT_FAILURE;
45 } else
46 if (S_ISDIR(st.st_mode)) {
47 dev_t st_dev = st.st_dev;
48 ino_t st_ino = st.st_ino;
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000049 char *p = bb_xasprintf("%s/..", arg);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000050
Rob Landleyd00b3a52005-08-20 05:07:08 +000051 if (stat(p, &st) == 0) {
52 short ret = (st_dev != st.st_dev) ||
53 (st_dev == st.st_dev && st_ino == st.st_ino);
54 if (opt & OPT_d)
55 bb_printf("%u:%u\n", major(st_dev), minor(st_dev));
56 else if (!(opt & OPT_q))
57 bb_printf("%s is %sa mountpoint\n", arg, ret?"":"not ");
58 return !ret;
59 }
60 } else {
61 if (!(opt & OPT_q))
62 bb_error_msg("%s: not a directory", arg);
63 return EXIT_FAILURE;
64 }
65 }
66 if (!(opt & OPT_q))
Mike Frysinger948a09d2006-03-23 02:07:20 +000067 bb_perror_msg("%s", arg);
Rob Landleyd00b3a52005-08-20 05:07:08 +000068 return EXIT_FAILURE;
69 }
70}