Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * mountpoint implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2005 Bernhard Fischer |
| 6 | * |
Mike Frysinger | f284c76 | 2006-04-16 20:38:26 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 8 | * |
| 9 | * Based on sysvinit's mountpoint |
| 10 | */ |
| 11 | |
Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 12 | #include "busybox.h" |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 13 | |
| 14 | int mountpoint_main(int argc, char **argv) |
| 15 | { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 16 | struct stat st; |
| 17 | char *arg; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 18 | int opt = getopt32(argc, argv, "qdx"); |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 19 | #define OPT_q (1) |
| 20 | #define OPT_d (2) |
| 21 | #define OPT_x (4) |
| 22 | |
| 23 | if (optind != argc - 1) |
| 24 | bb_show_usage(); |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 26 | arg = argv[optind]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 28 | if ( (opt & OPT_x && stat(arg, &st) == 0) || (lstat(arg, &st) == 0) ) { |
| 29 | if (opt & OPT_x) { |
| 30 | if (S_ISBLK(st.st_mode)) { |
| 31 | printf("%u:%u\n", major(st.st_rdev), |
| 32 | minor(st.st_rdev)); |
| 33 | return EXIT_SUCCESS; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 34 | } else { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 35 | if (opt & OPT_q) |
| 36 | putchar('\n'); |
| 37 | else |
| 38 | bb_error_msg("%s: not a block device", arg); |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 39 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 40 | return EXIT_FAILURE; |
| 41 | } else |
| 42 | if (S_ISDIR(st.st_mode)) { |
| 43 | dev_t st_dev = st.st_dev; |
| 44 | ino_t st_ino = st.st_ino; |
| 45 | char *p = xasprintf("%s/..", arg); |
| 46 | |
| 47 | if (stat(p, &st) == 0) { |
| 48 | int ret = (st_dev != st.st_dev) || |
| 49 | (st_dev == st.st_dev && st_ino == st.st_ino); |
| 50 | if (opt & OPT_d) |
| 51 | printf("%u:%u\n", major(st_dev), minor(st_dev)); |
| 52 | else if (!(opt & OPT_q)) |
| 53 | printf("%s is %sa mountpoint\n", arg, ret?"":"not "); |
| 54 | return !ret; |
| 55 | } |
| 56 | } else { |
| 57 | if (!(opt & OPT_q)) |
| 58 | bb_error_msg("%s: not a directory", arg); |
| 59 | return EXIT_FAILURE; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 60 | } |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 61 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 62 | if (!(opt & OPT_q)) |
| 63 | bb_perror_msg("%s", arg); |
| 64 | return EXIT_FAILURE; |
Rob Landley | d00b3a5 | 2005-08-20 05:07:08 +0000 | [diff] [blame] | 65 | } |