10 static int op_close();
12 static int op_write();
13 static long op_lseek();
17 open(name
, flags
, mode
)
22 return (*__files
)(CMD_OPEN
, flags
, name
, mode
);
26 fileops(cmd
, fd
, buf
, len
)
32 case CMD_OPEN
: return op_open(buf
, fd
, len
);
33 case CMD_READ
: return op_read(fd
, buf
, len
);
35 case CMD_WRITE: return op_write(fd, buf, len);
36 case CMD_LSEEK: rv = op_lseek(fd, *(long*)buf, len);
38 if( rv == -1 ) return -1;
41 case CMD_CLOSE
: return op_close(fd
);
48 #define MAX_OPEN_FILES 5
49 ioblock _iob
[MAX_OPEN_FILES
];
56 ioblock
* cur
= &_iob
[fd
];
58 int amount_left_in_buffer
;
61 if (fd
< 0 || fd
>= MAX_OPEN_FILES
|| _iob
[fd
].block_read
== 0)
69 /* pull in next block as required */
70 if (cur
->amount_left
<= 0)
72 int read_len
= cur
->block_read(cur
,
74 (long) cur
->offset
/ sizeof(cur
->buffer
)
77 fprintf(stderr
, "br: returned %d\n", read_len
);
81 cur
->amount_left
= read_len
;
83 if (cur
->amount_left
> len
)
86 amount_to_copy
= cur
->amount_left
;
89 fprintf(stderr
, "r: len=%d, amount_left=%ld, offset=%ld, buf=%x\n",
90 len
, cur
->amount_left
, cur
->offset
,
91 (int) cur
->buffer
[cur
->offset
% sizeof(cur
->buffer
)]);
94 &cur
->buffer
[cur
->offset
% sizeof(cur
->buffer
)],
96 amount_read
+= amount_to_copy
;
97 len
-= amount_to_copy
;
98 cur
->offset
+= amount_to_copy
;
99 buf
+= amount_to_copy
;
100 cur
->amount_left
-= amount_to_copy
;
105 /****************************************************************************/
108 op_open(name
, flags
, mode
)
116 * discover whether the iob has been initialised or not
118 if (_iob
[0].flags
== 0)
120 _iob
[0].flags
= O_RDONLY
;
121 _iob
[1].flags
= O_WRONLY
;
122 _iob
[2].flags
= O_WRONLY
;
125 * discover next free iob
127 for (fd
= 3; fd
< MAX_OPEN_FILES
; ++fd
)
129 if (_iob
[fd
].block_read
== NULL
&& _iob
[fd
].block_write
== NULL
)
132 if (fd
>= MAX_OPEN_FILES
)
134 errno
= EMFILE
; /* too many open files */
139 * try and find the file
142 if (fsdos_open_file(cur
, name
, flags
, mode
) >= 0)
144 cur
->amount_left
= 0;
148 cur
->block_read
= NULL
; /* ensure that the file is closed */
149 cur
->block_write
= NULL
;
154 /****************************************************************************/
160 if (fd
< 0 || fd
>= MAX_OPEN_FILES
|| _iob
[0].flags
== 0)
167 ioblock
* cur
= &_iob
[fd
];
169 cur
->block_read
= NULL
;
170 cur
->block_write
= NULL
;
177 /****************************************************************************/