/* This program is a modification of mtdump from the SIMH tools package. It was created to convert the IBM 1130 tapes into individual files. It extracts files from a SIMH format tape file into raw copies of the bytes and to ASCII with CR/LF line ending from EBCDIC. Modified by David Gesswein djg@pdp8online.com */ /* This program extracts files from a simulated magtape Copyright (c) 1993-2003, Robert M. Supnik 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 ROBERT M SUPNIK 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 Robert M Supnik shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from Robert M Supnik. */ #include #include #include #include #include #include #define F_STD 0 #define F_E11 1 #define F_TPC 2 #define F_TPF 3 static unsigned char e2a[256] = { 0, 1, 2, 3,156, 9,134,127,151,141,142, 11, 12, 13, 14, 15, 16, 17, 18, 19,157,133, 8,135, 24, 25,146,143, 28, 29, 30, 31, 128,129,130,131,132, 10, 23, 27,136,137,138,139,140, 5, 6, 7, 144,145, 22,147,148,149,150, 4,152,153,154,155, 20, 21,158, 26, 32,160,161,162,163,164,165,166,167,168, 91, 46, 60, 40, 43, 33, 38,169,170,171,172,173,174,175,176,177, 93, 36, 42, 41, 59, 94, 45, 47,178,179,180,181,182,183,184,185,124, 44, 37, 95, 62, 63, 186,187,188,189,190,191,192,193,194, 96, 58, 35, 64, 39, 61, 34, 195, 97, 98, 99,100,101,102,103,104,105,196,197,198,199,200,201, 202,106,107,108,109,110,111,112,113,114,203,204,205,206,207,208, 209,126,115,116,117,118,119,120,121,122,210,211,212,213,214,215, 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231, 123, 65, 66, 67, 68, 69, 70, 71, 72, 73,232,233,234,235,236,237, 125, 74, 75, 76, 77, 78, 79, 80, 81, 82,238,239,240,241,242,243, 92,159, 83, 84, 85, 86, 87, 88, 89, 90,244,245,246,247,248,249, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,250,251,252,253,254,255 }; #define MAXRLNT 65536 int main (int argc, char *argv[]) { int obj, i, k, fc, rc, tpos; unsigned int bc, fmt, rlntsiz; char *s; unsigned char bca[4]; unsigned char buf[MAXRLNT]; unsigned char buf_ascii[MAXRLNT]; int preveof; FILE *ifile; FILE *out = NULL; FILE *out_ascii = NULL; char fn[80], full_fn[80]; int n, retc; int min_lrecl, max_lrecl; int ignore_logical = 0; if ((argc < 2) || (argv[0] == NULL)) { printf ("Usage is: ibm_extract {-secf} file [file...]\n"); exit (0); } s = argv[1]; fmt = F_STD; rlntsiz = 4; if ((s != NULL) && (*s++ == '-')) { ++argv; --argc; switch (*s) { case 'S': case 's': fmt = F_STD; rlntsiz = 4; break; case 'E': case 'e': fmt = F_E11; rlntsiz = 4; break; case 'C': case 'c': fmt = F_TPC; rlntsiz = 2; break; /* case 'F': case 'f': */ /* fmt = F_TPF; break; */ case 'L': case 'l': ignore_logical = 1; break; default: fprintf (stderr, "Bad option %c\n", *s); return 0; } } for (i = 1; i < argc; i++) { ifile = fopen (argv[i], "rb"); if (ifile == NULL) { printf ("Error opening file: %s\n", argv[i]); exit (0); } printf ("Processing input file %s\n", argv[i]); tpos = 0; rc = 1; fc = 1; preveof = 0; printf ("Processing tape file %d\n", fc); for (obj = 1;;) { fseek (ifile, tpos, SEEK_SET); k = fread (bca, 1, rlntsiz, ifile); switch (fmt) { case F_STD: case F_E11: bc = (((unsigned int) bca[3]) << 24) | (((unsigned int) bca[2]) << 16) | (((unsigned int) bca[1]) << 8) | ((unsigned int) bca[0]); break; case F_TPC: bc = (((unsigned int) bca[1]) << 8) | ((unsigned int) bca[0]); break; } if ((k == 0) || (bc == 0xFFFFFFFF)) { if (rc != 1) printf ("end of file %d (%s) (no filemark) records %d record length %d to %d\n", fc, fn, rc-1, min_lrecl, max_lrecl); printf ("End of physical tape\n"); break; } if (bc & 0x80000000) { printf ("Error marker at record %d\n", rc); bc = bc & ~0x80000000; } if (bc == 0) { if (preveof) { printf ("Obj %d, position %d, end of logical tape\n", obj, tpos); if (!ignore_logical) break; } preveof = 1; if (rc == 1) printf ("Obj %d, position %d, end of tape file %d (%s) no records\n", obj, tpos, fc, fn); else printf ("Obj %d, position %d, end of tape file %d (%s) records %d record length %d to %d\n", obj, tpos, fc, fn, rc-1, min_lrecl, max_lrecl); fc++; obj++; rc = 1; tpos = tpos + rlntsiz; } else if (bc > MAXRLNT) { printf ("Invalid record length %d, terminating dump\n", bc); break; } else { if (preveof) printf ("Processing tape file %d\n", fc); preveof = 0; if (bc >= sizeof(buf)) { printf("Record longer than buffer %d -> %d\n", bc, sizeof(buf)); } else { k = fread (buf, 1, bc, ifile); if (k != bc) { printf("Record read errror %d -> %d\n", bc, k); } else { if (rc == 1) { if (out != NULL) { fclose(out); out = NULL; } if (out_ascii != NULL) { fclose(out_ascii); out_ascii = NULL; } { n = 0; if (e2a[buf[8]] == ' ' && e2a[buf[79]] == '*') { for (n = 0; n < 9; n++) { unsigned char c = e2a[buf[n]]; if (c == ' ') { break; // EARLY EXIT } else { fn[n] = c; } } fn[n] = 0; } if (n == 0) { sprintf(fn, "file%04d",fc); } strcpy(full_fn, "ascii/"); strcat(full_fn, fn); out_ascii = fopen(full_fn, "w"); if (out_ascii == NULL) { printf("Unable to open file %s: %s\n", full_fn, strerror(errno)); exit(1); } } strcpy(full_fn, "raw/"); strcat(full_fn, fn); out = fopen(full_fn, "w"); if (out == NULL) { printf("Unable to open file %s: %s\n", full_fn, strerror(errno)); exit(1); } min_lrecl = MAXRLNT; max_lrecl = 0; } } if (out_ascii != NULL) { int step; for (n = 0; n < bc; n++) { buf_ascii[n] = e2a[buf[n]]; } if (bc % 80 == 0) { step = 80; } else { step = bc; } for (n = 0; n < bc; n += step) { retc = fwrite(&buf_ascii[n], 1, step, out_ascii); if (retc != step) { printf("Short write %d %d\n", retc, step); exit(1); } fprintf(out_ascii, "\r\n"); } } retc = fwrite(buf, 1, bc, out); if (retc != bc) { printf("Short write %d %d\n", retc, bc); exit(1); } } if (bc < min_lrecl) min_lrecl = bc; if (bc > max_lrecl) max_lrecl = bc; #if 0 printf ("Obj %d, position %d, record %d, length = %d (0x%X)\n", obj, tpos, rc, bc, bc); #endif rc++; obj++; switch (fmt) { case F_STD: tpos = tpos + 8 + ((bc + 1) & ~1); break; case F_E11: tpos = tpos + 8 + bc; break; case F_TPC: tpos = tpos + 2 + ((bc + 1) & ~1); break; } } } fclose (ifile); } return 0; }