"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> |
| 11 | #include <stdlib.h> |
| 12 | |
| 13 | #include "busybox.h" |
| 14 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 15 | int losetup_main(int argc, char **argv); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 16 | int losetup_main(int argc, char **argv) |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 17 | { |
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 | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 20 | unsigned long long offset = 0; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 22 | opt = getopt32(argc, argv, "do:", &opt_o); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 23 | argc -= optind; |
| 24 | argv += optind; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 26 | if (opt == 0x3) // -d + -o (illegal) |
| 27 | bb_show_usage(); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 28 | |
| 29 | if (opt == 0x1) { // -d |
| 30 | /* detach takes exactly one argument */ |
| 31 | if (argc != 1) |
| 32 | bb_show_usage(); |
| 33 | if (!del_loop(argv[0])) |
| 34 | return EXIT_SUCCESS; |
| 35 | bb_perror_nomsg_and_die(); |
| 36 | } |
| 37 | |
| 38 | if (opt == 0x2) // -o |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 39 | offset = xatoull(opt_o); |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 40 | |
| 41 | /* -o or no option */ |
| 42 | |
| 43 | if (argc == 2) { |
| 44 | if (set_loop(&argv[0], argv[1], offset) < 0) |
| 45 | bb_perror_nomsg_and_die(); |
| 46 | } else if (argc == 1) { |
| 47 | char *s = query_loop(argv[0]); |
| 48 | if (!s) bb_perror_nomsg_and_die(); |
| 49 | printf("%s: %s\n", argv[0], s); |
| 50 | if (ENABLE_FEATURE_CLEAN_UP) free(s); |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 51 | } else { |
Denis Vlasenko | 0b193a7 | 2006-09-29 21:47:11 +0000 | [diff] [blame] | 52 | char dev[sizeof(LOOP_NAME"0")] = LOOP_NAME"0"; |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 53 | char c; |
| 54 | for (c = '0'; c <= '9'; ++c) { |
| 55 | char *s; |
Denis Vlasenko | 0b193a7 | 2006-09-29 21:47:11 +0000 | [diff] [blame] | 56 | dev[sizeof(LOOP_NAME"0")-2] = c; |
Denis Vlasenko | 956a569 | 2006-09-27 14:51:27 +0000 | [diff] [blame] | 57 | s = query_loop(dev); |
| 58 | if (s) { |
| 59 | printf("%s: %s\n", dev, s); |
| 60 | if (ENABLE_FEATURE_CLEAN_UP) free(s); |
| 61 | } |
| 62 | } |
| 63 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 64 | return EXIT_SUCCESS; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 65 | } |