Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / mt / mt.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)mt.c     8.2 (Berkeley) 5/4/95
35  * $FreeBSD: src/usr.bin/mt/mt.c,v 1.26.2.3 2002/11/08 11:35:57 joerg Exp $
36  * $DragonFly: src/usr.bin/mt/mt.c,v 1.2 2003/06/17 04:29:29 dillon Exp $
37  */
38
39 /*
40  * mt --
41  *   magnetic tape manipulation program
42  */
43 #include <sys/types.h>
44 #include <sys/ioctl.h>
45 #include <sys/mtio.h>
46
47 #include <ctype.h>
48 #include <err.h>
49 #include <fcntl.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 /* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
56 #if defined(__FreeBSD__)
57 /* c_flags */
58 #define NEED_2ARGS      0x01
59 #define ZERO_ALLOWED    0x02
60 #define IS_DENSITY      0x04
61 #define DISABLE_THIS    0x08
62 #define IS_COMP         0x10
63 #endif /* defined(__FreeBSD__) */
64
65 #ifndef TRUE
66 #define TRUE 1
67 #endif
68 #ifndef FALSE
69 #define FALSE 0
70 #endif
71
72 struct commands {
73         char *c_name;
74         int c_code;
75         int c_ronly;
76 #if defined(__FreeBSD__)
77         int c_flags;
78 #endif /* defined(__FreeBSD__) */
79 } com[] = {
80         { "bsf",        MTBSF,  1 },
81         { "bsr",        MTBSR,  1 },
82 #if defined(__FreeBSD__)
83         /* XXX FreeBSD considered "eof" dangerous, since it's being
84            confused with "eom" (and is an alias for "weof" anyway) */
85         { "eof",        MTWEOF, 0, DISABLE_THIS },
86 #else
87         { "eof",        MTWEOF, 0 },
88 #endif
89         { "fsf",        MTFSF,  1 },
90         { "fsr",        MTFSR,  1 },
91         { "offline",    MTOFFL, 1 },
92         { "rewind",     MTREW,  1 },
93         { "rewoffl",    MTOFFL, 1 },
94         { "status",     MTNOP,  1 },
95 #if defined(__FreeBSD__)
96         { "weof",       MTWEOF, 0, ZERO_ALLOWED },
97 #else
98         { "weof",       MTWEOF, 0 },
99 #endif
100 #if defined(__FreeBSD__)
101         { "erase",      MTERASE, 0, ZERO_ALLOWED},
102         { "blocksize",  MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
103         { "density",    MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
104         { "eom",        MTEOD, 1 },
105         { "eod",        MTEOD, 1 },
106         { "smk",        MTWSS, 0 },
107         { "wss",        MTWSS, 0 },
108         { "fss",        MTFSS, 1 },
109         { "bss",        MTBSS, 1 },
110         { "comp",       MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED|IS_COMP },
111         { "retension",  MTRETENS, 1 },
112         { "rdhpos",     MTIOCRDHPOS,  0 },
113         { "rdspos",     MTIOCRDSPOS,  0 },
114         { "sethpos",    MTIOCHLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
115         { "setspos",    MTIOCSLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
116         { "errstat",    MTIOCERRSTAT, 0 },
117         { "setmodel",   MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
118         { "seteotmodel",        MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
119         { "getmodel",   MTIOCGETEOTMODEL },
120         { "geteotmodel",        MTIOCGETEOTMODEL },
121 #endif /* defined(__FreeBSD__) */
122         { NULL }
123 };
124
125 void printreg __P((char *, u_int, char *));
126 void status __P((struct mtget *));
127 void usage __P((void));
128 #if defined (__FreeBSD__)
129 void st_status (struct mtget *);
130 int stringtodens (const char *s);
131 const char *denstostring (int d);
132 int denstobp(int d, int bpi);
133 u_int32_t stringtocomp(const char *s);
134 const char * comptostring(u_int32_t comp);
135 void warn_eof __P((void));
136 #endif /* defined (__FreeBSD__) */
137
138 int
139 main(argc, argv)
140         int argc;
141         char *argv[];
142 {
143         register struct commands *comp;
144         struct mtget mt_status;
145         struct mtop mt_com;
146         int ch, len, mtfd;
147         char *p, *tape;
148
149         if ((tape = getenv("TAPE")) == NULL)
150                 tape = DEFTAPE;
151
152         while ((ch = getopt(argc, argv, "f:t:")) != -1)
153                 switch(ch) {
154                 case 'f':
155                 case 't':
156                         tape = optarg;
157                         break;
158                 case '?':
159                 default:
160                         usage();
161                 }
162         argc -= optind;
163         argv += optind;
164
165         if (argc < 1 || argc > 2)
166                 usage();
167
168         len = strlen(p = *argv++);
169         for (comp = com;; comp++) {
170                 if (comp->c_name == NULL)
171                         errx(1, "%s: unknown command", p);
172                 if (strncmp(p, comp->c_name, len) == 0)
173                         break;
174         }
175 #if defined(__FreeBSD__)
176         if((comp->c_flags & NEED_2ARGS) && argc != 2)
177                 usage();
178         if(comp->c_flags & DISABLE_THIS) {
179                 warn_eof();
180         }
181 #endif /* defined(__FreeBSD__) */
182         if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
183                 err(1, "%s", tape);
184         if (comp->c_code != MTNOP) {
185                 mt_com.mt_op = comp->c_code;
186                 if (*argv) {
187 #if defined (__FreeBSD__)
188                         if (!isdigit(**argv) &&
189                             (comp->c_flags & IS_DENSITY)) {
190                                 const char *dcanon;
191                                 mt_com.mt_count = stringtodens(*argv);
192                                 if (mt_com.mt_count == 0)
193                                         errx(1, "%s: unknown density", *argv);
194                                 dcanon = denstostring(mt_com.mt_count);
195                                 if (strcmp(dcanon, *argv) != 0)
196                                         printf(
197                                         "Using \"%s\" as an alias for %s\n",
198                                                *argv, dcanon);
199                                 p = "";
200                         } else if (!isdigit(**argv) &&
201                                    (comp->c_flags & IS_COMP)) {
202
203                                 mt_com.mt_count = stringtocomp(*argv);
204                                 if ((u_int32_t)mt_com.mt_count == 0xf0f0f0f0)
205                                         errx(1, "%s: unknown compression",
206                                              *argv);
207                                 p = "";
208                         } else
209                                 /* allow for hex numbers; useful for density */
210                                 mt_com.mt_count = strtol(*argv, &p, 0);
211 #else
212                         mt_com.mt_count = strtol(*argv, &p, 10);
213 #endif /* defined(__FreeBSD__) */
214                         if ((mt_com.mt_count <=
215 #if defined (__FreeBSD__)
216                             ((comp->c_flags & ZERO_ALLOWED)? -1: 0)
217                             && ((comp->c_flags & IS_COMP) == 0)
218 #else
219                             0
220 #endif /* defined (__FreeBSD__) */
221                             ) || *p)
222                                 errx(1, "%s: illegal count", *argv);
223                 }
224                 else
225                         mt_com.mt_count = 1;
226 #if     defined(__FreeBSD__)
227                 switch (comp->c_code) {
228                 case MTIOCERRSTAT:
229                 {
230                         int i;
231                         union mterrstat umn;
232                         struct scsi_tape_errors *s = &umn.scsi_errstat;
233
234                         if (ioctl(mtfd, comp->c_code, (caddr_t)&umn) < 0)
235                                 err(2, "%s", tape);
236                         (void)printf("Last I/O Residual: %u\n", s->io_resid);
237                         (void)printf(" Last I/O Command:");
238                         for (i = 0; i < sizeof (s->io_cdb); i++)
239                                 (void)printf(" %02X", s->io_cdb[i]);
240                         (void)printf("\n");
241                         (void)printf("   Last I/O Sense:\n\n\t");
242                         for (i = 0; i < sizeof (s->io_sense); i++) {
243                                 (void)printf(" %02X", s->io_sense[i]);
244                                 if (((i + 1) & 0xf) == 0) {
245                                         (void)printf("\n\t");
246                                 }
247                         }
248                         (void)printf("\n");
249                         (void)printf("Last Control Residual: %u\n",
250                             s->ctl_resid);
251                         (void)printf(" Last Control Command:");
252                         for (i = 0; i < sizeof (s->ctl_cdb); i++)
253                                 (void)printf(" %02X", s->ctl_cdb[i]);
254                         (void)printf("\n");
255                         (void)printf("   Last Control Sense:\n\n\t");
256                         for (i = 0; i < sizeof (s->ctl_sense); i++) {
257                                 (void)printf(" %02X", s->ctl_sense[i]);
258                                 if (((i + 1) & 0xf) == 0) {
259                                         (void)printf("\n\t");
260                                 }
261                         }
262                         (void)printf("\n\n");
263                         exit(0);
264                         /* NOTREACHED */
265                 }
266                 case MTIOCRDHPOS:
267                 case MTIOCRDSPOS:
268                 {
269                         u_int32_t block;
270                         if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
271                                 err(2, "%s", tape);
272                         (void)printf("%s: %s block location %u\n", tape,
273                             (comp->c_code == MTIOCRDHPOS)? "hardware" :
274                             "logical", block);
275                         exit(0);
276                         /* NOTREACHED */
277                 }
278                 case MTIOCSLOCATE:
279                 case MTIOCHLOCATE:
280                 {
281                         u_int32_t block = (u_int32_t)mt_com.mt_count;
282                         if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
283                                 err(2, "%s", tape);
284                         exit(0);
285                         /* NOTREACHED */
286                 }
287                 case MTIOCGETEOTMODEL:
288                 {
289                         u_int32_t om;
290                         if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
291                                 err(2, "%s", tape);
292                         (void)printf("%s: the model is %u filemar%s at EOT\n",
293                             tape, om, (om > 1)? "ks" : "k");
294                         exit(0);
295                         /* NOTREACHED */
296                 }
297                 case MTIOCSETEOTMODEL:
298                 {
299                         u_int32_t om, nm = (u_int32_t)mt_com.mt_count;
300                         if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
301                                 err(2, "%s", tape);
302                         if (ioctl(mtfd, comp->c_code, (caddr_t)&nm) < 0)
303                                 err(2, "%s", tape);
304                         (void)printf("%s: old model was %u filemar%s at EOT\n",
305                             tape, om, (om > 1)? "ks" : "k");
306                         (void)printf("%s: new model  is %u filemar%s at EOT\n",
307                             tape, nm, (nm > 1)? "ks" : "k");
308                         exit(0);
309                         /* NOTREACHED */
310                 }
311                 default:
312                         break;
313                 }
314 #endif
315                 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
316                         err(1, "%s: %s", tape, comp->c_name);
317         } else {
318                 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
319                         err(1, NULL);
320                 status(&mt_status);
321         }
322         exit(0);
323         /* NOTREACHED */
324 }
325
326 #ifdef vax
327 #include <vax/mba/mtreg.h>
328 #include <vax/mba/htreg.h>
329
330 #include <vax/uba/utreg.h>
331 #include <vax/uba/tmreg.h>
332 #undef b_repcnt         /* argh */
333 #include <vax/uba/tsreg.h>
334 #endif
335
336 #ifdef sun
337 #include <sundev/tmreg.h>
338 #include <sundev/arreg.h>
339 #endif
340
341 #ifdef tahoe
342 #include <tahoe/vba/cyreg.h>
343 #endif
344
345 #if defined(__FreeBSD__) && defined(__i386__)
346 #include <machine/wtio.h>
347 #endif
348
349 struct tape_desc {
350         short   t_type;         /* type of magtape device */
351         char    *t_name;        /* printing name */
352         char    *t_dsbits;      /* "drive status" register */
353         char    *t_erbits;      /* "error" register */
354 } tapes[] = {
355 #ifdef vax
356         { MT_ISTS,      "ts11",         0,              TSXS0_BITS },
357         { MT_ISHT,      "tm03",         HTDS_BITS,      HTER_BITS },
358         { MT_ISTM,      "tm11",         0,              TMER_BITS },
359         { MT_ISMT,      "tu78",         MTDS_BITS,      0 },
360         { MT_ISUT,      "tu45",         UTDS_BITS,      UTER_BITS },
361 #endif
362 #ifdef sun
363         { MT_ISCPC,     "TapeMaster",   TMS_BITS,       0 },
364         { MT_ISAR,      "Archive",      ARCH_CTRL_BITS, ARCH_BITS },
365 #endif
366 #ifdef tahoe
367         { MT_ISCY,      "cipher",       CYS_BITS,       CYCW_BITS },
368 #endif
369 #if defined (__FreeBSD__)
370         /*
371          * XXX This is weird.  The st driver reports the tape drive
372          * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver
373          * either reports MT_ISVIPER1 for an Archive tape, or 0x11
374          * (MT_ISMFOUR) for other tapes.
375          * XXX for the wt driver, rely on it behaving like a "standard"
376          * magtape driver.
377          */
378         { MT_ISAR,      "SCSI tape drive", 0,           0 },
379 #if defined (__i386__)
380         { MT_ISVIPER1,  "Archive Viper", WTDS_BITS, WTER_BITS },
381         { MT_ISMFOUR,   "Wangtek",       WTDS_BITS, WTER_BITS },
382 #endif
383 #endif /* defined (__FreeBSD__) */
384         { 0 }
385 };
386
387 /*
388  * Interpret the status buffer returned
389  */
390 void
391 status(bp)
392         register struct mtget *bp;
393 {
394         register struct tape_desc *mt;
395
396         for (mt = tapes;; mt++) {
397                 if (mt->t_type == 0) {
398                         (void)printf("%d: unknown tape drive type\n",
399                             bp->mt_type);
400                         return;
401                 }
402                 if (mt->t_type == bp->mt_type)
403                         break;
404         }
405 #if defined (__FreeBSD__)
406         if(mt->t_type == MT_ISAR)
407                 st_status(bp);
408         else {
409 #endif /* defined (__FreeBSD__) */
410         (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
411         printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
412         printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
413         (void)putchar('\n');
414 #if defined (__FreeBSD__)
415         }
416 #endif /* defined (__FreeBSD__) */
417 }
418
419 /*
420  * Print a register a la the %b format of the kernel's printf.
421  */
422 void
423 printreg(s, v, bits)
424         char *s;
425         register u_int v;
426         register char *bits;
427 {
428         register int i, any = 0;
429         register char c;
430
431         if (bits && *bits == 8)
432                 printf("%s=%o", s, v);
433         else
434                 printf("%s=%x", s, v);
435         if (!bits)
436                 return;
437         bits++;
438         if (v && bits) {
439                 putchar('<');
440                 while ((i = *bits++)) {
441                         if (v & (1 << (i-1))) {
442                                 if (any)
443                                         putchar(',');
444                                 any = 1;
445                                 for (; (c = *bits) > 32; bits++)
446                                         putchar(c);
447                         } else
448                                 for (; *bits > 32; bits++)
449                                         ;
450                 }
451                 putchar('>');
452         }
453 }
454
455 void
456 usage()
457 {
458         (void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
459         exit(1);
460 }
461
462 #if defined (__FreeBSD__)
463
464 struct densities {
465         int dens;
466         int bpmm;
467         int bpi;
468         const char *name;
469 } dens[] = {
470         /*
471          * Taken from T10 Project 997D 
472          * SCSI-3 Stream Device Commands (SSC)
473          * Revision 11, 4-Nov-97
474          */
475         /*Num.  bpmm    bpi     Reference     */
476         { 0x1,  32,     800,    "X3.22-1983" },
477         { 0x2,  63,     1600,   "X3.39-1986" },
478         { 0x3,  246,    6250,   "X3.54-1986" },
479         { 0x5,  315,    8000,   "X3.136-1986" },
480         { 0x6,  126,    3200,   "X3.157-1987" },
481         { 0x7,  252,    6400,   "X3.116-1986" },
482         { 0x8,  315,    8000,   "X3.158-1987" },
483         { 0x9,  491,    37871,  "X3.180" },
484         { 0xA,  262,    6667,   "X3B5/86-199" },
485         { 0xB,  63,     1600,   "X3.56-1986" },
486         { 0xC,  500,    12690,  "HI-TC1" },
487         { 0xD,  999,    25380,  "HI-TC2" },
488         { 0xF,  394,    10000,  "QIC-120" },
489         { 0x10, 394,    10000,  "QIC-150" },
490         { 0x11, 630,    16000,  "QIC-320" },
491         { 0x12, 2034,   51667,  "QIC-1350" },
492         { 0x13, 2400,   61000,  "X3B5/88-185A" },
493         { 0x14, 1703,   43245,  "X3.202-1991" },
494         { 0x15, 1789,   45434,  "ECMA TC17" },
495         { 0x16, 394,    10000,  "X3.193-1990" },
496         { 0x17, 1673,   42500,  "X3B5/91-174" },
497         { 0x18, 1673,   42500,  "X3B5/92-50" },
498         { 0x19, 2460,   62500,  "DLTapeIII" },
499         { 0x1A, 3214,   81633,  "DLTapeIV(20GB)" },
500         { 0x1B, 3383,   85937,  "DLTapeIV(35GB)" },
501         { 0x1C, 1654,   42000,  "QIC-385M" },
502         { 0x1D, 1512,   38400,  "QIC-410M" },
503         { 0x1E, 1385,   36000,  "QIC-1000C" },
504         { 0x1F, 2666,   67733,  "QIC-2100C" },
505         { 0x20, 2666,   67733,  "QIC-6GB(M)" },
506         { 0x21, 2666,   67733,  "QIC-20GB(C)" },
507         { 0x22, 1600,   40640,  "QIC-2GB(C)" },
508         { 0x23, 2666,   67733,  "QIC-875M" },
509         { 0x24, 2400,   61000,  "DDS-2" },
510         { 0x25, 3816,   97000,  "DDS-3" },
511         { 0x26, 3816,   97000,  "DDS-4" },
512         { 0x27, 3056,   77611,  "Mammoth" },
513         { 0x28, 1491,   37871,  "X3.224" },
514         { 0x41, 3868,   98250,  "DLTapeIV(40GB)" },
515         { 0x48, 5236,   133000, "SDLTapeI(110)" },
516         { 0x49, 7598,   193000, "SDLTapeI(160)" },
517         { 0, 0, 0, NULL }
518 };
519
520 struct compression_types {
521         u_int32_t       comp_number;
522         const char      *name;
523 } comp_types[] = {
524         { 0x00, "none" },
525         { 0x00, "off" },
526         { 0x10, "IDRC" },
527         { 0x20, "DCLZ" },
528         { 0xffffffff, "enable" },
529         { 0xffffffff, "on" },
530         { 0xf0f0f0f0, NULL}
531 };
532
533 const char *
534 denstostring(int d)
535 {
536         static char buf[20];
537         struct densities *sd;
538
539         /* densities 0 and 0x7f are handled as special cases */
540         if (d == 0)
541                 return "default";
542         if (d == 0x7f)
543                 return "same";
544         for (sd = dens; sd->dens; sd++)
545                 if (sd->dens == d)
546                         break;
547         if (sd->dens == 0)
548                 sprintf(buf, "0x%02x", d);
549         else 
550                 sprintf(buf, "0x%02x:%s", d, sd->name);
551         return buf;
552 }
553
554 /*
555  * Given a specific density number, return either the bits per inch or bits
556  * per millimeter for the given density.
557  */
558 int
559 denstobp(int d, int bpi)
560 {
561         struct densities *sd;
562
563         for (sd = dens; sd->dens; sd++)
564                 if (sd->dens == d)
565                         break;
566         if (sd->dens == 0)
567                 return(0);
568         else {
569                 if (bpi)
570                         return(sd->bpi);
571                 else
572                         return(sd->bpmm);
573         }
574 }
575
576 int
577 stringtodens(const char *s)
578 {
579         struct densities *sd;
580         size_t l = strlen(s);
581
582         for (sd = dens; sd->dens; sd++)
583                 if (strncasecmp(sd->name, s, l) == 0)
584                         break;
585         return sd->dens;
586 }
587
588
589 const char *
590 getblksiz(int bs)
591 {
592         static char buf[25];
593         if (bs == 0)
594                 return "variable";
595         else {
596                 sprintf(buf, "%d bytes", bs);
597                 return buf;
598         }
599 }
600
601 const char *
602 comptostring(u_int32_t comp)
603 {
604         static char buf[20];
605         struct compression_types *ct;
606
607         if (comp == MT_COMP_DISABLED)
608                 return "disabled";
609         else if (comp == MT_COMP_UNSUPP)
610                 return "unsupported";
611
612         for (ct = comp_types; ct->name; ct++)
613                 if (ct->comp_number == comp)
614                         break;
615
616         if (ct->comp_number == 0xf0f0f0f0) {
617                 sprintf(buf, "0x%x", comp);
618                 return(buf);
619         } else
620                 return(ct->name);
621 }
622
623 u_int32_t
624 stringtocomp(const char *s)
625 {
626         struct compression_types *ct;
627         size_t l = strlen(s);
628
629         for (ct = comp_types; ct->name; ct++)
630                 if (strncasecmp(ct->name, s, l) == 0)
631                         break;
632
633         return(ct->comp_number);
634 }
635
636 void
637 st_status(struct mtget *bp)
638 {
639         printf("Mode      Density              Blocksize      bpi      "
640                "Compression\n"
641                "Current:  %-17s    %-12s   %-7d  %s\n"
642                "---------available modes---------\n"
643                "0:        %-17s    %-12s   %-7d  %s\n"
644                "1:        %-17s    %-12s   %-7d  %s\n"
645                "2:        %-17s    %-12s   %-7d  %s\n"
646                "3:        %-17s    %-12s   %-7d  %s\n",
647                denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
648                denstobp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
649                denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
650                denstobp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
651                denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
652                denstobp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
653                denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
654                denstobp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
655                denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
656                denstobp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
657
658         if (bp->mt_dsreg != MTIO_DSREG_NIL) {
659                 auto char foo[32];
660                 const char *sfmt = "Current Driver State: %s.\n";
661                 printf("---------------------------------\n");
662                 switch (bp->mt_dsreg) {
663                 case MTIO_DSREG_REST:
664                         printf(sfmt, "at rest");      
665                         break;
666                 case MTIO_DSREG_RBSY:    
667                         printf(sfmt, "Communicating with drive");
668                         break;
669                 case MTIO_DSREG_WR:
670                         printf(sfmt, "Writing");
671                         break;
672                 case MTIO_DSREG_FMK:
673                         printf(sfmt, "Writing Filemarks");
674                         break;
675                 case MTIO_DSREG_ZER:
676                         printf(sfmt, "Erasing");
677                         break;
678                 case MTIO_DSREG_RD:
679                         printf(sfmt, "Reading");
680                         break;
681                 case MTIO_DSREG_FWD:
682                         printf(sfmt, "Spacing Forward");
683                         break;
684                 case MTIO_DSREG_REV:     
685                         printf(sfmt, "Spacing Reverse");
686                         break;
687                 case MTIO_DSREG_POS:
688                         printf(sfmt,
689                             "Hardware Positioning (direction unknown)");
690                         break;
691                 case MTIO_DSREG_REW:
692                         printf(sfmt, "Rewinding");
693                         break;
694                 case MTIO_DSREG_TEN:
695                         printf(sfmt, "Retensioning");
696                         break;
697                 case MTIO_DSREG_UNL:
698                         printf(sfmt, "Unloading");
699                         break;
700                 case MTIO_DSREG_LD:
701                         printf(sfmt, "Loading");
702                         break;
703                 default:
704                         (void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg);
705                         printf(sfmt, foo);
706                         break;
707                 }
708         }
709         if (bp->mt_resid == 0 && bp->mt_fileno == (daddr_t) -1 &&
710             bp->mt_blkno == (daddr_t) -1)
711                 return;
712         printf("---------------------------------\n");
713         printf("File Number: %ld\tRecord Number: %ld\tResidual Count %d\n",
714             bp->mt_fileno, bp->mt_blkno, bp->mt_resid);
715 }
716
717 void
718 warn_eof(void)
719 {
720         fprintf(stderr,
721                 "The \"eof\" command has been disabled.\n"
722                 "Use \"weof\" if you really want to write end-of-file marks,\n"
723                 "or \"eom\" if you rather want to skip to the end of "
724                 "recorded medium.\n");
725         exit(1);
726 }
727
728 #endif /* defined (__FreeBSD__) */