New tape manipulation tools

rawtap	Allows extract, create and append operations on .tap files.

cpytap	Copies a .tap file to a new .tap file while allowing file level edits; skip file, replace file,
		append files and insert files. Any files copied from the original source .tap will have
		their internal record structure maintained.

cosy		COSY is the compressed format used by the CDC1700. This program allows for
		extraction of all files from an archive and the creation of a new archive. It assumes
		that you would have used raw tap about to have extracted the COSY file from a
		tape.

dbtap	Utility to read, write and list .tap containers written in the DOS/BATCH-11 format. It
		understands ascii and binary modes and can be used to transfer files in and out of
		most PDP-11 operating systems (not sure about RSTS/E), early VMS and early
		TOPS-10 systems.
This commit is contained in:
John Forecast
2017-03-14 13:44:02 -07:00
committed by Mark Pizzolato
parent 94d94fe695
commit b08ebe8eb0
29 changed files with 4978 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
# all of these can be over-ridden on the "make" command line if don't suit
# your environment
TOOL=cpytap
CFLAGS=-O2 -Wall -Wshadow -Wextra -pedantic -Woverflow -Wstrict-overflow
BIN=/usr/local/bin
INSTALL=install
CC=gcc
$(TOOL): $(TOOL).c tapeio.c tapeio.h tap.h defs.h
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $(TOOL) $(TOOL).c tapeio.c $(LDLIBS)
.PHONY: clean install uninstall
clean:
rm -f $(TOOL)
install: $(TOOL)
$(INSTALL) -p -m u=rx,g=rx,o=rx $(TOOL) $(BIN)
uninstall:
rm -f $(BIN)/$(TOOL)

441
extracters/cpytap/cpytap.c Normal file
View File

