blob: b5ce35cf1441c4b4e89de003ed63d819c0e34eb3 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Rob Landleyd00b3a52005-08-20 05:07:08 +000013
Denis Vlasenko06af2162007-02-03 17:28:39 +000014int mountpoint_main(int argc, char **argv);
Rob Landleyd00b3a52005-08-20 05:07:08 +000015int mountpoint_main(int argc, char **argv)
16{
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000017 struct stat st;
18 char *arg;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000019 int opt = getopt32(argc, argv, "qdx");
Rob Landleyd00b3a52005-08-20 05:07:08 +000020#define OPT_q (1)
21#define OPT_d (2)
22#define OPT_x (4)
23
24 if (optind != argc - 1)
25 bb_show_usage();
Rob Landleyd00b3a52005-08-20 05:07:08 +000026
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000027 arg = argv[optind];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000028
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000029 if ( (opt & OPT_x && stat(arg, &st) == 0) || (lstat(arg, &st) == 0) ) {
30 if (opt & OPT_x) {
31 if (S_ISBLK(st.st_mode)) {
32 printf("%u:%u\n", major(st.st_rdev),
33 minor(st.st_rdev));
34 return EXIT_SUCCESS;
Rob Landleyd00b3a52005-08-20 05:07:08 +000035 } else {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000036 if (opt & OPT_q)
37 putchar('\n');
38 else
39 bb_error_msg("%s: not a block device", arg);
Rob Landleyd00b3a52005-08-20 05:07:08 +000040 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000041 return EXIT_FAILURE;
42 } else
43 if (S_ISDIR(st.st_mode)) {
44 dev_t st_dev = st.st_dev;
45 ino_t st_ino = st.st_ino;
46 char *p = xasprintf("%s/..", arg);
47
48 if (stat(p, &st) == 0) {
49 int ret = (st_dev != st.st_dev) ||
50 (st_dev == st.st_dev && st_ino == st.st_ino);
51 if (opt & OPT_d)
52 printf("%u:%u\n", major(st_dev), minor(st_dev));
53 else if (!(opt & OPT_q))
54 printf("%s is %sa mountpoint\n", arg, ret?"":"not ");
55 return !ret;
56 }
57 } else {
58 if (!(opt & OPT_q))
59 bb_error_msg("%s: not a directory", arg);
60 return EXIT_FAILURE;
Rob Landleyd00b3a52005-08-20 05:07:08 +000061 }
Rob Landleyd00b3a52005-08-20 05:07:08 +000062 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000063 if (!(opt & OPT_q))
64 bb_perror_msg("%s", arg);
65 return EXIT_FAILURE;
Rob Landleyd00b3a52005-08-20 05:07:08 +000066}