]> git.sur5r.net Git - cc65/blob - libsrc/cbm/cbm_read.c
Added o65 symbol export capability
[cc65] / libsrc / cbm / cbm_read.c
1 /*
2  * Marc 'BlackJack' Rintsch, 19.03.2001
3  *
4  * int cbm_read(unsigned char lfn, void* buffer, unsigned int size);
5  */
6
7 #include <cbm.h>
8 #include <errno.h>
9
10 int cbm_read(unsigned char lfn, void* buffer, unsigned int size)
11 {
12     static unsigned int bytesread;
13     static unsigned char tmp;
14
15     /* if we can't change to the inputchannel #lfn then return an error */
16     if (_oserror = cbm_k_chkin(lfn)) return -1;
17     
18     bytesread = 0;
19
20     while (bytesread<size && !cbm_k_readst()) {
21         tmp = cbm_k_basin();
22         
23         /* the kernal routine BASIN sets ST to EOF if the end of file
24          * is reached the first time, then we have store tmp.
25          * every subsequent call returns EOF and READ ERROR in ST, then
26          * we have to exit the loop here immidiatly. */
27         if (cbm_k_readst() & 0xBF) break;
28         
29         ((unsigned char*)buffer)[bytesread++] = tmp;
30     }
31     
32     cbm_k_clrch();
33     return bytesread;
34 }