@@ -0,0 +1,441 @@
/* cpytap.c: copy SIMH .tap container with file changes
Copyright (c) 2015-2017, John Forecast
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of John Forecast shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from John Forecast.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "tapeio.h"
#define MINRECLEN 1
#define MAXRECLEN 65536
#define DEFRECLEN 10240
int reclen = DEFRECLEN;
/*
* We maintain an array of requests indexed by the file # on the source tape.
*/
#define MAXFILES 100 /* Max files on tape */
#define APPFILES 20 /* Max append files */
struct info {
int reclen; /* Record length to be used */
char *filename; /* Filename */
};
struct fileop {
int used; /* Use count */
int skipfile; /* Skip this file */
struct info repfile; /* Replace with this file */
struct info insfiles[APPFILES]; /* Insert these files (in order) */
} fileops[MAXFILES + 1];
struct fileop appendops;
char *srcfile = NULL, *dstfile = NULL;
FILE *src = NULL, *dst = NULL;
/*++
* usage
*
* Display a usage message on stderr and exit.
*
* Inputs:
*
* None
*
* Outputs:
*
* None
*
* Returns:
*
* Never returns
*
--*/
void usage(void)
{
fprintf(stderr,
"Usage: cpytap src dst [-r len] [-S n] [-I n,file] [-R n,file] [-A file]\n");
fprintf(stderr,
" Copy src .tap file to destination maintaining the internal\n"
" record structure. The switches control file level changes to\n"
" destination tape.\n");
fprintf(stderr, "\nSwitches:\n\n");
fprintf(stderr,
"-r len - Specify max record size when writing new files.\n"
" There may be multiple \"-r\" switches on the\n"
" command line if the new files need different\n"
" record sizes. (%u <= len <= %u, default %u)\n"
" If len is outside the supported range, it will be\n"
" quietly modified to the minimum or maximum value.\n",
MINRECLEN, MAXRECLEN, DEFRECLEN);
fprintf(stderr,
"-I n,file - Insert specified file before file n of the source tape.\n");
fprintf(stderr,
"-R n,file - Replace file n of the source tape with the specified file.\n");
fprintf(stderr,
"-S n - Skip file n from the source tape.\n");
fprintf(stderr,
"-A file - Append the specified file after all the files on\n"
" the source tape have been copied to the destination.\n");
fprintf(stderr,
"\nFiles are number 1 - N according to their position on the\n"
"source tape.\n");
exit(1);
}
/*++
* copyFile
*
* Copy the next file from the source tape to the destination tape
* replicating the record structure up to and including the tape mark.
*
* Inputs:
*
* None
*
* Outputs:
*
* ...
*
* Returns:
*
* ST_EOM - End of medium detected
* ST_TM - Tape mark detected
*
--*/
static unsigned int copyFile(void)
{
char record[MAXRCLNT];
uint32 len;
for (;;) {
switch (len = ReadTapeRecord(src, record, sizeof(record))) {
case ST_EOM:
return ST_EOM;
case ST_TM:
if (WriteTapeMark(dst, 0) == 0)
return ST_TM;
fprintf(stderr, "Error writing tape mark to destination tape\n");
exit(6);
default:
if (WriteTapeRecord(dst, record, len) == 0)
continue;
fprintf(stderr, "Error writing record to destination tape\n");
exit(6);
}
}
}
/*++
* writeFile
*
* Write a file at the current position on the destination tape.
*
* Inputs:
*
* filename - Pointer to name of file to be written
* rlen - Max record size to use
*
* Outputs:
*
* ...
*
* Returns:
*
* None
*
--*/
static void writeFile(
char *filename,
int rlen
)
{
FILE *file;
size_t datalen;
char record[MAXRCLNT];
if ((file = fopen(filename, "r")) != NULL) {
while ((datalen = fread(record, sizeof(char), rlen, file)) != 0) {
if (ferror(file)) {
fprintf(stderr, "Error reading %s\n", filename);
exit(4);
}
if (WriteTapeRecord(dst, record, rlen) != 0) {
fprintf(stderr, "Error writing record to destination tape\n");
exit(5);
}
}
if (WriteTapeMark(dst, 0) != 0) {
fprintf(stderr, "Error writing tape mark to destination tape\n");
exit(5);
}
fclose(file);
return;
}
fprintf(stderr, "Failed to open file - %s\n", filename);
exit(3);
}
/*++
* parse
*
* Parse a command line switch argument of the form "n,filename" where n
* is an integerin the range 1 - MAXFILES.
*
* Inputs:
*
* arg - Pointer to "n,filename" string
* file - Pointer to int to receive "n"
* name - Pointer to char* to receive filename
*
* Outputs:
*
* ...
*
* Returns:
*
* 0 - Success
* -1 - Invalid argument format
*
--*/
int parse(
char *arg,
int *file,
char **name
)
{
char *endptr;
*file = strtoul(arg, &endptr, 0);
if ((*endptr == ',') && (*++endptr == '\0'))
return -1;
if ((*file == 0) || (*file > MAXFILES))
return -1;
*name = endptr;
return 0;
}
/*++
* main
*
* Entry point for cpytap program.
*
* Inputs:
*
* argc - # of supplied arguments
* argv - Array of argument strings
*
* Outputs:
*
* None
*
* Returns:
*
* Exit status for cpytap
*
--*/
int main(
int argc,
char *argv[]
)
{
int ch, i, filenumber = 1, filenum;
char *filename;
if (argc < 3)
usage();
srcfile = argv[1];
dstfile = argv[2];
argc -= 2;
argv += 2;
memset(fileops, 0, sizeof(fileops));
memset(&appendops, 0, sizeof(appendops));
while ((ch = getopt(argc, argv, "r:A:I:R:S:")) != -1) {
switch (ch) {
case 'r':
reclen = strtoul(optarg, NULL, 0);
if (reclen < MINRECLEN)
reclen = MINRECLEN;
if (reclen > MAXRECLEN)
reclen = MAXRECLEN;
done:
break;
case 'A':
for (i = 0; i < APPFILES; i++) {
if (appendops.insfiles[i].filename == NULL) {
appendops.insfiles[i].reclen = reclen;
appendops.insfiles[i].filename = argv[optind];
goto done;
}
}
fprintf(stderr, "No space for appending file - %s\n", argv[optind]);
return 5;
case 'I':
if (parse(optarg, &filenum, &filename) != 0) {
fprintf(stderr, "Invalid argument - -I %s\n", optarg);
return 7;
}
for (i = 0; i < APPFILES; i++) {
if (fileops[filenum].insfiles[i].filename == NULL) {
fileops[filenum].insfiles[i].reclen = reclen;
fileops[filenum].insfiles[i].filename = filename;
goto done;
}
}
fprintf(stderr, "No space for inserting file - %s\n", filename);
return 5;
case 'R':
if (parse(optarg, &filenum, &filename) != 0) {
fprintf(stderr, "Invalid argument - -R %s\n", optarg);
return 7;
}
if (fileops[filenum].repfile.filename != NULL) {
fprintf(stderr, "File %u is already being replaced.\n", filenum);
return 8;
}
fileops[filenum].repfile.reclen = reclen;
fileops[filenum].repfile.filename = filename;
break;
case 'S':
filenum = strtoul(optarg, NULL, 0);
if ((filenum == 0) || (filenum > MAXFILES)) {
fprintf(stderr, "Invalid file number - -D %u\n", filenum);
return 6;
}
fileops[filenum].skipfile = 1;
break;
case '?':
default:
usage();
}
}
/*
* Begin processing the source tape.
*/
switch (OpenTapeForRead(&src, srcfile)) {
case TIO_SUCCESS:
break;
case TIO_ERROR:
fprintf(stderr, "%s has errors and may not copy correctly\n", srcfile);
break;
case TIO_CORRUPT:
fprintf(stderr, "%s is not a SIMH .tap container file\n", srcfile);
exit(2);
case TIO_OPENFAIL:
fprintf(stderr, "%s open failed\n", srcfile);
exit(3);
}
/*
* Begin processing the destination tape.
*/
switch (OpenTapeForWrite(&dst, dstfile)) {
case TIO_SUCCESS:
break;
case TIO_IOERROR:
fprintf(stderr, "Error writing to destination tape - %s\n", dstfile);
exit(5);
case TIO_CREATEFAIL:
fprintf(stderr, "Failed to create destination tape - %s\n", dstfile);
exit(3);
}
/*
* Process the files on the tape.
*/
for (;;) {
if (filenumber <= MAXFILES) {
struct fileop *op = &fileops[filenumber];
/*
* Process possible repalcement.
*/
if (op->repfile.filename != NULL)
writeFile(op->repfile.filename, op->repfile.reclen);
/*
* Process any insertions before the current file.
*/
for (i = 0; i < APPFILES; i++)
if (op->insfiles[i].filename != NULL)
writeFile(op->insfiles[i].filename, op->insfiles[i].reclen);
/*
* Now either copy or skip over the next file on the source tape.
*/
if ((op->skipfile != 0) || (op->repfile.filename != 0)) {
if (SkipToNextTapeMark(src) == ST_EOM)
break;
} else {
if (copyFile() == ST_EOM)
break;
}
filenumber++;
} else {
/*
* If there are more than MAXFILES files on the source tape, just copy
* the remaining files.
*/
if (copyFile() == ST_EOM)
break;
}
}
/*
* Handle any appends
*/
for (i = 0; i < APPFILES; i++)
if (appendops.insfiles[i].filename != NULL)
writeFile(appendops.insfiles[i].filename, appendops.insfiles[i].reclen);
WriteTapeMark(dst, 0);
CloseTape(src);
CloseTape(dst);
return 0;
}

