blob: c2d3339e0ab3c4fbe48e35eea04e346f01cf0016 [file] [log] [blame]
Matt Kraai83788da2002-03-20 17:38:37 +00001/*
2 * Mini losetup implementation for busybox
3 *
4 * Copyright (C) 2002 Matt Kraai.
5 *
Bernhard Reutner-Fischer4a1865c2006-01-13 18:11:59 +00006 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Matt Kraai83788da2002-03-20 17:38:37 +00007 */
8
9#include <getopt.h>
10#include <stdlib.h>
11
12#include "busybox.h"
13
Rob Landley1d589b22005-11-29 23:47:10 +000014int losetup_main (int argc, char **argv)
Matt Kraai83788da2002-03-20 17:38:37 +000015{
Matt Kraai83788da2002-03-20 17:38:37 +000016 int offset = 0;
Matt Kraai83788da2002-03-20 17:38:37 +000017
Rob Landley6a6798b2005-08-10 20:35:54 +000018 /* This will need a "while(getopt()!=-1)" loop when we can have more than
19 one option, but for now we can't. */
20 switch(getopt(argc,argv, "do:")) {
21 case 'd':
22 /* detach takes exactly one argument */
Rob Landleyce887932005-12-21 17:00:25 +000023 if(optind+1!=argc) bb_show_usage();
24 if(!del_loop(argv[optind])) return EXIT_SUCCESS;
Rob Landley1d589b22005-11-29 23:47:10 +000025die_failed:
26 bb_perror_msg_and_die("%s",argv[optind]);
Matt Kraai83788da2002-03-20 17:38:37 +000027
Rob Landley6a6798b2005-08-10 20:35:54 +000028 case 'o':
Bernhard Reutner-Fischer4a1865c2006-01-13 18:11:59 +000029 offset = bb_xparse_number (optarg, NULL);
Rob Landley6a6798b2005-08-10 20:35:54 +000030 /* Fall through to do the losetup */
31 case -1:
32 /* losetup takes two argument:, loop_device and file */
Rob Landley1d589b22005-11-29 23:47:10 +000033 if(optind+2==argc) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000034 if(set_loop(&argv[optind], argv[optind + 1], offset)>=0)
35 return EXIT_SUCCESS;
36 else goto die_failed;
Rob Landley1d589b22005-11-29 23:47:10 +000037 }
38 if(optind+1==argc) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000039 char *s=query_loop(argv[optind]);
40 if (!s) goto die_failed;
41 printf("%s: %s\n",argv[optind],s);
42 if(ENABLE_FEATURE_CLEAN_UP) free(s);
43 return EXIT_SUCCESS;
Rob Landley1d589b22005-11-29 23:47:10 +000044 }
Rob Landley6a6798b2005-08-10 20:35:54 +000045 break;
46 }
47 bb_show_usage();
48 return EXIT_FAILURE;
Matt Kraai83788da2002-03-20 17:38:37 +000049}