]> git.sur5r.net Git - cc65/blob - testcode/lib/ft.c
Fixed _textcolor definition.
[cc65] / testcode / lib / ft.c
1 /*
2 ** simple file I/O test
3 **
4 ** 12-Jun-2000, Christian Groessler
5 **
6 ** please compile with
7 **   cl65 -tsystem ft.c getsp.s -o ft.com
8 **
9 ** The program asks for a filename (if it hasn't
10 ** got one from argv). I then opens the file,
11 ** reads the the first 16 bytes and displays them
12 ** (as hex values).
13 ** The values of sp (cc65 runtime stack pointer)
14 ** are displayed at some places. The displayed
15 ** value should always be the same.
16 */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <fcntl.h>
21 #include <errno.h>
22 #include <conio.h>
23 #include <unistd.h>
24
25 extern int getsp(void);  /* is provided in getsp.s */
26
27 /* Atari's fd indirection table */
28 #ifdef __ATARI__
29 extern char __fd_index[];
30 struct fd_t {
31   char usage;
32   char iocb;
33   char dev;
34   char flag;
35 };
36 extern struct fd_t __fd_table[];
37 #endif
38
39 int main(int argc,char **argv)
40 {
41     char *filename,*x;
42     char buf[20];
43     int i,l,lr;
44     int fd;
45     int csp;
46
47     if (argc >= 2) {
48         filename = *(argv+1);
49     }
50     else {
51         printf("\nfilename: ");
52         x = fgets(buf,19,stdin);
53         printf("\n");
54         if (!x) {
55             printf("nothing read\n");
56             return(0);
57         }
58 #if 0
59         l = strlen(x);
60         printf("read: ");
61         for (i=0; i<l; i++) printf("%02X ",*(x+i)); printf("\n");
62 #endif
63         filename = x;
64     }
65     printf("using filename \"%s\"\n",filename);
66     csp = getsp();
67     printf("now opening file... sp = %d\n",csp);
68     fd = open(filename,O_RDONLY);
69     csp = getsp();
70     if (fd == -1) {
71         char x1 = _oserror;
72         printf("open failed: os: %d,\n\terrno: %d, sp = %d\n",x1,errno,csp);
73         cgetc();
74         return(0);
75     }
76     printf("open success -- handle = $%x, sp = %d\n",fd,csp);
77 #ifdef __ATARI__
78     printf("fd_index:\n ");
79     for (i=0; i<12; i++) printf("%02X ",__fd_index[i]);
80     printf("\nfd_table:\n");
81     for (i=0; i<8; i++) {
82         printf(" usa: %d, iocb: %02X, dev: %02X\n",
83                __fd_table[i].usage,
84                __fd_table[i].iocb,
85                __fd_table[i].dev);
86     }
87 #endif
88     lr = read(fd,buf,16);  /* read first 16 bytes */
89     csp = getsp();
90     if (lr == -1) {
91         printf("read failed: %d (sp = %d)\n",errno,csp);
92         cgetc();
93         return(0);
94     }
95     l = close(fd);
96     if (l == -1) {
97         printf("close failed: %d\n",errno);
98         cgetc();
99         return(0);
100     }
101     csp = getsp();
102     printf("\n\nThe data read: (%d bytes, sp = %d)\n",lr,csp);
103     for (i=0; i<lr; i++) {
104         printf("%02X ",buf[i]);
105         if (!((i+1) & 7)) printf("\n");
106     }
107     printf("\n\npress return to exit...");
108     getchar();
109     return(0);
110 }