]> git.sur5r.net Git - c128-kasse/blob - src/cat.c
README: remove emulator configuration, we use -config vicerc
[c128-kasse] / src / cat.c
1 #include <stdio.h>
2 #include <conio.h>
3 #include <string.h>
4 #include <dirent.h>
5 #include <stdlib.h>
6
7 #include "general.h"
8
9 #define x2(x) (buffer[x] <= 0xF ? "0" : ""), buffer[x]
10
11 int main(void) {
12   char *filename = NULL;
13   FILE *file;
14   unsigned int c;
15   unsigned char i;
16   char buffer[8];
17   char readable[9];
18
19   memset(readable, '\0', 9);
20
21   while (1) {
22     clrscr();
23     while (filename == NULL || *filename == '\0') {
24       printf("Please enter filename (q to exit):\r\n");
25       filename = get_input();
26     }
27     if (*filename == 'q')
28       return 0;
29     c = 0;
30     if ((file = fopen(filename, "r")) == NULL) {
31       printf("Could not open file\r\n");
32       continue;
33     }
34     clrscr();
35     while (!feof(file)) {
36       if (fgets(buffer, 8, file) != buffer) {
37         printf("Could not read from file, bailing out\r\n");
38         return 1;
39       }
40       for (i = 0; i < 8; i++)
41         if ((buffer[i] >= 0x41 && buffer[i] <= 0x5A) ||
42             (buffer[i] >= 0xC1 && buffer[i] <= 0xDA) ||
43             (buffer[i] >= 0x30 && buffer[i] <= 0x39))
44           readable[i] = buffer[i];
45         else
46           readable[i] = 0x2E;
47       printf("%s%x |%s%x %s%x %s%x %s%x %s%x %s%x %s%x %s%x| %s \n",
48              (c <= 0xF ? "0" : ""), c, x2(0), x2(1), x2(2), x2(3), x2(4), x2(5),
49              x2(6), x2(7), readable);
50       c++;
51       if ((c % 20) == 0) {
52         get_input();
53         clrscr();
54       }
55     }
56     fclose(file);
57     filename = NULL;
58     printf("File finished, press RETURN...\n");
59     get_input();
60   }
61 }