3
0

mt.c 2.5 KB

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