]> git.sur5r.net Git - c128-kasse/blob - src/cat.c
Add hex viewer "cat"
[c128-kasse] / src / cat.c
1 #include <stdio.h>
2 #include <conio.h>
3 #include <string.h>
4 #include <dirent.h>
5 #include <cbm.h>
6
7 #include "general.h"
8
9 #define x2(x) (buffer[x] < 10 ? "0" : ""), buffer[x]
10
11 int main() {
12         char *filename = NULL;
13         FILE *file;
14         unsigned int c;
15         unsigned char i;
16         char buffer[8];
17         char readable[9];
18         struct cbm_dirent *dirent;
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, d for dirlist):\r\n");
26                         filename = get_input();
27                 }
28                 if (*filename == 'q')
29                         return 0;
30                 else if (*filename == 'd') {
31                         c = 0;
32                         if (cbm_opendir((BYTE)8, (BYTE)8) != 0) {
33                                 cprintf("could not open directory\r\n");
34                                 return 1;
35                         }
36                         while (cbm_readdir((BYTE)8, dirent) == 0) {
37                                 printf("file %d: %s\n", c, dirent->name);
38                                 get_input();
39                         }
40                         cbm_closedir((BYTE)8);
41
42                         printf("Finished listing directory, press RETURN...\n");
43                         get_input();
44
45                         filename = NULL;
46                         continue;
47                 }
48                 c = 0;
49                 if ((file = fopen(filename, "r")) == NULL) {
50                         printf("Could not open file\r\n");
51                         continue;
52                 }
53                 clrscr();
54                 while (!feof(file)) {
55                         if (fgets(buffer, 8, file) != buffer) {
56                                 printf("Could not read from file, bailing out\r\n");
57                                 return 1;
58                         }
59                         for (i = 0; i < 8; i++)
60                                 if (    (buffer[i] >= 0x41 && buffer[i] <= 0x5A) ||
61                                         (buffer[i] >= 0xC1 && buffer[i] <= 0xDA) ||
62                                         (buffer[i] >= 0x30 && buffer[i] <= 0x39))
63                                         readable[i] = buffer[i];
64                                 else readable[i] = 0x2E;
65                         printf("%s%x |%s%x %s%x %s%x %s%x %s%x %s%x %s%x %s%x| %s \n",
66                                 (c <= 0xF ? "0" : ""), c,
67                                 x2(0), x2(1), x2(2), x2(3),
68                                 x2(4), x2(5), x2(6), x2(7),
69                                 readable);
70                         c++;
71                         if ((c % 20) == 0) {
72                                 get_input();
73                                 clrscr();
74                         }
75                 }
76                 fclose(file);
77                 filename = NULL;
78                 printf("File finished, press RETURN...\n");
79                 get_input();
80         }
81 }