diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-26 17:54:18 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-26 17:54:18 +0000 |
commit | 7ae209c0190c0c6e6b1c95ee9005edf4729e1ce0 (patch) | |
tree | 39924a81fa6fa6d7ab2ee7246ce24285078e8ae2 | |
parent | 137fbe495d3922b71490d01083f04331eb0e6671 (diff) | |
download | busybox-w32-7ae209c0190c0c6e6b1c95ee9005edf4729e1ce0.tar.gz busybox-w32-7ae209c0190c0c6e6b1c95ee9005edf4729e1ce0.tar.bz2 busybox-w32-7ae209c0190c0c6e6b1c95ee9005edf4729e1ce0.zip |
losetup: support -f (Loic Grenie <loic.grenie@gmail.com>)
function old new delta
losetup_main 238 278 +40
packed_usage 23021 23027 +6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 46/0) Total: 46 bytes
-rw-r--r-- | include/usage.h | 14 | ||||
-rw-r--r-- | util-linux/losetup.c | 60 |
2 files changed, 45 insertions, 29 deletions
diff --git a/include/usage.h b/include/usage.h index 09c8fa7d1..b028f5a5c 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -1868,19 +1868,21 @@ | |||
1868 | " -f Output data as the log grows" | 1868 | " -f Output data as the log grows" |
1869 | 1869 | ||
1870 | #define losetup_trivial_usage \ | 1870 | #define losetup_trivial_usage \ |
1871 | "[-o OFFSET] [-d] LOOPDEVICE [FILE]]" | 1871 | "[-o OFS] LOOPDEV FILE - associate loop devices\n" \ |
1872 | " losetup -d LOOPDEV - disassociate\n" \ | ||
1873 | " losetup [-f] - show" | ||
1872 | #define losetup_full_usage \ | 1874 | #define losetup_full_usage \ |
1873 | "(Dis)associate LOOPDEVICE with FILE, or display current associations" \ | 1875 | "Options:\n" \ |
1874 | "\n\nOptions:\n" \ | 1876 | " -o OFS Start OFS bytes into FILE\n" \ |
1875 | " -d Disassociate LOOPDEVICE\n" \ | 1877 | " -f Show first free loop device" |
1876 | " -o OFFSET Start OFFSET bytes into FILE" | ||
1877 | #define losetup_notes_usage \ | 1878 | #define losetup_notes_usage \ |
1878 | "No arguments will display all current associations.\n" \ | 1879 | "No arguments will display all current associations.\n" \ |
1879 | "One argument (losetup /dev/loop1) will display the current association\n" \ | 1880 | "One argument (losetup /dev/loop1) will display the current association\n" \ |
1880 | "(if any), or disassociate it (with -d). The display shows the offset\n" \ | 1881 | "(if any), or disassociate it (with -d). The display shows the offset\n" \ |
1881 | "and filename of the file the loop device is currently bound to.\n\n" \ | 1882 | "and filename of the file the loop device is currently bound to.\n\n" \ |
1882 | "Two arguments (losetup /dev/loop1 file.img) create a new association,\n" \ | 1883 | "Two arguments (losetup /dev/loop1 file.img) create a new association,\n" \ |
1883 | "with an optional offset (-o 12345). Encryption is not yet supported.\n\n" | 1884 | "with an optional offset (-o 12345). Encryption is not yet supported.\n" \ |
1885 | "losetup -f will show the first loop free loop device\n\n" | ||
1884 | 1886 | ||
1885 | #define ls_trivial_usage \ | 1887 | #define ls_trivial_usage \ |
1886 | "[-1Aa" USE_FEATURE_LS_TIMESTAMPS("c") "Cd" \ | 1888 | "[-1Aa" USE_FEATURE_LS_TIMESTAMPS("c") "Cd" \ |
diff --git a/util-linux/losetup.c b/util-linux/losetup.c index 9409cdff5..57e8569dc 100644 --- a/util-linux/losetup.c +++ b/util-linux/losetup.c | |||
@@ -14,54 +14,68 @@ | |||
14 | int losetup_main(int argc, char **argv); | 14 | int losetup_main(int argc, char **argv); |
15 | int losetup_main(int argc, char **argv) | 15 | int losetup_main(int argc, char **argv) |
16 | { | 16 | { |
17 | char dev[] = LOOP_NAME"0"; | ||
17 | unsigned opt; | 18 | unsigned opt; |
18 | char *opt_o; | 19 | char *opt_o; |
20 | char *s; | ||
19 | unsigned long long offset = 0; | 21 | unsigned long long offset = 0; |
20 | 22 | ||
21 | opt = getopt32(argv, "do:", &opt_o); | 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); | ||
22 | argc -= optind; | 26 | argc -= optind; |
23 | argv += optind; | 27 | argv += optind; |
24 | 28 | ||
25 | if (opt == 0x3) // -d + -o (illegal) | 29 | if (opt == 0x2) // -o |
30 | offset = xatoull(opt_o); | ||
31 | |||
32 | if (opt == 0x4 && argc) // -f does not take any argument | ||
26 | bb_show_usage(); | 33 | bb_show_usage(); |
27 | 34 | ||
28 | if (opt == 0x1) { // -d | 35 | if (opt == 0x1) { // -d |
29 | /* detach takes exactly one argument */ | 36 | /* detach takes exactly one argument */ |
30 | if (argc != 1) | 37 | if (argc != 1) |
31 | bb_show_usage(); | 38 | bb_show_usage(); |
32 | if (!del_loop(argv[0])) | 39 | if (del_loop(argv[0])) |
33 | return EXIT_SUCCESS; | 40 | bb_perror_nomsg_and_die(); |
34 | bb_perror_nomsg_and_die(); | 41 | return EXIT_SUCCESS; |
35 | } | 42 | } |
36 | 43 | ||
37 | if (opt == 0x2) // -o | ||
38 | offset = xatoull(opt_o); | ||
39 | |||
40 | /* -o or no option */ | ||
41 | |||
42 | if (argc == 2) { | 44 | if (argc == 2) { |
45 | /* -o or no option */ | ||
43 | if (set_loop(&argv[0], argv[1], offset) < 0) | 46 | if (set_loop(&argv[0], argv[1], offset) < 0) |
44 | bb_perror_nomsg_and_die(); | 47 | bb_perror_nomsg_and_die(); |
45 | } else if (argc == 1) { | 48 | return EXIT_SUCCESS; |
46 | char *s = query_loop(argv[0]); | 49 | } |
50 | |||
51 | if (argc == 1) { | ||
52 | /* -o or no option */ | ||
53 | s = query_loop(argv[0]); | ||
47 | if (!s) | 54 | if (!s) |
48 | bb_perror_nomsg_and_die(); | 55 | bb_perror_nomsg_and_die(); |
49 | printf("%s: %s\n", argv[0], s); | 56 | printf("%s: %s\n", argv[0], s); |
50 | if (ENABLE_FEATURE_CLEAN_UP) | 57 | if (ENABLE_FEATURE_CLEAN_UP) |
51 | free(s); | 58 | free(s); |
52 | } else { | 59 | return EXIT_SUCCESS; |
53 | char dev[sizeof(LOOP_NAME"0")] = LOOP_NAME"0"; | 60 | } |
54 | char c; | 61 | |
55 | for (c = '0'; c <= '9'; ++c) { | 62 | /* -o, -f or no option */ |
56 | char *s; | 63 | while (1) { |
57 | dev[sizeof(LOOP_NAME"0")-2] = c; | 64 | s = query_loop(dev); |
58 | s = query_loop(dev); | 65 | if (!s) { |
59 | if (s) { | 66 | if (opt == 0x4) { |
60 | printf("%s: %s\n", dev, s); | 67 | printf("%s\n", dev); |
61 | if (ENABLE_FEATURE_CLEAN_UP) | 68 | return EXIT_SUCCESS; |
62 | free(s); | ||
63 | } | 69 | } |
70 | } else { | ||
71 | if (opt != 0x4) | ||
72 | printf("%s: %s\n", dev, s); | ||
73 | if (ENABLE_FEATURE_CLEAN_UP) | ||
74 | free(s); | ||
64 | } | 75 | } |
76 | |||
77 | if (++dev[sizeof(dev) - 2] > '9') | ||
78 | break; | ||
65 | } | 79 | } |
66 | return EXIT_SUCCESS; | 80 | return EXIT_SUCCESS; |
67 | } | 81 | } |