Initial public busybox upstream commit
[busybox4maemo] / 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 tarball for details.
8  */
9
10 #include <getopt.h>
11
12 #include "libbb.h"
13
14 int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
15 int losetup_main(int argc, char **argv)
16 {
17         char dev[] = LOOP_NAME"0";
18         unsigned opt;
19         char *opt_o;
20         char *s;
21         unsigned long long offset = 0;
22
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);
26         argc -= optind;
27         argv += optind;
28
29         if (opt == 0x2) // -o
30                 offset = xatoull(opt_o);
31
32         if (opt == 0x4 && argc) // -f does not take any argument
33                 bb_show_usage();
34
35         if (opt == 0x1) { // -d
36                 /* detach takes exactly one argument */
37                 if (argc != 1)
38                         bb_show_usage();
39                 if (del_loop(argv[0]))
40                         bb_simple_perror_msg_and_die(argv[0]);
41                 return EXIT_SUCCESS;
42         }
43
44         if (argc == 2) {
45                 /* -o or no option */
46                 if (set_loop(&argv[0], argv[1], offset) < 0)
47                         bb_simple_perror_msg_and_die(argv[0]);
48                 return EXIT_SUCCESS;
49         }
50
51         if (argc == 1) {
52                 /* -o or no option */
53                 s = query_loop(argv[0]);
54                 if (!s)
55                         bb_simple_perror_msg_and_die(argv[0]);
56                 printf("%s: %s\n", argv[0], s);
57                 if (ENABLE_FEATURE_CLEAN_UP)
58                         free(s);
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) {
67                                 puts(dev);
68                                 return EXIT_SUCCESS;
69                         }
70                 } else {
71                         if (opt != 0x4)
72                                 printf("%s: %s\n", dev, s);
73                         if (ENABLE_FEATURE_CLEAN_UP)
74                                 free(s);
75                 }
76
77                 if (++dev[sizeof(dev) - 2] > '9')
78                         break;
79         }
80         return EXIT_SUCCESS;
81 }