home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1679 / wt.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  5.5 KB  |  183 lines

  1. /*    @(#) wt.c - a Wangtek command utility.
  2.  *    A utility to issue the ioctl(2) commands to retension
  3.  *    and/or erase cartridge tapes using the Wangtek (wt) driver.
  4.  *    There was no way to accomplish this provided with the system,
  5.  *    and while trivial, one needs to be able to retension new tapes
  6.  *    and sometimes erase cartridges, to remove records that confuse
  7.  *    the less-than-great Wangtek driver provided by ISC.  Some other
  8.  *    basic ops added as time passed that, while possible by proper
  9.  *    command line operations using various commands, these were awkward
  10.  *    and error prone.  This kludge helps a little bit.
  11.  *    Of course, the REAL answer is a decent driver, and proper support
  12.  *    utilities.
  13.  *
  14.  *    This thing will try /dev/rmt0, if that fails, it will look at
  15.  *    /dev/tape, then /dev/rSA/rmt0.  If given a name via -f option it
  16.  *    will try only that name.
  17.  *
  18.  *    Tossed together (literally) by pat@rwing Fri Aug 10 22:54:44 PDT 1990
  19.  *
  20.  *    Placed in the Public Domain by pat@rwing.
  21.  */
  22.  
  23. #include <sys/wtioctl.h>
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27.  
  28. #define    NOENTS 4
  29.  
  30. /* HUNK
  31.  * size of read() requests when scanning over data - I heard that ctg
  32.  * tapes block at 63k, performance trials tend to bear this out.
  33.  */
  34. #define HUNK 64512
  35.  
  36. void    exit();        /* to keep lint(1) smiling */
  37. void    usage();
  38.  
  39. /* usual default names for the normal rewind-on-close device */
  40. char *tape[NOENTS] = {
  41.     "/dev/rmt0",
  42.     "/dev/tape",
  43.     "/dev/rSA/tape",
  44.     (char *)NULL
  45. };
  46.  
  47. /* usual default names for the NO rewind-on-close device */
  48. char *ntape[NOENTS] = {
  49.     "/dev/rnmt0",
  50.     "/dev/ntape",
  51.     "/dev/rSA/ntape",
  52.     (char *)NULL
  53. };
  54.  
  55. /* foundation of a couple of commonly used error messages */
  56. char msgfmt[] = "Unable to open tape for %s\n";
  57. char errnofmt[] = "Return from errno = %d\n";
  58. extern int errno;
  59.  
  60. main(argc, argv)
  61. int argc;
  62. char **argv;
  63. {
  64.     int fd, i, j, count, result;
  65.     char scratch[HUNK];    /* buffer for reads to scan over data */
  66.     char *cname=argv[0];
  67.  
  68.     /* see if user specifies a device, and if so, get its name */
  69.     if (*argv[1] == '-') {
  70.     if (argv[1][1] == 'f') {
  71.         if (argv[1][2] != '\0') {
  72.         ntape[0] = tape[0] = &argv[1][2];
  73.         argv += 1; argc -= 1;
  74.         } else {
  75.         ntape[0] = tape[0] = argv[2];
  76.         argv += 2; argc -= 2;
  77.         }
  78.         ntape[1] = tape[1] = (char *)NULL; /* lock out default names */
  79.     } else {
  80.         (void) fprintf(stderr,
  81.               "\n%s: %2.2s -- invalid option\n\n", cname, argv[1]);
  82.         usage(cname);
  83.         exit(1);
  84.     }
  85.     }
  86.  
  87.     /* erase tape */
  88.     if (!strncmp(argv[1], "erase", 3)){
  89.     /* search for a working device */
  90.     for (i = 0; tape[i] != NULL && (fd = open(tape[i], O_WRONLY)) < 0; i++)
  91.             ;
  92.     if (fd < 0) {        /* open failed, complain and exit */
  93.         (void) fprintf(stderr, msgfmt, "writing");
  94.         (void) perror("");
  95.         exit(errno);    /* in case a script can use it */
  96.     }
  97.     if (ioctl(fd, WTQICMD, ERASE) <0)
  98.         perror("erase ioctl failed");
  99.  
  100.     (void) close(fd);    /* if close fails, nothing we can do about it */
  101.     } /* end erase */
  102.  
  103.     else if (!strncmp(argv[1], "retens", 3)) {    /* retension tape */
  104.     for (i = 0; tape[i] != NULL && (fd = open(tape[i], O_RDONLY)) < 0; i++)
  105.             ;
  106.     if (fd < 0) {
  107.         (void) fprintf(stderr, msgfmt, "reading");
  108.         (void) perror("");
  109.         exit(errno);
  110.     }
  111.     if (ioctl(fd, WTQICMD, RETENS) < 0)
  112.         perror("retension ioctl failed");
  113.  
  114.     (void) close(fd);
  115.     } /* end retension */
  116.  
  117.     else if (!strncmp(argv[1], "rewind", 3)) {    /* rewind */
  118.     for (i = 0; tape[i] != NULL && (fd = open(tape[i], O_RDONLY)) < 0; i++)
  119.             ;
  120.     if (fd < 0) {
  121.         (void) fprintf(stderr, msgfmt, "reading");
  122.         (void) perror("");
  123.         exit(errno);
  124.     }
  125.     (void) close(fd); /* no rewind ioctl(), so fake it with open/close */
  126.     } /* end rewind */
  127.  
  128.     /* This operation is faked, so it does not handle requests
  129.      * to skip over more files (archives) than exist on the tape.
  130.      * Still, its useful to position the tape if one knows the number
  131.      * of recordings are on the ctg.
  132.      */
  133.  
  134.     else if (!strncmp(argv[1], "fsf", 3)) {    /* forward skip over archive */
  135.     count = atoi(argv[2]);
  136.     count = (count == 0 ? 1 : count);
  137.     for (j = 0; j < count; j++) {
  138.         for (i = 0; ntape[i] != NULL    /* split to keep on screen */
  139.         && (fd = open(ntape[i], O_RDONLY)) < 0; i++)
  140.                 ;
  141.         if (fd < 0) {
  142.         (void) fprintf(stderr, msgfmt, "reading");
  143.         (void) perror("");
  144.         exit(errno);
  145.         }
  146.  
  147.         while((result = read(fd, scratch, HUNK)) > 0)
  148.                 ;
  149.  
  150.         if (result < 0) {
  151.         perror("past EOD - rewind tape to use");
  152.         exit(1);
  153.         }
  154.         (void) close(fd); /* closing tape opened as ntape holds position */
  155.     }
  156.     } /* end fwd skip file */
  157.  
  158.     else {  /* unrecognized cmd */
  159.     usage(cname);    /* tell user how to use this thing */
  160.     exit(1);    /* give a failed exit */
  161.     }
  162.     exit(0);
  163.     /*NOTREACHED*/
  164. }
  165.  
  166. void usage(cname)
  167. char *cname;
  168. {
  169.     
  170.     /* void casts are to make lint shut up */
  171.     (void) fprintf(stderr, "Command utility for Wangtek ctg drives.\n");
  172.     (void) fprintf(stderr, "Usage: %s [-f device] command \n", cname);
  173.     (void) fprintf(stderr, "command is one of:\n");
  174.     (void) fprintf(stderr, "    erase - erase tape\n");
  175.     (void) fprintf(stderr, "    retens - retension tape \n");
  176.     (void) fprintf(stderr, "    rewind - rewind a tape left unwound\n");
  177.     (void) fprintf(stderr, "    fsf [n] - skip over file or archive on tape\n");
  178.     (void) fprintf(stderr, "if an open fails, wt tries other names for ");
  179.     (void) fprintf(stderr, "the tape device,\nin case a node wasn't");
  180.     (void) fprintf(stderr, "created, or was removed.  If a name is given by\n");
  181.     (void) fprintf(stderr, "the -f option, only that name will be tried.\n");
  182. }
  183.