home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / tapeutils.zip / tuuros.c < prev    next >
C/C++ Source or Header  |  1990-08-03  |  7KB  |  251 lines

  1. /*
  2.  * ostape.c: reads OS standard labeled tapes, using dd to do the real work.
  3.  *
  4.  * Usage: ostape
  5.  *
  6.  * Christine Gianone, CUCCA, December 1986
  7.  *
  8.  */
  9.  
  10. /* Preprocessor Stuff */
  11.  
  12. #include <stdio.h>
  13. #include <ctype.h>
  14.  
  15. #define MAXLEN 90            /* Max length for a label line */
  16. #define MAXFN  17            /* Max length for filename */
  17.  
  18. #define XVOL1 0                /* Symbols for label identifiers */
  19. #define XEOV1 1 
  20. #define XHDR1 2
  21. #define XHDR2 3
  22. #define XEOF1 4
  23. #define XEOF2 5
  24. #define XUVL1 6
  25. #define XUHL1 7
  26. #define XUTL1 8
  27.  
  28. /* Global Declarations */
  29.  
  30. char buff[MAXLEN];            /* Input line buffer */
  31. FILE *fp, *fopen();            /* File pointer & open function */
  32.  
  33. struct lbl {                /* Label ids and case indexes */
  34.     char *id;
  35.     int code;
  36. };
  37.  
  38. struct lbl tbl[10] = {
  39.     "VOL1", XVOL1,            /* Volume label */
  40.     "EOV1", XEOV1,            /* End-of-Volume */
  41.     "HDR1", XHDR1,            /* Header 1 */
  42.     "HDR2", XHDR2,            /* Header 2 */
  43.     "EOF1", XEOF1,            /* End of file 1 */
  44.     "EOF2", XEOF2,            /*  and 2 */
  45.     "UVL1", XUVL1,            /* User labels */
  46.     "UHL1", XUHL1,            /*  are */
  47.     "UTL1", XUTL1            /*  ignored... */
  48. };
  49.  
  50. int nids = (sizeof(tbl) / sizeof(struct lbl));
  51.  
  52. extern int errno;            /* System error stuff */
  53. extern char *sys_errlist[];
  54.  
  55. char vn[6];                /* Volume name */
  56. char fn[MAXFN];                /* File name */
  57. int bs;                    /* Blocksize */
  58. int rl;                    /* Record length */
  59. int bc;                    /* Block count in EOF1 */
  60. char rf;                /* Record format */
  61.  
  62. char *ddhdr = "dd if=/dev/rmt12 of=hdr.tmp ibs=800 cbs=80 conv=unblock,ascii";
  63.  
  64. /* Main function */
  65.  
  66. main(argc,argv)
  67.     int argc;                /* Command line arguments */
  68.     char *argv[];            /* (not used) */
  69.     {
  70.     int x;                /* Declare local variables */
  71.     int files = 0;            /* Files processed */
  72.     int skip = 0;            /* Files skipped */
  73.     char ddcmd[200];
  74.  
  75.     for (files = 0 ; ; )
  76.       {
  77.           unlink("hdr.tmp");    /* Delete temp header file */
  78.           system(ddhdr);
  79.  
  80.           if ((x = phdr()) < 0)    /* Process the header */
  81.         break;
  82.           else if (x == 0)
  83.         {
  84.             printf("dd cannot read %s in %c format", fn, rf);
  85.             system("mt fsf 2");    /* Skip forward 2 files */
  86.             skip++;        /* to next tape header. */
  87.             continue;
  88.         }
  89.           sprintf(ddcmd, 
  90.                      "dd if=/dev/rmt12 of=%s ibs=%d cbs=%d conv=unblock,ascii",
  91.               fn,bs,rl);
  92.           system(ddcmd);        /* Run the dd command above */
  93.           sprintf(ddcmd, "ls -l %s", fn); /* to read the tape file */
  94.           files++;            /* Count the file. */
  95.           system(ddcmd);        /* Run the list command above */
  96.  
  97.           unlink("eof.tmp");    /* Delete old temp trailer file */
  98.           system(ddhdr);
  99.  
  100.           if ((x = peof()) < 0)    /* Process trailer labels */
  101.         break;
  102.       }
  103.     unlink("hdr.tmp");
  104.     unlink("eof.tmp");
  105.     printf("All done! Files Read: %d -- Files Skipped: %d\n", files, skip);
  106.  
  107.     }
  108.  
  109. /* Process a header ... */
  110. /* Returns 1 valid file header with recfm F, */
  111. /*  0 if valid file header with some other recfm (which dd can't handle), or */
  112. /* -1 otherwise - fatal error or end of tape */
  113.  
  114. phdr()
  115. {    
  116.     int i, j, k, l = 0;
  117.     char c;
  118.  
  119.     *fn=bs=rl=rf=0;            /* Initialize file variables */
  120.     fp = fopen("hdr.tmp", "r");        /* Try to open header file */
  121.     if (fp == NULL)            /* Check for errors */
  122.       {
  123.       printf("%s\n",sys_errlist[errno]); /* Oops, can't... */
  124.       return(-1);
  125.       }
  126.     while (fgets(buff, MAXLEN, fp) != NULL) /* Read each line */
  127.       {
  128.       switch (k = lookup()) {    /* Got line, look up label id */
  129.         case XVOL1:            /* VOL1 */
  130.           buff[10] = 0;
  131.           printf("VOL: '%s'\n", buff+4); /* Print volid */
  132.           break;
  133.  
  134.         case XEOV1:            /* End of volume */
  135.           printf("End of volume\n");
  136.           return(-1);
  137.           
  138.         case XHDR1:            /* File header 1 */
  139.           for (i = 0; i < MAXFN; i++) /* Copy name */
  140.         {
  141.             c = buff[i+4];    /* and convert to lower case */
  142.             fn[i] = isupper(c) ? tolower(c) : c;
  143.         }
  144.           for (i = MAXFN - 1; i > 0; i--) /* Trim trailing blanks */
  145.         {
  146.             if (fn[i] == ' ')
  147.               fn[i] = '\0';
  148.             else break;
  149.         }
  150.           printf("Filename:  '%s'\n", fn); /* Print the name */
  151.           break;
  152.  
  153.         case XHDR2:            /* File header 2 */
  154.           buff[15] = 0;
  155.           rl = atoi (buff + 10);    /* Record length */
  156.           buff[10] = 0;
  157.           bs = atoi (buff + 5);    /* Block size */
  158.           rf = buff[4];        /* Record format */
  159.           printf ("rl: %d, bs: %d, rf: %c\n", rl, bs, rf);
  160.           break;
  161.  
  162.         case XUVL1:            /* User headers */
  163.         case XUHL1:            /* (ignored) */
  164.           break;
  165.  
  166.         default:
  167.           printf("Unexpected ID header label:\n%s\n",buff);
  168.           if(++l > 3)        /* Give up if too many, */
  169.         return(-1);        /* probably not real headers */
  170.           break;
  171.       }
  172.       }
  173.     if (fclose(fp) == EOF)        /* Close the file */
  174.       {
  175.       printf("%s\n",sys_errlist[errno]);
  176.       return(-1);
  177.       }
  178.     if (*fn != '\0' )            /* If we have a filename */
  179.       return((rf == 'F') ? 1 : 0);    /* return 1 or 0 based on recfm */
  180.     else return(-1);            /* otherwise probably end of tape */
  181. }
  182.  
  183. /* Process trailer labels... */
  184. /*  Returns -1 if the trailer label is invalid, otherwise 0. */
  185.  
  186. peof()
  187. {    
  188.     int i, j, k, l = 0;            /* Local variables */
  189.     char c;
  190.     bc = 0;                /* Block count */
  191.  
  192.     fp = fopen("eof.tmp", "r");        /* Try to open trailer label file */
  193.     if (fp == NULL)            /* Check for errors */
  194.       {
  195.       printf("%s\n",sys_errlist[errno]);
  196.       return(-1);
  197.       }
  198.     while (fgets(buff, MAXLEN, fp) != NULL) /* Read each line */
  199.       {
  200.       switch (k = lookup())        /* Got line, look up label id */
  201.         {
  202.           case XEOF1:        /* End of file 1 */
  203.         buff[60] = 0;
  204.         bc = atoi (buff + 54);    /* Get block count & print it */
  205.         printf("EOF:  %d block%c\n", bc, (bc == 1) ? ' ' : 's');
  206.         break;
  207.         
  208.           case XEOV1:        /* End of volume */
  209.         printf("End of volume");
  210.         return(-1);
  211.  
  212.           case XEOF2:        /* End of file 2 */
  213.         break;
  214.  
  215.           case XUTL1:        /* User trailer */
  216.         break;            /*  (ignored) */
  217.  
  218.           default:
  219.         printf("Unexpected ID in trailer label:\n%s\n",buff);
  220.         if (++l > 3)        /* If too many unknowns, */
  221.           return(-1);        /* probably not a real trailer. */
  222.         break;
  223.         }
  224.       }
  225.     if (fclose(fp) == EOF)        /* Close the file */
  226.       {
  227.       printf("%s\n",sys_errlist[errno]);
  228.       return(-1);
  229.       }
  230.     return(0);
  231. }
  232.  
  233. /* Lookup */
  234.  
  235. lookup()                /* Look up label ID, */
  236. {                    /* return case index, or -1. */
  237.     int i, j;
  238.  
  239.     for (j = 0; j < nids; j++)        /* For each ID in our list (row) */
  240.       {
  241.       for (i = 0; i < 4; i++)    /* Compare each character (column) */
  242.         {
  243.         if (buff[i] != tbl[j].id[i]) /* If chars differ, */
  244.           break;        /* then try next row, */
  245.         else continue;        /* otherwise keep comparing. */
  246.         }
  247.       if (i == 4) return (tbl[j].code); /* i is 4 if all chars match. */
  248.       }
  249.     return (-1);            /* No match, return -1. */
  250. }
  251.