3
0

losetup.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "libbb.h"
  10. int losetup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  11. int losetup_main(int argc UNUSED_PARAM, char **argv)
  12. {
  13. unsigned opt;
  14. int n;
  15. char *opt_o;
  16. unsigned long long offset = 0;
  17. enum {
  18. OPT_d = (1 << 0),
  19. OPT_o = (1 << 1),
  20. OPT_f = (1 << 2),
  21. };
  22. /* max 2 args, all opts are mutually exclusive */
  23. opt_complementary = "?2:d--of:o--df:f--do";
  24. opt = getopt32(argv, "do:f", &opt_o);
  25. argv += optind;
  26. if (opt == OPT_o)
  27. offset = xatoull(opt_o);
  28. if (opt == OPT_d) {
  29. /* -d BLOCKDEV */
  30. if (!argv[0] || argv[1])
  31. bb_show_usage();
  32. if (del_loop(argv[0]))
  33. bb_simple_perror_msg_and_die(argv[0]);
  34. return EXIT_SUCCESS;
  35. }
  36. if (argv[0]) {
  37. char *s;
  38. if (opt == OPT_f) /* -f should not have arguments */
  39. bb_show_usage();
  40. if (argv[1]) {
  41. /* [-o OFS] BLOCKDEV FILE */
  42. if (set_loop(&argv[0], argv[1], offset) < 0)
  43. bb_simple_perror_msg_and_die(argv[0]);
  44. return EXIT_SUCCESS;
  45. }
  46. /* [-o OFS] BLOCKDEV */
  47. s = query_loop(argv[0]);
  48. if (!s)
  49. bb_simple_perror_msg_and_die(argv[0]);
  50. printf("%s: %s\n", argv[0], s);
  51. if (ENABLE_FEATURE_CLEAN_UP)
  52. free(s);
  53. return EXIT_SUCCESS;
  54. }
  55. /* [-o OFS|-f] with no params */
  56. n = 0;
  57. while (1) {
  58. char *s;
  59. char dev[LOOP_NAMESIZE];
  60. sprintf(dev, LOOP_FORMAT, n);
  61. s = query_loop(dev);
  62. n++;
  63. if (!s) {
  64. if (n > 9 && errno && errno != ENXIO)
  65. return EXIT_SUCCESS;
  66. if (opt == OPT_f) {
  67. puts(dev);
  68. return EXIT_SUCCESS;
  69. }
  70. } else {
  71. if (opt != OPT_f)
  72. printf("%s: %s\n", dev, s);
  73. if (ENABLE_FEATURE_CLEAN_UP)
  74. free(s);
  75. }
  76. }
  77. return EXIT_SUCCESS;
  78. }