2000-06-11 23:36:16 +00:00
|
|
|
/*
|
2014-06-30 09:10:35 +00:00
|
|
|
** simple file I/O test
|
|
|
|
**
|
|
|
|
** 12-Jun-2000, Christian Groessler
|
|
|
|
**
|
|
|
|
** please compile with
|
|
|
|
** cl65 -tsystem ft.c getsp.s -o ft.com
|
|
|
|
**
|
|
|
|
** The program asks for a filename (if it hasn't
|
|
|
|
** got one from argv). I then opens the file,
|
2022-02-21 20:44:31 +00:00
|
|
|
** reads the first 16 bytes and displays them
|
2014-06-30 09:10:35 +00:00
|
|
|
** (as hex values).
|
|
|
|
** The values of sp (cc65 runtime stack pointer)
|
|
|
|
** are displayed at some places. The displayed
|
|
|
|
** value should always be the same.
|
|
|
|
*/
|
2000-06-11 23:36:16 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <conio.h>
|
2009-11-25 17:59:55 +00:00
|
|
|
#include <unistd.h>
|
2000-06-11 23:36:16 +00:00
|
|
|
|
|
|
|
extern int getsp(void); /* is provided in getsp.s */
|
|
|
|
|
2013-05-28 19:56:37 +00:00
|
|
|
/* Atari's fd indirection table */
|
2013-06-17 19:34:08 +00:00
|
|
|
#ifdef __ATARI__
|
2000-06-25 01:40:29 +00:00
|
|
|
extern char __fd_index[];
|
2000-06-11 23:36:16 +00:00
|
|
|
struct fd_t {
|
|
|
|
char usage;
|
|
|
|
char iocb;
|
|
|
|
char dev;
|
|
|
|
char flag;
|
|
|
|
};
|
2000-06-25 01:40:29 +00:00
|
|
|
extern struct fd_t __fd_table[];
|
2013-05-28 19:56:37 +00:00
|
|
|
#endif
|
2000-06-11 23:36:16 +00:00
|
|
|
|
|
|
|
int main(int argc,char **argv)
|
|
|
|
{
|
|
|
|
char *filename,*x;
|
|
|
|
char buf[20];
|
|
|
|
int i,l,lr;
|
|
|
|
int fd;
|
|
|
|
int csp;
|
|
|
|
|
|
|
|
if (argc >= 2) {
|
|
|
|
filename = *(argv+1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("\nfilename: ");
|
|
|
|
x = fgets(buf,19,stdin);
|
|
|
|
printf("\n");
|
|
|
|
if (!x) {
|
|
|
|
printf("nothing read\n");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
l = strlen(x);
|
|
|
|
printf("read: ");
|
|
|
|
for (i=0; i<l; i++) printf("%02X ",*(x+i)); printf("\n");
|
|
|
|
#endif
|
|
|
|
filename = x;
|
|
|
|
}
|
|
|
|
printf("using filename \"%s\"\n",filename);
|
|
|
|
csp = getsp();
|
|
|
|
printf("now opening file... sp = %d\n",csp);
|
|
|
|
fd = open(filename,O_RDONLY);
|
|
|
|
csp = getsp();
|
|
|
|
if (fd == -1) {
|
|
|
|
char x1 = _oserror;
|
|
|
|
printf("open failed: os: %d,\n\terrno: %d, sp = %d\n",x1,errno,csp);
|
|
|
|
cgetc();
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
printf("open success -- handle = $%x, sp = %d\n",fd,csp);
|
2013-06-17 19:34:08 +00:00
|
|
|
#ifdef __ATARI__
|
2000-06-11 23:36:16 +00:00
|
|
|
printf("fd_index:\n ");
|
2000-06-25 01:40:29 +00:00
|
|
|
for (i=0; i<12; i++) printf("%02X ",__fd_index[i]);
|
2000-06-11 23:36:16 +00:00
|
|
|
printf("\nfd_table:\n");
|
|
|
|
for (i=0; i<8; i++) {
|
|
|
|
printf(" usa: %d, iocb: %02X, dev: %02X\n",
|
2000-06-25 01:40:29 +00:00
|
|
|
__fd_table[i].usage,
|
|
|
|
__fd_table[i].iocb,
|
|
|
|
__fd_table[i].dev);
|
2000-06-11 23:36:16 +00:00
|
|
|
}
|
2013-05-28 19:56:37 +00:00
|
|
|
#endif
|
2000-06-11 23:36:16 +00:00
|
|
|
lr = read(fd,buf,16); /* read first 16 bytes */
|
|
|
|
csp = getsp();
|
|
|
|
if (lr == -1) {
|
|
|
|
printf("read failed: %d (sp = %d)\n",errno,csp);
|
|
|
|
cgetc();
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
l = close(fd);
|
|
|
|
if (l == -1) {
|
|
|
|
printf("close failed: %d\n",errno);
|
|
|
|
cgetc();
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
csp = getsp();
|
|
|
|
printf("\n\nThe data read: (%d bytes, sp = %d)\n",lr,csp);
|
|
|
|
for (i=0; i<lr; i++) {
|
|
|
|
printf("%02X ",buf[i]);
|
|
|
|
if (!((i+1) & 7)) printf("\n");
|
|
|
|
}
|
|
|
|
printf("\n\npress return to exit...");
|
|
|
|
getchar();
|
|
|
|
return(0);
|
|
|
|
}
|