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