View File

@@ -0,0 +1,42 @@
cpytap manipulates a .tap magtape container file used by SIMH. It copies an
existing .tap file to a newly created .tap while modifying its file level
contents. While performing the copy, individual files may be skipped or
replaced and new files may be inserted at specified positions or appended
after the last source file has been copied. For files which are copied between
the source and destination tapes, the internal record structure of each file
is maintained. Replacement or inserted/appended files may only be written
with a specified maximum record size.
cpytap is invoked by:
cpytap src dst [-r len] [-I n,file] [-R n,file] [-S n] [-A file]
Where:
-r len Max record size to be used when writing new files to the
destination tape. (1 <= len <= 65536, default 10240).
There may be multiple "-r" switches on the command line.
When a "-r" switch is specified, it takes effect on all
following editing commands.
-I n,file Insert the specified file before file n of the source tape
-R n,file Replace file n of the source tape with the specified file
-S n Skip file n of the source tape
-A file Append the specified fie after all the files on the source
tape have been copied to the destination tape.
Files on the source tape are numbered 1 - n.
When multiple -I commands reference the same source tape file or there are
multiple -A commands, the files will be written to the destination tape in
the order specified on the command line. If a -R command and a -I command
reference the same source tape file, the -R file will be written first
followed by the -I file(s).
The editing control tables are pre-built into the executable. Edit commands
may be issued for files 1 - 100, subsequent files will just be copied from
source to destination. There may be up to 20 -I commands for each source tape
file and up to 20 -A commands.

