home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume21 / exebyte_toc / smtops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-29  |  3.5 KB  |  185 lines

  1. #if !lint && !SABER
  2. static char RcsId[] = "$Header: smtops.c,v 1.2 89/10/27 16:14:58 mlandau Exp $";
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <sys/types.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/mtio.h>
  10.  
  11. #if REMOTE
  12. #include <rmt.h>
  13. #endif
  14.  
  15. #include "exitcodes.h"
  16. #include "smtops.h"
  17.  
  18.  
  19. /****************************************************************************
  20.  * BASIC MAGTAPE OPERATIONS FOR EXEBYTE DRIVES
  21.  ***************************************************************************/
  22.  
  23.  
  24. /*
  25.  * smt_open():  Open a tape device.
  26.  * 
  27.  * Actually, it's just a wrapper around open, but data abstraction is 
  28.  * good, right?
  29.  */
  30.  
  31.  
  32. int    smt_open(device, mode)
  33. char    *device;
  34. int    mode;
  35. {   
  36.     int    fd;
  37.     
  38.     if ((fd = open(device, mode)) < 0)
  39.     {   
  40.     perror(device);
  41.     exit(EXIT_IO);
  42.     }
  43.     return (fd);
  44. }
  45.  
  46.  
  47. /* 
  48.  * smt_close():  Close a tape device -- this is just a wrapper around close.
  49.  */
  50.  
  51. void    smt_close(tapefd)
  52. int    tapefd;
  53. {   
  54.     if (close(tapefd) < 0)
  55.     {   
  56.     perror("tape close");
  57.     exit(EXIT_IO);
  58.     }
  59. }
  60.  
  61.  
  62. /* 
  63.  * smt_close_without_eof():  Rewind and close a tape device.
  64.  * 
  65.  *     This routine provides a rewind-and-close operation, which is
  66.  *     necessary to prevent the tape device driver from adding an EOF
  67.  *     mark to the tape if the last operation before closing the device
  68.  *     was a write.  Since we are continutally rewriting the same file,
  69.  *    we need to inform the driver that we don't want a new EOF mark
  70.  *     every time we do so.
  71.  */
  72.  
  73. void    smt_close_without_eof(tapefd)
  74. int    tapefd;
  75. {   
  76.     smt_rewind(tapefd);
  77.     smt_close(tapefd);
  78. }
  79.  
  80.  
  81. /* 
  82.  * smt_read():  Read from the tape drive
  83.  * smt_write(): Write to the tape drive
  84.  * 
  85.  * 
  86.  *     This are wrappers around read and write.  They're mostly here
  87.  *     so that we can use the rmt library if we want to.
  88.  */
  89.  
  90. int    smt_read(tapefd, buffer, count)
  91. int    tapefd;
  92. char    *buffer;
  93. int    count;
  94. {   
  95.     return (read(tapefd, buffer, count));
  96. }
  97.  
  98.  
  99. int    smt_write(tapefd, buffer, count)
  100. int    tapefd;
  101. char    *buffer;
  102. int    count;
  103. {   
  104.     return (write(tapefd, buffer, count));
  105. }
  106.  
  107.  
  108. /* 
  109.  * smt_status():  Return the status of the tape drive.
  110.  * 
  111.  * This code is cribbed from the mts command.  The smt_stat structure
  112.  * looks like this:
  113.  * 
  114.  *     struct smt_stat
  115.  *     {
  116.  *         char   smt_type[8];    -- cartridge type
  117.  *        u_long smt_remain;    -- KBytes left on tape
  118.  *        u_long smt_size;    -- Total size of tape (KBytes)
  119.  *         u_long smt_ecc;        -- ECC numbers
  120.  *         long   smt_wp:1;    -- write protected?
  121.  *         long   smt_bot:1;    -- at beginning of tape?
  122.  *    }
  123.  */
  124.  
  125. struct smt_stat    *smt_status(tapefd)
  126. int         tapefd;
  127. {   
  128.     static struct smt_stat status;
  129.     
  130.     if (ioctl(tapefd, SMTIOGETSTAT, &status) < 0)
  131.     {   
  132.     perror("tape status");
  133.     exit(EXIT_IO);
  134.     }
  135.     return (&status);
  136. }
  137.  
  138.  
  139.  
  140. /* 
  141.  * smt_rewind():  Rewind a tape and verify that it worked.
  142.  */
  143.  
  144. void    smt_rewind(tapefd)
  145. int    tapefd;
  146. {   
  147.     static struct mtop rewind_op = { MTREW, 1 };
  148.     
  149.     if (ioctl(tapefd, MTIOCTOP, &rewind_op) < 0)
  150.     {   
  151.     perror("tape rewind");
  152.     exit(EXIT_IO);
  153.     }
  154. #if !REMOTE
  155.     /* rmtlib.a does not like the custom ioctls used in the smt_status
  156.        routine, so don't use it internally */
  157.     if (!smt_status(tapefd)->smt_bot)
  158.     {   
  159.     fputs("Could not rewind tape for some unknown reason.");
  160.     exit(EXIT_IO);
  161.     }
  162. #endif
  163. }
  164.  
  165.  
  166. /* 
  167.  * smt_eof():  Write an EOF mark on the tape.
  168.  * 
  169.  * We will probably never need this, but it doesn't hurt to include it.
  170.  */
  171.  
  172. void    smt_eof(tapefd)
  173. int    tapefd;
  174. {   
  175.     static struct mtop eof_op = { MTWEOF, 1 };
  176.     
  177.     if (ioctl(tapefd, MTIOCTOP, &eof_op) < 0)
  178.     {   
  179.     perror("write eof");
  180.     exit(EXIT_IO);
  181.     }
  182. }
  183.  
  184.  
  185.