blob: e2d4e4d79162571f910ed09cd31ad41979eec091 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Matt Kraai83788da2002-03-20 17:38:37 +00002/*
3 * Mini losetup implementation for busybox
4 *
5 * Copyright (C) 2002 Matt Kraai.
6 *
Bernhard Reutner-Fischer4a1865c2006-01-13 18:11:59 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Matt Kraai83788da2002-03-20 17:38:37 +00008 */
9
10#include <getopt.h>
Matt Kraai83788da2002-03-20 17:38:37 +000011
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Matt Kraai83788da2002-03-20 17:38:37 +000013
Denis Vlasenko06af2162007-02-03 17:28:39 +000014int losetup_main(int argc, char **argv);
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000015int losetup_main(int argc, char **argv)
Matt Kraai83788da2002-03-20 17:38:37 +000016{
Denis Vlasenko67b23e62006-10-03 21:00:06 +000017 unsigned opt;
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000018 char *opt_o;
Denis Vlasenko13858992006-10-08 12:49:22 +000019 unsigned long long offset = 0;
Matt Kraai83788da2002-03-20 17:38:37 +000020
Denis Vlasenko67b23e62006-10-03 21:00:06 +000021 opt = getopt32(argc, argv, "do:", &opt_o);
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000022 argc -= optind;
23 argv += optind;
Matt Kraai83788da2002-03-20 17:38:37 +000024
Denis Vlasenko956a5692006-09-27 14:51:27 +000025 if (opt == 0x3) // -d + -o (illegal)
26 bb_show_usage();
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000027
28 if (opt == 0x1) { // -d
29 /* detach takes exactly one argument */
30 if (argc != 1)
31 bb_show_usage();
32 if (!del_loop(argv[0]))
33 return EXIT_SUCCESS;
34 bb_perror_nomsg_and_die();
35 }
36
37 if (opt == 0x2) // -o
Denis Vlasenko13858992006-10-08 12:49:22 +000038 offset = xatoull(opt_o);
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000039
40 /* -o or no option */
41
42 if (argc == 2) {
43 if (set_loop(&argv[0], argv[1], offset) < 0)
44 bb_perror_nomsg_and_die();
45 } else if (argc == 1) {
46 char *s = query_loop(argv[0]);
Denis Vlasenkoc34d3552007-04-19 00:09:34 +000047 if (!s)
48 bb_perror_nomsg_and_die();
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000049 printf("%s: %s\n", argv[0], s);
Denis Vlasenkoc34d3552007-04-19 00:09:34 +000050 if (ENABLE_FEATURE_CLEAN_UP)
51 free(s);
Denis Vlasenko956a5692006-09-27 14:51:27 +000052 } else {
Denis Vlasenko0b193a72006-09-29 21:47:11 +000053 char dev[sizeof(LOOP_NAME"0")] = LOOP_NAME"0";
Denis Vlasenko956a5692006-09-27 14:51:27 +000054 char c;
55 for (c = '0'; c <= '9'; ++c) {
56 char *s;
Denis Vlasenko0b193a72006-09-29 21:47:11 +000057 dev[sizeof(LOOP_NAME"0")-2] = c;
Denis Vlasenko956a5692006-09-27 14:51:27 +000058 s = query_loop(dev);
59 if (s) {
60 printf("%s: %s\n", dev, s);
Denis Vlasenkoc34d3552007-04-19 00:09:34 +000061 if (ENABLE_FEATURE_CLEAN_UP)
62 free(s);
Denis Vlasenko956a5692006-09-27 14:51:27 +000063 }
64 }
65 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000066 return EXIT_SUCCESS;
Matt Kraai83788da2002-03-20 17:38:37 +000067}