| 1 | /* This program converts between the old 128 words/block DECtape images |
| 2 | and the new 129 words/block DECtape image format. |
| 3 | to run execute blkdectp infile outfile |
| 4 | It will convert the infile to the opposite format and write to outfile. |
| 5 | */ |
| 6 | #ifdef PC |
| 7 | #include <io.h> |
| 8 | #else |
| 9 | #include <unistd.h> |
| 10 | #define O_BINARY 0 |
| 11 | #endif |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <fcntl.h> |
| 18 | |
| 19 | main(int argc, char *argv[]) |
| 20 | { |
| 21 | int fdin,fdout; |
| 22 | struct stat statbuf; |
| 23 | unsigned short buf[129]; |
| 24 | int rc; |
| 25 | int insize,outsize; |
| 26 | |
| 27 | if (argc != 3) { |
| 28 | printf("Usage: %s infile outfile\n",argv[0]); |
| 29 | exit(1); |
| 30 | } |
| 31 | fdin = open(argv[1],O_RDONLY | O_BINARY); |
| 32 | if (fdin < 0) { |
| 33 | perror("Unable to open input file"); |
| 34 | exit(1); |
| 35 | } |
| 36 | fdout = open(argv[2],O_WRONLY | O_CREAT | O_BINARY, 0666); |
| 37 | if (fdout < 0) { |
| 38 | perror("Unable to open output file"); |
| 39 | exit(1); |
| 40 | } |
| 41 | if (fstat(fdin, &statbuf) < 0) { |
| 42 | perror("Unable to get size of input file"); |
| 43 | exit(1); |
| 44 | } |
| 45 | if (statbuf.st_size % (129*2) == 0) { |
| 46 | insize = 129; |
| 47 | outsize = 128; |
| 48 | } else if (statbuf.st_size % (128*2) == 0) { |
| 49 | insize = 128; |
| 50 | outsize = 129; |
| 51 | } else { |
| 52 | printf("Input file is not a multiple of 128 or 129 words, can't convert\n"); |
| 53 | exit(1); |
| 54 | } |
| 55 | |
| 56 | memset(buf, 0, sizeof(buf)); |
| 57 | do { |
| 58 | rc = read(fdin, buf, insize*2); |
| 59 | if (rc == insize*2) { |
| 60 | if (write(fdout, buf, outsize*2) != outsize*2) { |
| 61 | perror("Error writing to output file"); |
| 62 | exit(1); |
| 63 | } |
| 64 | } else if (rc < 0) { |
| 65 | perror("Error reading input file"); |
| 66 | exit(1); |
| 67 | } else if (rc != 0) { |
| 68 | printf("Short read on input file %d\n",rc); |
| 69 | exit(1); |
| 70 | } |
| 71 | } while (rc > 0); |
| 72 | } |