losetup: fix util-linux compatibility
[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:       "[-r] [-o OFS] {-f|LOOPDEV} FILE - associate loop devices\n"
12 //usage:       "        losetup -d LOOPDEV - disassociate\n"
13 //usage:       "        losetup -a - show status of  all\n"
14 //usage:       "        losetup -f - show next available"
15 //usage:#define losetup_full_usage "\n\n"
16 //usage:       "        -o OFS  Start OFS bytes into FILE"
17 //usage:     "\n        -r      Read-only"
18 //usage:     "\n        -f      Show/find first free loop device"
19 //usage:
20 //usage:#define losetup_notes_usage
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         char *opt_o;
35         char dev[LOOP_NAMESIZE];
36         enum {
37                 OPT_d = (1 << 0),
38                 OPT_o = (1 << 1),
39                 OPT_f = (1 << 2),
40                 OPT_a = (1 << 3),
41                 OPT_r = (1 << 4), /* must be last */
42         };
43
44         opt_complementary = "?2:d--ofar:a--ofr";
45         opt = getopt32(argv, "do:far", &opt_o);
46         argv += optind;
47
48         /* LOOPDEV */
49         if (!opt && argv[0] && !argv[1]) {
50                 char *s;
51
52                 s = query_loop(argv[0]);
53                 if (!s)
54                         bb_simple_perror_msg_and_die(argv[0]);
55                 printf("%s: %s\n", argv[0], s);
56                 if (ENABLE_FEATURE_CLEAN_UP)
57                         free(s);
58                 return EXIT_SUCCESS;
59         }
60
61         /* -d LOOPDEV */
62         if (opt == OPT_d) {
63                 if (del_loop(argv[0]))
64                         bb_simple_perror_msg_and_die(argv[0]);
65                 return EXIT_SUCCESS;
66         }
67
68         /* -a */
69         if (opt == OPT_a) {
70                 int n;
71                 for (n = 0; n < 10; n++) {
72                         char *s;
73
74                         sprintf(dev, LOOP_FORMAT, n);
75                         s = query_loop(dev);
76                         if (s) {
77                                 printf("%s: %s\n", dev, s);
78                                 if (ENABLE_FEATURE_CLEAN_UP)
79                                         free(s);
80                         }
81                 }
82                 return EXIT_SUCCESS;
83         }
84
85         /* contains -f */
86         if (opt & OPT_f) {
87                 char *s;
88                 int n = 0;
89
90                 do {
91                         sprintf(dev, LOOP_FORMAT, n);
92                         s = query_loop(dev);
93                         if (s && ENABLE_FEATURE_CLEAN_UP)
94                                 free(s);
95                 } while (s);
96                 if ((opt == OPT_f) && !argv[0]) {
97                         puts(dev);
98                         return EXIT_SUCCESS;
99                 }
100         }
101
102         /* [-r] [-o OFS] {-f|LOOPDEV} FILE */
103         if (argv[0] && ((opt & OPT_f) || argv[1])) {
104                 unsigned long long offset = 0;
105                 char *d = dev;
106
107                 if (opt == OPT_o)
108                         offset = xatoull(opt_o);
109                 if (opt != OPT_f)
110                         d = *(argv++);
111
112                 if (argv[0]) {
113                         if (set_loop(&d, argv[0], offset, (opt / OPT_r)) < 0)
114                                 bb_simple_perror_msg_and_die(argv[0]);
115                         return EXIT_SUCCESS;
116                 }
117         }
118
119         bb_show_usage();
120         return EXIT_FAILURE;
121 }