40
extracters/cpytap/defs.h Normal file
View File

@@ -0,0 +1,40 @@
/* defs.h: Common definitions
Copyright (c) 2015, John Forecast
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of John Forecast shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from John Forecast.
*/
#ifndef __DEFS_H__
#define __DEFS_H__
#if defined(VMS)
#include <ints.h>
#else
typedef signed char int8;
typedef signed short int16;
typedef signed int int32;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
#endif
#endif

50
extracters/cpytap/tap.h Normal file
View File

@@ -0,0 +1,50 @@
/* tap.h: simh tape representation definitions
Copyright (c) 2015, John Forecast
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of John Forecast shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from John Forecast.
*/
/*
* Metadata markers
*/
#define ST_EOM 0xFFFFFFFF /* end of medium */
#define ST_GAP 0xFFFFFFFE /* erase gap */
#define ST_TM 0x00000000 /* tape mark */
/*
* Record length field layout
*/
#define ST_ERROR 0x80000000 /* record contains an error */
#define ST_MBZ 0x7F000000 /* must be zero */
#define ST_LENGTH 0x00FFFFFF /* record length */
/*
* Data in the .tap containe file is rounded up to an even number of bytes
*/
#define RECLEN(c) (((c) + 1) & ~1)
/*
* The maximum record length supported by this code is 64K.
*/
#define MAXRCLNT 65536

701
extracters/cpytap/tapeio.c Normal file
View File

