home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8707 / 45 / taprd.c < prev   
Encoding:
C/C++ Source or Header  |  1990-07-13  |  1.7 KB  |  108 lines

  1. /*
  2. **    this creation just reads raw tape file specified as
  3. **    1st argument.  it then reports on record size etc.  on
  4. **    double tapemark it stops.
  5. */ 
  6.  
  7. #include    <sys/types.h>
  8. #include    <sys/stat.h>
  9. #include    <signal.h>
  10. #include    <stdio.h>
  11. #include    <fcntl.h>
  12.  
  13. #define        NBUF    44    /* Maximum size tape block likely to be encountered */
  14. #define        NERR    10    /* NERR consecutive ==> giveup */
  15.  
  16. char        buf[NBUF*BUFSIZ];
  17. int        flag;
  18. int        err;
  19.  
  20. catch(s)
  21.     int    s;
  22. {
  23.     signal(s, SIG_IGN);
  24.     flag = 1;
  25. }
  26.  
  27. main(argc, argv)
  28.     int        argc;
  29.     char **        argv;
  30. {
  31.     register    iold;
  32.     register    i;
  33.     register    nrec;
  34.     register    infd;
  35.     struct stat    sbuf;
  36.  
  37.     signal(SIGHUP, catch);
  38.     if ( signal(SIGINT, SIG_IGN) == SIG_DFL )
  39.         signal(SIGINT, catch);
  40.     if ( signal(SIGQUIT, SIG_IGN) == SIG_DFL )
  41.         signal(SIGQUIT, catch);
  42.  
  43.     if(argc != 2)
  44.     {
  45.         fprintf(stderr, "Usage: %s file\n", argv[0]);
  46.         return 2;
  47.     }
  48.  
  49.     if((infd = open(argv[1], O_RDONLY)) == -1)
  50.     {
  51.         perror(argv[1]);
  52.         return 2;
  53.     }
  54.  
  55.     fstat(infd, &sbuf);
  56.     if((sbuf.st_mode&S_IFMT) != S_IFCHR)
  57.     {
  58.         fprintf(stderr, "%s not a raw device !!\n", argv[1]);
  59.         return 2;
  60.     }
  61.  
  62.     nrec = 0;
  63.     iold = read(infd, buf, sizeof buf);
  64.     for(;;)
  65.     {
  66.         nrec++;
  67.         i = read(infd, buf, sizeof buf);
  68.         if(i == iold && i == 0)
  69.             goto fini;    /* considered end-of-tape */
  70.         if ( (i==iold) && (i==-1) && (nrec==NERR) )
  71.         {
  72.             printf("%d consecutive errors: EOT assumed\n",NERR);
  73.             return 1;
  74.         }
  75.         if((i == iold) && !flag)
  76.             continue;
  77.         if(iold)
  78.         {
  79.             if(iold != -1)
  80.             {
  81.                 printf("%6d. * %6o(%6d.)\n",
  82.                     nrec, iold, iold);
  83.             }
  84.             else
  85.             {
  86.                 printf("%6d. *  ERR  \n", nrec);
  87.                 err++;
  88.                 if(nrec > 1)
  89.                     return err;
  90.             }
  91.         }
  92.         else
  93.         {
  94.             printf("\tTM\n");
  95.         }
  96.         nrec = 0;
  97.         iold = i;
  98.         if(flag)
  99.         {
  100.             printf("Interrupted\n");
  101.             return 1;
  102.         }
  103.     }
  104. fini:
  105.     printf("\tEOT\n");
  106.     return err;
  107. }
  108.