]> git.sur5r.net Git - cc65/blob - testcode/lib/ft.c
info about c1541 in docs, lowered highest available address to $6000 due to
[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
24 extern int getsp(void);  /* is provided in getsp.s */
25
26 #ifdef __ATARI__  /* Atari's fd indirection table */
27 extern char __fd_index[];
28 struct fd_t {
29   char usage;
30   char iocb;
31   char dev;
32   char flag;
33 };
34 extern struct fd_t __fd_table[];
35 #endif /* #ifdef __ATARI__ */
36
37 int main(int argc,char **argv)
38 {
39     char *filename,*x;
40     char buf[20];
41     int i,l,lr;
42     int fd;
43     int csp;
44
45     if (argc >= 2) {
46         filename = *(argv+1);
47     }
48     else {
49         printf("\nfilename: ");
50         x = fgets(buf,19,stdin);
51         printf("\n");
52         if (!x) {
53             printf("nothing read\n");
54             return(0);
55         }
56 #if 0
57         l = strlen(x);
58         printf("read: ");
59         for (i=0; i<l; i++) printf("%02X ",*(x+i)); printf("\n");
60 #endif
61         filename = x;
62     }
63     printf("using filename \"%s\"\n",filename);
64     csp = getsp();
65     printf("now opening file... sp = %d\n",csp);
66     fd = open(filename,O_RDONLY);
67     csp = getsp();
68     if (fd == -1) {
69         char x1 = _oserror;
70         printf("open failed: os: %d,\n\terrno: %d, sp = %d\n",x1,errno,csp);
71         cgetc();
72         return(0);
73     }
74     printf("open success -- handle = $%x, sp = %d\n",fd,csp);
75 #ifdef __ATARI__
76     printf("fd_index:\n ");
77     for (i=0; i<12; i++) printf("%02X ",__fd_index[i]);
78     printf("\nfd_table:\n");
79     for (i=0; i<8; i++) {
80         printf(" usa: %d, iocb: %02X, dev: %02X\n",
81                __fd_table[i].usage,
82                __fd_table[i].iocb,
83                __fd_table[i].dev);
84     }
85 #endif /* #ifdef __ATARI__ */
86     lr = read(fd,buf,16);  /* read first 16 bytes */
87     csp = getsp();
88     if (lr == -1) {
89         printf("read failed: %d (sp = %d)\n",errno,csp);
90         cgetc();
91         return(0);
92     }
93     l = close(fd);
94     if (l == -1) {
95         printf("close failed: %d\n",errno);
96         cgetc();
97         return(0);
98     }
99     csp = getsp();
100     printf("\n\nThe data read: (%d bytes, sp = %d)\n",lr,csp);
101     for (i=0; i<lr; i++) {
102         printf("%02X ",buf[i]);
103         if (!((i+1) & 7)) printf("\n");
104     }
105     printf("\n\npress return to exit...");
106     getchar();
107     return(0);
108 }