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