"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini losetup implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2002 Matt Kraai. |
| 6 | * |
Bernhard Reutner-Fischer | 4a1865c | 2006-01-13 18:11:59 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 12 | int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 13 | int losetup_main(int argc UNUSED_PARAM, char **argv) |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 14 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 15 | unsigned opt; |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 16 | int n; |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 17 | char *opt_o; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 18 | unsigned long long offset = 0; |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 19 | enum { |
| 20 | OPT_d = (1 << 0), |
| 21 | OPT_o = (1 << 1), |
| 22 | OPT_f = (1 << 2), |
| 23 | }; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 24 | |
Bernhard Reutner-Fischer | 1d4cd60 | 2008-07-21 15:29:36 +0000 | [diff] [blame] | 25 | /* max 2 args, all opts are mutually exclusive */ |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 26 | opt_complementary = "?2:d--of:o--df:f--do"; |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 27 | opt = getopt32(argv, "do:f", &opt_o); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 28 | argv += optind; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 29 | |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 30 | if (opt == OPT_o) |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 31 | offset = xatoull(opt_o); |
| 32 | |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 33 | if (opt == OPT_d) { |
| 34 | /* -d BLOCKDEV */ |
| 35 | if (!argv[0] || argv[1]) |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 36 | bb_show_usage(); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 37 | if (del_loop(argv[0])) |
Denis Vlasenko | 94e3365 | 2007-12-22 15:44:23 +0000 | [diff] [blame] | 38 | bb_simple_perror_msg_and_die(argv[0]); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 39 | return EXIT_SUCCESS; |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 42 | if (argv[0]) { |
| 43 | char *s; |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 44 | |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 45 | if (opt == OPT_f) /* -f should not have arguments */ |
| 46 | bb_show_usage(); |
| 47 | |
| 48 | if (argv[1]) { |
| 49 | /* [-o OFS] BLOCKDEV FILE */ |
| 50 | if (set_loop(&argv[0], argv[1], offset) < 0) |
| 51 | bb_simple_perror_msg_and_die(argv[0]); |
| 52 | return EXIT_SUCCESS; |
| 53 | } |
| 54 | /* [-o OFS] BLOCKDEV */ |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 55 | s = query_loop(argv[0]); |
Denis Vlasenko | c34d355 | 2007-04-19 00:09:34 +0000 | [diff] [blame] | 56 | if (!s) |
Denis Vlasenko | 94e3365 | 2007-12-22 15:44:23 +0000 | [diff] [blame] | 57 | bb_simple_perror_msg_and_die(argv[0]); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 58 | printf("%s: %s\n", argv[0], s); |
Denis Vlasenko | c34d355 | 2007-04-19 00:09:34 +0000 | [diff] [blame] | 59 | if (ENABLE_FEATURE_CLEAN_UP) |
| 60 | free(s); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 61 | return EXIT_SUCCESS; |
| 62 | } |
| 63 | |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 64 | /* [-o OFS|-f] with no params */ |
| 65 | n = 0; |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 66 | while (1) { |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 67 | char *s; |
Lauri Kasanen | 7c1b2b5 | 2010-07-04 00:04:03 +0200 | [diff] [blame] | 68 | char dev[LOOP_NAMESIZE]; |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 69 | |
Lauri Kasanen | 7c1b2b5 | 2010-07-04 00:04:03 +0200 | [diff] [blame] | 70 | sprintf(dev, LOOP_FORMAT, n); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 71 | s = query_loop(dev); |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 72 | n++; |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 73 | if (!s) { |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 74 | if (n > 9 && errno && errno != ENXIO) |
| 75 | return EXIT_SUCCESS; |
| 76 | if (opt == OPT_f) { |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 77 | puts(dev); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 78 | return EXIT_SUCCESS; |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 79 | } |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 80 | } else { |
Denys Vlasenko | 9251014 | 2010-05-19 00:39:17 +0200 | [diff] [blame] | 81 | if (opt != OPT_f) |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 82 | printf("%s: %s\n", dev, s); |
| 83 | if (ENABLE_FEATURE_CLEAN_UP) |
| 84 | free(s); |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 87 | return EXIT_SUCCESS; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 88 | } |