1984-11-29 14:22:02 +00:00
|
|
|
#include <stdio.h>
|
1985-02-04 22:18:16 +00:00
|
|
|
#include "bc_io.h"
|
2018-06-23 22:35:51 +00:00
|
|
|
#include "lib.h"
|
1984-11-29 14:22:02 +00:00
|
|
|
|
2016-12-12 20:16:32 +00:00
|
|
|
Filedesc _fdtable[16];
|
1984-11-29 14:22:02 +00:00
|
|
|
/* BASIC file descriptor table */
|
|
|
|
/* Channel assignment:
|
|
|
|
-1 terminal IO
|
|
|
|
0 data file
|
|
|
|
1-15 user files
|
|
|
|
*/
|
|
|
|
|
2016-12-12 20:16:32 +00:00
|
|
|
int _chann = -1;
|
|
|
|
FILE* _chanrd = stdin;
|
|
|
|
FILE* _chanwr = stdout;
|
1984-11-29 14:22:02 +00:00
|
|
|
|
2016-12-12 20:15:25 +00:00
|
|
|
void _setchan(int index)
|
1984-11-29 14:22:02 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2016-12-12 20:16:32 +00:00
|
|
|
printf("setchannel %d\n", index);
|
1984-11-29 14:22:02 +00:00
|
|
|
#endif
|
|
|
|
fflush(_chanwr);
|
2016-12-12 20:16:32 +00:00
|
|
|
if (index == -1)
|
1984-11-29 14:22:02 +00:00
|
|
|
{
|
2016-12-12 20:16:32 +00:00
|
|
|
_chann = -1;
|
|
|
|
_chanrd = stdin;
|
|
|
|
_chanwr = stdout;
|
1984-11-29 14:22:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-12-12 20:16:32 +00:00
|
|
|
if (index < 0 || index > 15)
|
1984-11-29 14:22:02 +00:00
|
|
|
error(27);
|
2016-12-12 20:16:32 +00:00
|
|
|
_chann = index;
|
|
|
|
_chanrd = _chanwr = _fdtable[index].fd;
|
1984-11-29 14:22:02 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:15:25 +00:00
|
|
|
void _asschn(void)
|
1984-11-29 14:22:02 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2016-12-12 20:16:32 +00:00
|
|
|
printf("_asschn %d\n", _chann);
|
1984-11-29 14:22:02 +00:00
|
|
|
#endif
|
2016-12-12 20:16:32 +00:00
|
|
|
if (_chann == -1)
|
|
|
|
return;
|
1984-11-29 14:22:02 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf(" file %d\n", _fdtable[_chann].fd);
|
|
|
|
#endif
|
2016-12-12 20:16:32 +00:00
|
|
|
if (_chann < 0 || _chann > 15)
|
1984-11-29 14:22:02 +00:00
|
|
|
error(27);
|
2016-12-12 20:16:32 +00:00
|
|
|
if (_fdtable[_chann].fd == 0)
|
1984-11-29 14:22:02 +00:00
|
|
|
error(39);
|
2016-12-12 20:16:32 +00:00
|
|
|
if (feof(_fdtable[_chann].fd))
|
1984-11-29 14:22:02 +00:00
|
|
|
error(2);
|
|
|
|
}
|
|
|
|
|
2016-12-12 20:15:25 +00:00
|
|
|
void _clochn(int nr)
|
1984-11-29 14:22:02 +00:00
|
|
|
{
|
2016-12-12 20:16:32 +00:00
|
|
|
if (nr < 1 || nr > 15 || _fdtable[nr].fd == 0)
|
|
|
|
error(3);
|
1984-11-29 14:22:02 +00:00
|
|
|
fclose(_fdtable[nr].fd);
|
2016-12-12 20:16:32 +00:00
|
|
|
_fdtable[nr].fd = 0;
|
|
|
|
_fdtable[nr].fname = 0;
|
1984-11-29 14:22:02 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:15:25 +00:00
|
|
|
void _opnchn(int reclen, String* fname, String* mode)
|
1984-11-29 14:22:02 +00:00
|
|
|
{
|
|
|
|
/* channel has been set */
|
2016-12-12 20:16:32 +00:00
|
|
|
FILE* f;
|
1984-11-29 14:22:02 +00:00
|
|
|
int m;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2016-12-12 20:16:32 +00:00
|
|
|
printf("open %d %s %s \n", reclen, mode->strval, fname->strval);
|
1984-11-29 14:22:02 +00:00
|
|
|
#endif
|
|
|
|
/* check for opened/closed file */
|
2016-12-12 20:16:32 +00:00
|
|
|
if (_fdtable[_chann].fd)
|
1984-11-29 14:22:02 +00:00
|
|
|
error(30);
|
2016-12-12 20:16:32 +00:00
|
|
|
switch (*mode->strval)
|
1984-11-29 14:22:02 +00:00
|
|
|
{
|
|
|
|
case 'O':
|
|
|
|
case 'o':
|
2022-07-17 10:58:48 +00:00
|
|
|
if ((f = fopen(fname->strval, "wb")) == NULL)
|
1984-11-29 14:22:02 +00:00
|
|
|
error(28);
|
2016-12-12 20:16:32 +00:00
|
|
|
m = OMODE;
|
1984-11-29 14:22:02 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
case 'i':
|
2022-07-17 10:58:48 +00:00
|
|
|
if ((f = fopen(fname->strval, "rb")) == NULL)
|
1984-11-29 14:22:02 +00:00
|
|
|
error(28);
|
2016-12-12 20:16:32 +00:00
|
|
|
m = IMODE;
|
1984-11-29 14:22:02 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
2022-07-17 10:58:48 +00:00
|
|
|
if ((f = fopen(fname->strval, "ab")) == NULL)
|
1984-11-29 14:22:02 +00:00
|
|
|
error(28);
|
2016-12-12 20:16:32 +00:00
|
|
|
m = RMODE;
|
1984-11-29 14:22:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-12-12 20:16:32 +00:00
|
|
|
printf("file mode %s\n", mode->strval);
|
1984-11-29 14:22:02 +00:00
|
|
|
error(29);
|
|
|
|
}
|
2016-12-12 20:16:32 +00:00
|
|
|
_chanwr = _chanrd = _fdtable[_chann].fd = f;
|
|
|
|
_fdtable[_chann].fname = fname->strval;
|
|
|
|
_fdtable[_chann].reclength = reclen;
|
|
|
|
_fdtable[_chann].mode = m;
|
1984-11-29 14:22:02 +00:00
|
|
|
#ifdef DEBUG
|
2016-12-12 20:16:32 +00:00
|
|
|
printf("file descr %d\n", f);
|
1984-11-29 14:22:02 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-12-12 20:15:25 +00:00
|
|
|
int _ioeof(int channel)
|
1984-11-29 14:22:02 +00:00
|
|
|
{
|
2016-12-12 20:16:32 +00:00
|
|
|
FILE* fd;
|
1984-11-29 14:22:02 +00:00
|
|
|
char c;
|
2016-12-12 20:16:32 +00:00
|
|
|
if (channel < 0 || channel > 15)
|
|
|
|
error(3);
|
|
|
|
fd = _fdtable[channel].fd;
|
|
|
|
if (fd == 0)
|
1984-11-29 14:22:02 +00:00
|
|
|
error(3);
|
2016-12-12 20:16:32 +00:00
|
|
|
c = fgetc(fd);
|
|
|
|
if (feof(_fdtable[channel].fd))
|
|
|
|
return (-1);
|
|
|
|
ungetc(c, fd);
|
|
|
|
return (0);
|
1984-11-29 14:22:02 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 12:34:14 +00:00
|
|
|
void _closeall(void)
|
1984-11-29 14:22:02 +00:00
|
|
|
{
|
|
|
|
/* close all open files */
|
|
|
|
int i;
|
2016-12-12 20:16:32 +00:00
|
|
|
for (i = 1; i < 16; i++)
|
|
|
|
if (_fdtable[i].fd)
|
|
|
|
_clochn(i);
|
1984-11-29 14:22:02 +00:00
|
|
|
}
|