mt.c 2.2 KB

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