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