@@ -0,0 +1,701 @@
/* tapeio.c: Tape I/O routines
Copyright (c) 2017, John Forecast
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of John Forecast shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from John Forecast.
*/
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include "tapeio.h"
char buffer[MAXRCLNT];
int rLength, occupied;
static int verifyFormat(FILE *);
/*++
* OpenTapeForRead
*
* Open an existing SIMH .tap format file for read access. If the file is
* successfully opened, scan the file to determine if it is a valid .tap
* format file and whether there are error records present.
*
* Inputs:
*
* handle - file handle returned here
* name - name of the file
*
* Outputs:
*
* None
*
* Returns:
*
* TIO_SUCCESS - file successfully opened, format is valid
* TIO_ERROR - file successfully opened, error records present
* TIO_CORRUPT - file successfully opened, format is invalid
* TIO_OPENFAIL - file open failed
*
* Note the file remains open if the return status is TIO_SUCCESS or
* TIO_ERROR
*
--*/
int OpenTapeForRead(
FILE **handle,
char *name
)
{
FILE *tfile;
if ((tfile = fopen(name, "r")) != NULL) {
int status;
status = verifyFormat(tfile);
rewind(tfile);
if ((status != TIO_SUCCESS) && (status != TIO_ERROR))
CloseTape(tfile);
else *handle = tfile;
return status;
}
return TIO_OPENFAIL;
}
/*++
* OpenTapeForWrite
*
* Create a new SIMH .tap format fiole for write access. Two tape marks are
* written to the file and the file handle is rewound to the beginning of the
* file. If the file already exists, and error (TIO_CREATEFAIL) is returned.
*
* Inputs:
*
* handle - file handle returned here
* name - name of the file
*
* Outputs:
*
* None
*
* Returns:
*
* TIO_SUCCESS - file successfully created
* TIO_IOERROR - I/O error writing the initial file contents
* TIO_CREATEFAIL - file create failed
*
--*/
int OpenTapeForWrite(
FILE **handle,
char *name
)
{
FILE *tfile;
/*
* Fail if the file exists
*/
if (access(name, F_OK) == 0)
return TIO_CREATEFAIL;
if ((tfile = fopen(name, "w+")) != NULL) {
uint32 tm = 0;
int status = TIO_SUCCESS;
/*
* Write 2 tape marks
*/
if (fwrite(&tm, sizeof(tm), 2, tfile) != 2)
status = TIO_IOERROR;
if (status == TIO_SUCCESS) {
rewind(tfile);
*handle = tfile;
return TIO_SUCCESS;
}
/*
* Failed to write the 2 tape marks. Try to delete the file before
* returning an error.
*/
CloseTape(tfile);
unlink(name);
return TIO_IOERROR;
}
return TIO_CREATEFAIL;
}
/*++
* OpenTapeForAppend
*
* Open an existing SIMH .tap format file for write access, leaving the
* file handle positioned just before the final tape mark. If the file is
* successfully opened, scan the file to determine if it is a valid .tap
* format file and whether there are error records present.
*
* Inputs:
*
* handle - file handle returned here
* name - name of the file
*
* Outputs:
*
* None
*
* Returns:
*
* TIO_SUCCESS - file successfully opened, format is valid
* TIO_ERROR - file successfully opened, error records present
* TIO_CORRUPT - file successfully opened, format is invalid
* TIO_OPENFAIL - file open failed
*
* Note the file remains open if the return status is TIO_SUCCESS or
* TIO_ERROR
*
--*/
int OpenTapeForAppend(
FILE **handle,
char *name
)
{
FILE *tfile;
if ((tfile =fopen(name, "r+")) != NULL) {
int status;
status = verifyFormat(tfile);
if ((status != TIO_SUCCESS) && (status != TIO_ERROR))
CloseTape(tfile);
else *handle = tfile;
return status;
}
return TIO_OPENFAIL;
}
/*++
* CloseTape
*
* If the tape is open, close it.
*
* Inputs:
*
* handle - file handle for the tape
*
* Outputs:
*
* None
*
* Returns:
*
* None
*
--*/
void CloseTape(
FILE *handle
)
{
if (handle != NULL)
fclose(handle);
}
/*++
* verifyFormat
*
* Verify the format of the SIMH .tap file. If the format is valid, leave
* the file handle positioned right before the last tape mark in the file.
*
* Inputs:
*
* handle - file handle for the tape
*
* Outputs:
*
* None
*
* Returns:
*
* TIO_SUCCESS - file format is correct
* TIO_ERROR - file format is correct, error records detected
* TIO_CORRUPT - file format is invalid
* TIO_IOERROR - I/O errror while processing file
*
--*/
static int verifyFormat(
FILE *handle
)
{
int errorCount = 0, tmSeen = 0;
uint8 meta[4];
uint32 header, bc;
off_t position;
struct stat stat;
/*
* Determine the size of the file.
*/
fstat(fileno(handle), &stat);
for (;;) {
position = ftello(handle);
/*
* If we are position at the end of file, there is a tape mark missing.
* Treat it as though there is one present.
*/
if (position == stat.st_size)
return TIO_SUCCESS;
if (fread(meta, sizeof(meta), 1, handle) != 1)
return TIO_CORRUPT;
bc = (((unsigned int)meta[3]) << 24) |
(((unsigned int)meta[2]) << 16) |
(((unsigned int)meta[1]) << 8) |
(unsigned int)meta[0];
switch (bc) {
case ST_TM:
if (++tmSeen <= 1)
break;
/* Treat second TM in a row as end of medium */
/* FALLTHROUGH */
case ST_EOM:
if (fseek(handle, -sizeof(meta), SEEK_CUR) != 0)
return TIO_IOERROR;
return errorCount ? TIO_ERROR : TIO_SUCCESS;
case ST_GAP:
break;
default:
/*
* Record descriptor
*/
tmSeen = 0;
header = bc;
if ((bc & ST_ERROR) != 0)
errorCount++;
if ((bc & ST_MBZ) != 0)
return TIO_CORRUPT;
bc = RECLEN(bc & ST_LENGTH);
/*
* Check if we are seeking outside of the file. If so, this is not
* a .tap container file.
*/
if ((position + bc + (2 * sizeof(meta))) > (unsigned long long)stat.st_size)
return TIO_CORRUPT;
if (fseek(handle, bc, SEEK_CUR) != 0)
return TIO_CORRUPT;
if (fread(meta, sizeof(meta), 1, handle) != 1)
return TIO_CORRUPT;
bc = (((unsigned int)meta[3]) << 24) |
(((unsigned int)meta[2]) << 16) |
(((unsigned int)meta[1]) << 8) |
(unsigned int)meta[0];
if (header != bc)
return TIO_CORRUPT;
}
}
}
/*++
* ReadTapeRecord
*
* Read the next record from the tape into the specified buffer. If the
* buffer is smaller than the record, the entire record will be consumed.
*
* Inputs:
*
* handle - file handle for the tape
* buf - pointer to the buffer to receive the data
* len - length of the buffer
*
* Outputs:
*
* None
*
* Returns:
*
* ST_EOM - end of medium detected
* ST_TM - tape mark detected
* Other - record length (including error flag)
* if the buffer is smaller than the record, the
* length returned will be that of the buffer
*
--*/
uint32 ReadTapeRecord(
FILE *handle,
void *buf,
int len
)
{
long pos = ftell(handle);
uint8 meta[4];
uint32 bc, erflag, length;
/*
* Note: any I/O errors are treated as "end of medium" detection.
*/
if (fread(meta, sizeof(meta), 1, handle) != 1)
return ST_EOM;
bc = (((unsigned int)meta[3]) << 24) |
(((unsigned int)meta[2]) << 16) |
(((unsigned int)meta[1]) << 8) |
(unsigned int)meta[0];
switch (bc) {
case ST_EOM:
case ST_TM:
return bc;
default:
erflag = bc & ST_ERROR;
bc &= ST_LENGTH;
length = (uint32)len;
if (bc < length)
length = bc;
if (fread(buf, sizeof(uint8), length, handle) != length)
return ST_EOM;
/*
* Now position the file after this record.
*/
pos += RECLEN(bc) + (2 * sizeof(meta));
if (fseek(handle, pos, SEEK_SET) != 0)
return ST_EOM;
return erflag | length;
}
return ST_EOM;
}
/*++
* ReadTapeRecordLength
*
* Get the length of the next record on the tape without actually reading
* the data.
*
* Inputs:
*
* handle - file handle for the tape
*
* Outputs:
*
* None
*
* Returns:
*
* ST_EOM - end of medium detected
* ST_TM - tape mark detected
* Other - record length (including error flag)
* if the buffer is smaller than the record, the
* length returned will be that of the buffer
*
--*/
uint32 ReadTapeRecordLength(
FILE *handle
)
{
long pos = ftell(handle);
uint8 meta[4];
uint32 bc, erflag;
/*
* Note: any I/O errors are treated as "end of medium" detection.
*/
if (fread(meta, sizeof(meta), 1, handle) != 1)
return ST_EOM;
bc = (((unsigned int)meta[3]) << 24) |
(((unsigned int)meta[2]) << 16) |
(((unsigned int)meta[1]) << 8) |
(unsigned int)meta[0];
switch (bc) {
case ST_EOM:
case ST_TM:
return bc;
default:
erflag = bc & ST_ERROR;
bc &= ST_LENGTH;
/*
* Now position the file after this record.
*/
pos += RECLEN(bc) + (2 * sizeof(meta));
if (fseek(handle, pos, SEEK_SET) != 0)
return ST_EOM;
return erflag | bc;
}
return ST_EOM;
}
/*++
* WriteTapeRecord
*
* Write a record to the tape at it's current position.
*
* Inputs:
*
* handle - file handle for the tape
* buf - pointer to the buffer to be written
* len - length of the buffer
*
* Outputs:
*
* None
*
* Returns:
*
* 0 if record was written successfully, -1 if write failed
*
--*/
int WriteTapeRecord(
FILE *handle,
void *buf,
int len
)
{
uint8 meta[4];
int datalen;
meta[0] = len & 0xFF;
meta[1] = (len >> 8) & 0xFF;
meta[2] = (len >> 16) & 0xFF;
meta[3] = (len >> 24) & 0xFF;
datalen = ((len + 1) & ST_LENGTH) & ~1;
if (fwrite(meta, sizeof(meta), 1, handle) != 1)
return -1;
if (fwrite(buf, datalen, 1, handle) != 1)
return -1;
if (fwrite(meta, sizeof(meta), 1, handle) != 1)
return -1;
return 0;
}
/*++
* SkipToNextTapeMark
*
* Skip forward to the next tape mark and position the file just past the
* tape mark.
*
* Inputs:
*
* handle - file handle for the tape
*
* Outputs:
*
* None
*
* Returns:
*
* ST_EOM - end of medium detected
* ST_TM - tape mark detected
*
--*/
unsigned int SkipToNextTapeMark(
FILE *handle
)
{
long pos = ftell(handle);
uint8 meta[4];
uint32 bc;
for (;;) {
/*
* Note: any I/O errors are treated as "end of medium" detection.
*/
if (fread(meta, sizeof(meta), 1, handle) != 1)
return ST_EOM;
bc = (((unsigned int)meta[3]) << 24) |
(((unsigned int)meta[2]) << 16) |
(((unsigned int)meta[1]) << 8) |
(unsigned int)meta[0];
switch (bc) {
case ST_EOM:
case ST_TM:
return bc;
default:
bc &= ST_LENGTH;
/*
* Now position the file after this record.
*/
pos += RECLEN(bc) + (2 * sizeof(meta));
if (fseek(handle, pos, SEEK_SET) != 0)
return ST_EOM;
break;
}
}
}
/*++
* WriteTapeMark
*
* Write a tape mark to the tape at it's current position and, optionally,
* backup to before the tape mark.
*
* Inputs:
*
* handle - file handle for the tape
* backup - if 1, reposition the tape to before the tape mark
*
* Outputs:
*
* None
*
* Returns:
*
* 0 if tape mark was written successfully, -1 if write failed
*
--*/
int WriteTapeMark(
FILE *handle,
int backup
)
{
uint32 tm = 0;
if (fwrite(&tm, sizeof(tm), 1, handle) != 1)
return -1;
if (backup)
if (fseek(handle, -sizeof(tm), SEEK_CUR) != 0)
return -1;
return 0;
}
/*++
* initTapeBuffering
*
* Initialize variables for writes to tape for ASCII mode transfers
* (translates LF -> CRLF).
*
* Inputs:
*
* reclen - size of the tape record buffer to use
*
* Outputs:
*
* None
*
* Returns:
*
* None
*
--*/
void initTapeBuffering(
int reclen
)
{
rLength = reclen;
occupied = 0;
}
/*++
* flushTapeBuffering
*
* Flush any pending data out to the current tape.
*
* Inputs:
*
* handle - file handle for the tape
*
* Outputs:
*
* None
*
* Returns:
*
* 0 if data was successfully flushed, -1 if write failed
*
--*/
int flushTapeBuffering(
FILE *handle
)
{
uint32 count = occupied;
occupied = 0;
if (count != 0)
return WriteTapeRecord(handle, buffer, count);
return 0;
}
/*++
* writeTapeBuffering
*
* Write a character to the current tape, buffering the data into records.
*
* Inputs:
*
* handle - file handle for the tape
* ch - the character to be output
*
* Outputs:
*
* None
*
* Returns:
*
* 0 if character was successfully buffered or written to tape, -1 if
* write failed
*
--*/
int writeTapeBuffering(
FILE *handle,
char ch
)
{
buffer[occupied++] = ch;
if (occupied == rLength) {
occupied = 0;
return WriteTapeRecord(handle, buffer, rLength);
}
return 0;
}

