home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / 2.11BSD / maketape.c < prev    next >
C/C++ Source or Header  |  1991-04-29  |  2KB  |  104 lines

  1. /*
  2.  * Copyright (c) 1986 Regents of the University of California.
  3.  * All rights reserved.  The Berkeley software License Agreement
  4.  * specifies the terms and conditions for redistribution.
  5.  *
  6.  *    @(#)maketape.c    1.1 (2.10BSD Berkeley) 12/1/86
  7.  *                (2.11BSD Contel) 4/20/91
  8.  *        TU81s didn't like open/close/write at 1600bpi, use
  9.  *        ioctl to write tape marks instead.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/mtio.h>
  16.  
  17. #define MAXB 30
  18.  
  19. extern    int errno;
  20.  
  21. char    buf[MAXB * 512];
  22. char    name[50];
  23. struct    mtop mtio;
  24. int    blksz, recsz;
  25. int    mt;
  26. int    fd;
  27. int    cnt;
  28.  
  29. main(argc, argv)
  30.     int argc;
  31.     char *argv[];
  32. {
  33.     register int i, j = 0, k = 0;
  34.     FILE *mf;
  35.  
  36.     if (argc != 3) {
  37.         fprintf(stderr, "usage: maketape tapedrive makefile\n");
  38.         exit(1);
  39.     }
  40.     if ((mt = creat(argv[1], 0666)) < 0) {
  41.         perror(argv[1]);
  42.         exit(1);
  43.     }
  44.     if ((mf = fopen(argv[2], "r")) == NULL) {
  45.         perror(argv[2]);
  46.         exit(1);
  47.     }
  48.  
  49.     for (;;) {
  50.         if ((i = fscanf(mf, "%s %d", name, &blksz))== EOF)
  51.             exit(0);
  52.         if (i != 2) {
  53.             fprintf(stderr, "Help! Scanf didn't read 2 things (%d)\n", i);
  54.             exit(1);
  55.         }
  56.         if (blksz <= 0 || blksz > MAXB) {
  57.             fprintf(stderr, "Block size %d is invalid\n", blksz);
  58.             exit(1);
  59.         }
  60.         recsz = blksz * 512;    /* convert to bytes */
  61.         if (strcmp(name, "*") == 0) {
  62.             mtio.mt_op = MTWEOF;
  63.             mtio.mt_count = 1;
  64.             if (ioctl(mt, MTIOCTOP, &mtio) < 0)
  65.                 fprintf(stderr, "MTIOCTOP err: %d\n", errno);
  66.             k++;
  67.             continue;
  68.         }
  69.         fd = open(name, 0);
  70.         if (fd < 0) {
  71.             perror(name);
  72.             exit(1);
  73.         }
  74.         printf("%s: block %d, file %d\n", name, j, k);
  75.  
  76.         /*
  77.          * wfj fix to final block output.
  78.          * we pad the last record with nulls
  79.          * (instead of the bell std. of padding with trash).
  80.          * this allows you to access text files on the
  81.          * tape without garbage at the end of the file.
  82.          * (note that there is no record length associated
  83.          *  with tape files)
  84.          */
  85.  
  86.         while ((cnt=read(fd, buf, recsz)) == recsz) {
  87.             j++;
  88.             if (write(mt, buf, cnt) < 0) {
  89.                 perror(argv[1]);
  90.                 exit(1);
  91.             }
  92.         }
  93.         if (cnt>0) {
  94.             j++;
  95.             bzero(buf + cnt, recsz - cnt);
  96.             if (write(mt, buf, recsz) < 0) {
  97.                 perror(argv[1]);
  98.                 exit(1);
  99.             }
  100.         }
  101.     close(fd);
  102.     }
  103. }
  104.