move remaining help text from include/usage.src.h
[oweals/busybox.git] / util-linux / losetup.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini losetup implementation for busybox
4  *
5  * Copyright (C) 2002  Matt Kraai.
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
10 //usage:#define losetup_trivial_usage
11 //usage:       "[-o OFS] LOOPDEV FILE - associate loop devices\n"
12 //usage:       "        losetup -d LOOPDEV - disassociate\n"
13 //usage:       "        losetup [-f] - show"
14 //usage:#define losetup_full_usage "\n\n"
15 //usage:       "Options:"
16 //usage:     "\n        -o OFS  Start OFS bytes into FILE"
17 //usage:     "\n        -f      Show first free loop device"
18 //usage:
19 //usage:#define losetup_notes_usage
20 //usage:       "No arguments will display all current associations.\n"
21 //usage:       "One argument (losetup /dev/loop1) will display the current association\n"
22 //usage:       "(if any), or disassociate it (with -d). The display shows the offset\n"
23 //usage:       "and filename of the file the loop device is currently bound to.\n\n"
24 //usage:       "Two arguments (losetup /dev/loop1 file.img) create a new association,\n"
25 //usage:       "with an optional offset (-o 12345). Encryption is not yet supported.\n"
26 //usage:       "losetup -f will show the first loop free loop device\n\n"
27
28 #include "libbb.h"
29
30 int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31 int losetup_main(int argc UNUSED_PARAM, char **argv)
32 {
33         unsigned opt;
34         int n;
35         char *opt_o;
36         unsigned long long offset = 0;
37         enum {
38                 OPT_d = (1 << 0),
39                 OPT_o = (1 << 1),
40                 OPT_f = (1 << 2),
41         };
42
43         /* max 2 args, all opts are mutually exclusive */
44         opt_complementary = "?2:d--of:o--df:f--do";
45         opt = getopt32(argv, "do:f", &opt_o);
46         argv += optind;
47
48         if (opt == OPT_o)
49                 offset = xatoull(opt_o);
50
51         if (opt == OPT_d) {
52                 /* -d BLOCKDEV */
53                 if (!argv[0] || argv[1])
54                         bb_show_usage();
55                 if (del_loop(argv[0]))
56                         bb_simple_perror_msg_and_die(argv[0]);
57                 return EXIT_SUCCESS;
58         }
59
60         if (argv[0]) {
61                 char *s;
62
63                 if (opt == OPT_f) /* -f should not have arguments */
64                         bb_show_usage();
65
66                 if (argv[1]) {
67                         /* [-o OFS] BLOCKDEV FILE */
68                         if (set_loop(&argv[0], argv[1], offset) < 0)
69                                 bb_simple_perror_msg_and_die(argv[0]);
70                         return EXIT_SUCCESS;
71                 }
72                 /* [-o OFS] BLOCKDEV */
73                 s = query_loop(argv[0]);
74                 if (!s)
75                         bb_simple_perror_msg_and_die(argv[0]);
76                 printf("%s: %s\n", argv[0], s);
77                 if (ENABLE_FEATURE_CLEAN_UP)
78                         free(s);
79                 return EXIT_SUCCESS;
80         }
81
82         /* [-o OFS|-f] with no params */
83         n = 0;
84         while (1) {
85                 char *s;
86                 char dev[LOOP_NAMESIZE];
87
88                 sprintf(dev, LOOP_FORMAT, n);
89                 s = query_loop(dev);
90                 n++;
91                 if (!s) {
92                         if (n > 9 && errno && errno != ENXIO)
93                                 return EXIT_SUCCESS;
94                         if (opt == OPT_f) {
95                                 puts(dev);
96                                 return EXIT_SUCCESS;
97                         }
98                 } else {
99                         if (opt != OPT_f)
100                                 printf("%s: %s\n", dev, s);
101                         if (ENABLE_FEATURE_CLEAN_UP)
102                                 free(s);
103                 }
104         }
105         return EXIT_SUCCESS;
106 }