View File

@@ -0,0 +1,63 @@
/* tapeio.h: Tape I/O definitions
Copyright (c) 2015, John Forecast
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
JOHN FORECAST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of John Forecast shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from John Forecast.
*/
#include "tap.h"
#include "defs.h"
/*
* Tape open status return codes
*/
#define TIO_SUCCESS 0 /* operation successful */
#define TIO_ERROR -1 /* error record seen */
#define TIO_CORRUPT -2 /* tape format is corrupt */
#define TIO_OPENFAIL -3 /* open operation failed */
#define TIO_CREATEFAIL -4 /* create operation failed */
#define TIO_IOERROR -5 /* I/O error */
/*
* Tape open/close routines.
*/
extern int OpenTapeForRead(FILE **, char *);
extern int OpenTapeForWrite(FILE **, char *);
extern int OpenTapeForAppend(FILE **, char *);
extern void CloseTape(FILE *);
/*
* Tape I/O routines.
*/
uint32 ReadTapeRecord(FILE *, void *, int);
uint32 ReadTapeRecordLength(FILE *);
int WriteTapeRecord(FILE *, void *, int);
unsigned int SkipToNextTapeMark(FILE *);
int WriteTapeMark(FILE *, int);
/*
* Buffered I/O routines
*/
void initTapeBuffering(int);
int flushTapeBuffering(FILE *);
int writeTapeBuffering(FILE *, char);