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