"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; |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 13 | int losetup_main(int argc, char **argv) |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 14 | { |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 15 | char dev[] = LOOP_NAME"0"; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 16 | unsigned opt; |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 17 | char *opt_o; |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 18 | char *s; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 19 | unsigned long long offset = 0; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 21 | /* max 2 args, all opts are mutially exclusive */ |
| 22 | opt_complementary = "?2:d--of:o--df:f-do"; |
| 23 | opt = getopt32(argv, "do:f", &opt_o); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 24 | argc -= optind; |
| 25 | argv += optind; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 27 | if (opt == 0x2) // -o |
| 28 | offset = xatoull(opt_o); |
| 29 | |
| 30 | if (opt == 0x4 && argc) // -f does not take any argument |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 31 | bb_show_usage(); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 32 | |
| 33 | if (opt == 0x1) { // -d |
| 34 | /* detach takes exactly one argument */ |
| 35 | if (argc != 1) |
| 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 | |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 42 | if (argc == 2) { |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 43 | /* -o or no option */ |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 44 | if (set_loop(&argv[0], argv[1], offset) < 0) |
Denis Vlasenko | 94e3365 | 2007-12-22 15:44:23 +0000 | [diff] [blame] | 45 | bb_simple_perror_msg_and_die(argv[0]); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 46 | return EXIT_SUCCESS; |
| 47 | } |
| 48 | |
| 49 | if (argc == 1) { |
| 50 | /* -o or no option */ |
| 51 | s = query_loop(argv[0]); |
Denis Vlasenko | c34d355 | 2007-04-19 00:09:34 +0000 | [diff] [blame] | 52 | if (!s) |
Denis Vlasenko | 94e3365 | 2007-12-22 15:44:23 +0000 | [diff] [blame] | 53 | bb_simple_perror_msg_and_die(argv[0]); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 54 | printf("%s: %s\n", argv[0], s); |
Denis Vlasenko | c34d355 | 2007-04-19 00:09:34 +0000 | [diff] [blame] | 55 | if (ENABLE_FEATURE_CLEAN_UP) |
| 56 | free(s); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 57 | return EXIT_SUCCESS; |
| 58 | } |
| 59 | |
| 60 | /* -o, -f or no option */ |
| 61 | while (1) { |
| 62 | s = query_loop(dev); |
| 63 | if (!s) { |
| 64 | if (opt == 0x4) { |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 65 | puts(dev); |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 66 | return EXIT_SUCCESS; |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 67 | } |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 68 | } else { |
| 69 | if (opt != 0x4) |
| 70 | printf("%s: %s\n", dev, s); |
| 71 | if (ENABLE_FEATURE_CLEAN_UP) |
| 72 | free(s); |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 73 | } |
Denis Vlasenko | 7ae209c | 2007-09-26 17:54:18 +0000 | [diff] [blame] | 74 | |
| 75 | if (++dev[sizeof(dev) - 2] > '9') |
| 76 | break; |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 77 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 78 | return EXIT_SUCCESS; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 79 | } |