forked from unclebob/PDP8EmulatorIpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopt.c
67 lines (53 loc) · 1.22 KB
/
topt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Simple utility to translate a binary file, character by character
into lines composed of three octal digits.
So a file that contains "ABC"
will be translated to: 101\n102\n103\n
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
void putCharInOctal(int c, FILE* outFile) {
fputc('0'+((c>>6)&7), outFile);
fputc('0'+((c>>3)&7), outFile);
fputc('0'+(c&7), outFile);
fputc('\n', outFile);
}
void leader(int n, FILE* outFile) {
for (int i=0; i<n; i++)
putCharInOctal(0200, outFile);
}
int main(int ac, char** av) {
int textFlag = 0;
if (ac != 2 && ac != 3) {
printf("usage topt [-t] file\n");
exit(1);
}
av++;
if (strcmp(*av, "-t") == 0) {
printf("text mode.\n");
textFlag = 1;
av++;
}
FILE* inFile = fopen(*av, "r");
char outFileName[100];
bzero(outFileName, 100);
strcpy(outFileName, *av);
strcat(outFileName, ".txt");
printf("Creating %s\n", outFileName);
FILE* outFile = fopen(outFileName, "w");
leader(10, outFile);
int c;
while ((c = fgetc(inFile)) != EOF) {
if (textFlag) {
c+=0200;
if (c == 0212)
putCharInOctal(0215, outFile);
}
putCharInOctal(c, outFile);
}
leader(10, outFile);
fclose(inFile);
fclose(outFile);
}