"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: */ |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini losetup implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2002 Matt Kraai. |
| 6 | * |
Bernhard Reutner-Fischer | 4a1865c | 2006-01-13 18:11:59 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <getopt.h> |
| 11 | #include <stdlib.h> |
| 12 | |
| 13 | #include "busybox.h" |
| 14 | |
Rob Landley | 1d589b2 | 2005-11-29 23:47:10 +0000 | [diff] [blame] | 15 | int losetup_main (int argc, char **argv) |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 16 | { |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 17 | int offset = 0; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 18 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 19 | /* This will need a "while(getopt()!=-1)" loop when we can have more than |
| 20 | one option, but for now we can't. */ |
| 21 | switch(getopt(argc,argv, "do:")) { |
| 22 | case 'd': |
| 23 | /* detach takes exactly one argument */ |
Rob Landley | ce88793 | 2005-12-21 17:00:25 +0000 | [diff] [blame] | 24 | if(optind+1!=argc) bb_show_usage(); |
| 25 | if(!del_loop(argv[optind])) return EXIT_SUCCESS; |
Rob Landley | 1d589b2 | 2005-11-29 23:47:10 +0000 | [diff] [blame] | 26 | die_failed: |
| 27 | bb_perror_msg_and_die("%s",argv[optind]); |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 28 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 29 | case 'o': |
Bernhard Reutner-Fischer | 4a1865c | 2006-01-13 18:11:59 +0000 | [diff] [blame] | 30 | offset = bb_xparse_number (optarg, NULL); |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 31 | /* Fall through to do the losetup */ |
| 32 | case -1: |
| 33 | /* losetup takes two argument:, loop_device and file */ |
Rob Landley | 1d589b2 | 2005-11-29 23:47:10 +0000 | [diff] [blame] | 34 | if(optind+2==argc) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 35 | if(set_loop(&argv[optind], argv[optind + 1], offset)>=0) |
| 36 | return EXIT_SUCCESS; |
| 37 | else goto die_failed; |
Rob Landley | 1d589b2 | 2005-11-29 23:47:10 +0000 | [diff] [blame] | 38 | } |
| 39 | if(optind+1==argc) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 40 | char *s=query_loop(argv[optind]); |
| 41 | if (!s) goto die_failed; |
| 42 | printf("%s: %s\n",argv[optind],s); |
| 43 | if(ENABLE_FEATURE_CLEAN_UP) free(s); |
| 44 | return EXIT_SUCCESS; |
Rob Landley | 1d589b2 | 2005-11-29 23:47:10 +0000 | [diff] [blame] | 45 | } |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 46 | break; |
| 47 | } |
| 48 | bb_show_usage(); |
| 49 | return EXIT_FAILURE; |
Matt Kraai | 83788da | 2002-03-20 17:38:37 +0000 | [diff] [blame] | 50 | } |