mt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. */
  5. //usage:#define mt_trivial_usage
  6. //usage: "[-f device] opcode value"
  7. //usage:#define mt_full_usage "\n\n"
  8. //usage: "Control magnetic tape drive operation\n"
  9. //usage: "\n"
  10. //usage: "Available Opcodes:\n"
  11. //usage: "\n"
  12. //usage: "bsf bsfm bsr bss datacompression drvbuffer eof eom erase\n"
  13. //usage: "fsf fsfm fsr fss load lock mkpart nop offline ras1 ras2\n"
  14. //usage: "ras3 reset retension rewind rewoffline seek setblk setdensity\n"
  15. //usage: "setpart tell unload unlock weof wset"
  16. #include "libbb.h"
  17. #include <sys/mtio.h>
  18. /* missing: eod/seod, stoptions, stwrthreshold, densities */
  19. static const short opcode_value[] = {
  20. MTBSF,
  21. MTBSFM,
  22. MTBSR,
  23. MTBSS,
  24. MTCOMPRESSION,
  25. MTEOM,
  26. MTERASE,
  27. MTFSF,
  28. MTFSFM,
  29. MTFSR,
  30. MTFSS,
  31. MTLOAD,
  32. MTLOCK,
  33. MTMKPART,
  34. MTNOP,
  35. MTOFFL,
  36. MTOFFL,
  37. MTRAS1,
  38. MTRAS2,
  39. MTRAS3,
  40. MTRESET,
  41. MTRETEN,
  42. MTREW,
  43. MTSEEK,
  44. MTSETBLK,
  45. MTSETDENSITY,
  46. MTSETDRVBUFFER,
  47. MTSETPART,
  48. MTTELL,
  49. MTWSM,
  50. MTUNLOAD,
  51. MTUNLOCK,
  52. MTWEOF,
  53. MTWEOF
  54. };
  55. static const char opcode_name[] ALIGN1 =
  56. "bsf" "\0"
  57. "bsfm" "\0"
  58. "bsr" "\0"
  59. "bss" "\0"
  60. "datacompression" "\0"
  61. "eom" "\0"
  62. "erase" "\0"
  63. "fsf" "\0"
  64. "fsfm" "\0"
  65. "fsr" "\0"
  66. "fss" "\0"
  67. "load" "\0"
  68. "lock" "\0"
  69. "mkpart" "\0"
  70. "nop" "\0"
  71. "offline" "\0"
  72. "rewoffline" "\0"
  73. "ras1" "\0"
  74. "ras2" "\0"
  75. "ras3" "\0"
  76. "reset" "\0"
  77. "retension" "\0"
  78. "rewind" "\0"
  79. "seek" "\0"
  80. "setblk" "\0"
  81. "setdensity" "\0"
  82. "drvbuffer" "\0"
  83. "setpart" "\0"
  84. "tell" "\0"
  85. "wset" "\0"
  86. "unload" "\0"
  87. "unlock" "\0"
  88. "eof" "\0"
  89. "weof" "\0";
  90. int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  91. int mt_main(int argc UNUSED_PARAM, char **argv)
  92. {
  93. const char *file = "/dev/tape";
  94. struct mtop op;
  95. struct mtpos position;
  96. int fd, mode, idx;
  97. if (!argv[1]) {
  98. bb_show_usage();
  99. }
  100. if (strcmp(argv[1], "-f") == 0) {
  101. if (!argv[2] || !argv[3])
  102. bb_show_usage();
  103. file = argv[2];
  104. argv += 2;
  105. }
  106. idx = index_in_strings(opcode_name, argv[1]);
  107. if (idx < 0)
  108. bb_error_msg_and_die("unrecognized opcode %s", argv[1]);
  109. op.mt_op = opcode_value[idx];
  110. if (argv[2])
  111. op.mt_count = xatoi_positive(argv[2]);
  112. else
  113. op.mt_count = 1; /* One, not zero, right? */
  114. switch (opcode_value[idx]) {
  115. case MTWEOF:
  116. case MTERASE:
  117. case MTWSM:
  118. case MTSETDRVBUFFER:
  119. mode = O_WRONLY;
  120. break;
  121. default:
  122. mode = O_RDONLY;
  123. break;
  124. }
  125. fd = xopen(file, mode);
  126. switch (opcode_value[idx]) {
  127. case MTTELL:
  128. ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file);
  129. printf("At block %d\n", (int) position.mt_blkno);
  130. break;
  131. default:
  132. ioctl_or_perror_and_die(fd, MTIOCTOP, &op, "%s", file);
  133. break;
  134. }
  135. return EXIT_SUCCESS;
  136. }