mt.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 ATTRIBUTE_UNUSED, char **argv)
  81. {
  82. const char *file = "/dev/tape";
  83. struct mtop op;
  84. struct mtpos position;
  85. int fd, mode, idx;
  86. if (!argv[1]) {
  87. bb_show_usage();
  88. }
  89. if (strcmp(argv[1], "-f") == 0) {
  90. if (!argv[2] || !argv[3])
  91. bb_show_usage();
  92. file = argv[2];
  93. argv += 2;
  94. }
  95. idx = index_in_strings(opcode_name, argv[1]);
  96. if (idx < 0)
  97. bb_error_msg_and_die("unrecognized opcode %s", argv[1]);
  98. op.mt_op = opcode_value[idx];
  99. if (argv[2])
  100. op.mt_count = xatoi_u(argv[2]);
  101. else
  102. op.mt_count = 1; /* One, not zero, right? */
  103. switch (opcode_value[idx]) {
  104. case MTWEOF:
  105. case MTERASE:
  106. case MTWSM:
  107. case MTSETDRVBUFFER:
  108. mode = O_WRONLY;
  109. break;
  110. default:
  111. mode = O_RDONLY;
  112. break;
  113. }
  114. fd = xopen(file, mode);
  115. switch (opcode_value[idx]) {
  116. case MTTELL:
  117. ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file);
  118. printf("At block %d\n", (int) position.mt_blkno);
  119. break;
  120. default:
  121. ioctl_or_perror_and_die(fd, MTIOCTOP, &op, "%s", file);
  122. break;
  123. }
  124. return EXIT_SUCCESS;
  125. }