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