home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / mt / mt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-06  |  5.7 KB  |  227 lines

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