//#include #include #include #include #include int fh; struct mtget stblk; struct mtop opblk; #define BLKLEN 512 #define RBLKLEN 512 //#define RBLKLEN 65535 // var blks only char buf [BLKLEN] = "0123456789"; void disp_stat () { int stat; char buf [256]; ioctl (fh, MTIOCGET, (char*)&stblk); stat = stblk.mt_gstat; /* Display tape status */ sprintf (buf, "Status: %8.8X", stat); if (GMT_EOF(stat)) strcat (buf, " EOF"); if (GMT_BOT(stat)) strcat (buf, " BOT"); if (GMT_EOT(stat)) strcat (buf, " EOT"); if (GMT_SM(stat)) strcat (buf, " SET-MARK"); if (GMT_EOD(stat)) strcat (buf, " EOD"); if (GMT_WR_PROT(stat)) strcat (buf, " WR-PROT"); if (GMT_ONLINE(stat)) strcat (buf, " ON-LINE"); if (GMT_D_6250(stat)) strcat (buf, " 6250"); if (GMT_D_1600(stat)) strcat (buf, " 1600"); if (GMT_D_800(stat)) strcat (buf, " 800"); if (GMT_DR_OPEN(stat)) strcat (buf, " NO-TAPE"); printf ("ST0: %s\n", buf); }; void common () { printf ("stblk.mt_blkno=%d\n", stblk.mt_blkno); }; void set_blk (int len) { int rc; if (len) printf ("Setting length %d records.", len); else printf ("Setting variable records."); fflush (stdout); opblk.mt_op = MTSETBLK; opblk.mt_count = len; rc = ioctl (fh, MTIOCTOP, (char*)&opblk); printf (" rc=%d\n", rc); }; void my_read (int fh) { int rc; int en; char buf2 [RBLKLEN]; printf ("read..."); fflush (stdout); memset (buf2, ' ', 10); errno = 0; rc = read (fh, buf2, RBLKLEN); en = errno; printf (" rc=%d, errno=%d\n", rc, en); buf2 [10] = 0; printf ("%s\n", buf2); disp_stat (); common (); }; void my_read1 (int fh) { int rc; int en; char buf2 [1]; printf ("read1..."); fflush (stdout); memset (buf2, ' ', 10); errno = 0; rc = read (fh, buf2, 1); en = errno; printf (" rc=%d, errno=%d\n", rc, en); buf2 [1] = 0; printf ("%s\n", buf2); disp_stat (); common (); }; void my_write (int fh) { int rc; int en; printf ("write..."); fflush (stdout); errno = 0; rc = write (fh, buf, BLKLEN); en = errno; printf (" rc=%d, errno=%d\n", rc, en); disp_stat (); common (); }; void my_bksp (int fh) { int rc; printf ("backspace..."); fflush (stdout); opblk.mt_op = MTBSR; opblk.mt_count = 1; rc = ioctl (fh, MTIOCTOP, (char*)&opblk); printf (" rc=%d\n", rc); disp_stat (); common (); }; void my_rew (int fh) { int rc; printf ("rewind..."); fflush (stdout); opblk.mt_op = MTREW; opblk.mt_count = 1; rc = ioctl (fh, MTIOCTOP, (char*)&opblk); printf (" rc=%d\n", rc); disp_stat (); }; int main (int argc) { int rc; printf ("Opening Tape Handle\n"); fh = open ("/dev/st0", O_RDWR); if (fh >= 0) { rc = ioctl (fh, MTIOCGET, (char*)&stblk); if (rc < 0) { printf ("Failed to IOCTL the Tape\n"); } else { common (); }; disp_stat (); set_blk (BLKLEN); disp_stat (); my_rew (fh); buf [0] = 'A'; buf [1] = 'C'; my_write (fh); my_bksp (fh); if (argc == 1) my_read (fh); else my_read1 (fh); // special 1 char read buf [0] = 'B'; buf [1] = 'D'; my_write (fh); my_bksp (fh); my_read (fh); my_rew (fh); printf ("Closing Tape Handle\n"); close (fh); } else { printf ("Failed to Open Tape\n"); }; return (0); };