changed INDEX operator to handle multiple subscript arrays correctly
[unix-history] / usr / src / usr.bin / mt / mt.c
CommitLineData
fba0261b 1static char *sccsid = "@(#)mt.c 4.7 (Berkeley) 83/02/09";
d8c0aff8 2/*
209f2457
SL
3 * mt --
4 * magnetic tape manipulation program
d8c0aff8 5 */
d8c0aff8
BJ
6#include <stdio.h>
7#include <ctype.h>
8#include <sys/types.h>
9#include <sys/mtio.h>
10#include <sys/ioctl.h>
11
209f2457 12#define equal(s1,s2) (strcmp(s1, s2) == 0)
209f2457 13
d8c0aff8
BJ
14struct commands {
15 char *c_name;
16 int c_code;
fdb20def 17 int c_ronly;
d8c0aff8 18} com[] = {
209f2457
SL
19 { "weof", MTWEOF, 0 },
20 { "eof", MTWEOF, 0 },
21 { "fsf", MTFSF, 1 },
22 { "bsf", MTBSF, 1 },
23 { "fsr", MTFSR, 1 },
24 { "bsr", MTBSR, 1 },
25 { "rewind", MTREW, 1 },
26 { "offline", MTOFFL, 1 },
27 { "rewoffl", MTOFFL, 1 },
28 { "status", MTNOP, 1 },
29 { 0 }
d8c0aff8
BJ
30};
31
32int mtfd;
33struct mtop mt_com;
209f2457 34struct mtget mt_status;
d8c0aff8
BJ
35char *tape;
36
37main(argc, argv)
209f2457 38 char **argv;
d8c0aff8
BJ
39{
40 char line[80], *getenv();
41 register char *cp;
42 register struct commands *comp;
43
44 if (argc < 2) {
209f2457 45 fprintf(stderr, "usage: mt [ -f device ] command [ count ]\n");
d8c0aff8
BJ
46 exit(1);
47 }
209f2457 48 if ((equal(argv[1], "-t") || equal(argv[1], "-f")) && argc > 2) {
d8c0aff8
BJ
49 argc -= 2;
50 tape = argv[2];
51 argv += 2;
52 } else
53 if ((tape = getenv("TAPE")) == NULL)
209f2457 54 tape = DEFTAPE;
d8c0aff8
BJ
55 cp = argv[1];
56 for (comp = com; comp->c_name != NULL; comp++)
57 if (strncmp(cp, comp->c_name, strlen(cp)) == 0)
58 break;
59 if (comp->c_name == NULL) {
60 fprintf(stderr, "mt: don't grok \"%s\"\n", cp);
61 exit(1);
62 }
fdb20def
BJ
63 if ((mtfd = open(tape, comp->c_ronly ? 0 : 2)) < 0) {
64 perror(tape);
65 exit(1);
66 }
209f2457
SL
67 if (comp->c_code != MTNOP) {
68 mt_com.mt_op = comp->c_code;
69 mt_com.mt_count = (argc > 2 ? atoi(argv[2]) : 1);
70 if (mt_com.mt_count < 0) {
71 fprintf(stderr, "mt: negative repeat count\n");
72 exit(1);
73 }
74 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) {
61fc63ff 75 fprintf(stderr, "%s %s %d ", tape, comp->c_name,
209f2457
SL
76 mt_com.mt_count);
77 perror("failed");
78 exit(2);
79 }
80 } else {
81 if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
82 perror("mt");
83 exit(2);
84 }
85 status(&mt_status);
86 }
87}
88
09868be4
SL
89#ifdef vax
90#include <vaxmba/mtreg.h>
91#include <vaxmba/htreg.h>
92
93#include <vaxuba/utreg.h>
94#include <vaxuba/tmreg.h>
209f2457 95#undef b_repcnt /* argh */
09868be4
SL
96#include <vaxuba/tsreg.h>
97#endif
209f2457 98
61fc63ff 99#ifdef sun
fba0261b
SL
100#include <sundev/tmreg.h>
101#include <sundev/arreg.h>
61fc63ff
SL
102#endif
103
209f2457
SL
104struct tape_desc {
105 short t_type; /* type of magtape device */
106 char *t_name; /* printing name */
107 char *t_dsbits; /* "drive status" register */
108 char *t_erbits; /* "error" register */
109} tapes[] = {
09868be4 110#ifdef vax
209f2457
SL
111 { MT_ISTS, "ts11", 0, TSXS0_BITS },
112 { MT_ISHT, "tm03", HTDS_BITS, HTER_BITS },
113 { MT_ISTM, "tm11", 0, TMER_BITS },
114 { MT_ISMT, "tu78", MTDS_BITS, 0 },
115 { MT_ISUT, "tu45", UTDS_BITS, UTER_BITS },
61fc63ff
SL
116#endif
117#ifdef sun
118 { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
fba0261b 119 { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
09868be4 120#endif
209f2457
SL
121 { 0 }
122};
123
124/*
125 * Interpret the status buffer returned
126 */
127status(bp)
128 register struct mtget *bp;
129{
130 register struct tape_desc *mt;
131
132 for (mt = tapes; mt->t_type; mt++)
133 if (mt->t_type == bp->mt_type)
134 break;
135 if (mt->t_type == 0) {
136 printf("unknown tape drive type (%d)\n", bp->mt_type);
137 return;
138 }
61fc63ff 139 printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
209f2457 140 printreg("ds", bp->mt_dsreg, mt->t_dsbits);
61fc63ff
SL
141 printreg("\ner", bp->mt_erreg, mt->t_erbits);
142 putchar('\n');
209f2457
SL
143}
144
145/*
146 * Print a register a la the %b format of the kernel's printf
147 */
148printreg(s, v, bits)
61fc63ff
SL
149 char *s;
150 register char *bits;
151 register unsigned short v;
209f2457
SL
152{
153 register int i, any = 0;
154 register char c;
155
61fc63ff
SL
156 if (bits && *bits == 8)
157 printf("%s=%o", s, v);
158 else
159 printf("%s=%x", s, v);
160 bits++;
209f2457
SL
161 if (v && bits) {
162 putchar('<');
163 while (i = *bits++) {
164 if (v & (1 << (i-1))) {
165 if (any)
166 putchar(',');
167 any = 1;
168 for (; (c = *bits) > 32; bits++)
169 putchar(c);
170 } else
171 for (; *bits > 32; bits++)
172 ;
173 }
61fc63ff 174 putchar('>');
d8c0aff8
BJ
175 }
176}