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