ack/lang/basic/lib/file.c

132 lines
2.2 KiB
C
Raw Permalink Normal View History

1984-11-29 14:22:02 +00:00
#include <stdio.h>
#include "bc_io.h"
#include "lib.h"
1984-11-29 14:22:02 +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
*/
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
printf("setchannel %d\n", index);
1984-11-29 14:22:02 +00:00
#endif
fflush(_chanwr);
if (index == -1)
1984-11-29 14:22:02 +00:00
{
_chann = -1;
_chanrd = stdin;
_chanwr = stdout;
1984-11-29 14:22:02 +00:00
return;
}
if (index < 0 || index > 15)
1984-11-29 14:22:02 +00:00
error(27);
_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
printf("_asschn %d\n", _chann);
1984-11-29 14:22:02 +00:00
#endif
if (_chann == -1)
return;
1984-11-29 14:22:02 +00:00
#ifdef DEBUG
printf(" file %d\n", _fdtable[_chann].fd);
#endif
if (_chann < 0 || _chann > 15)
1984-11-29 14:22:02 +00:00
error(27);
if (_fdtable[_chann].fd == 0)
1984-11-29 14:22:02 +00:00
error(39);
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
{
if (nr < 1 || nr > 15 || _fdtable[nr].fd == 0)
error(3);
1984-11-29 14:22:02 +00:00
fclose(_fdtable[nr].fd);
_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 */
FILE* f;
1984-11-29 14:22:02 +00:00
int m;
#ifdef DEBUG
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 */
if (_fdtable[_chann].fd)
1984-11-29 14:22:02 +00:00
error(30);
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);
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);
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);
m = RMODE;
1984-11-29 14:22:02 +00:00
break;
default:
printf("file mode %s\n", mode->strval);
1984-11-29 14:22:02 +00:00
error(29);
}
_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
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
{
FILE* fd;
1984-11-29 14:22:02 +00:00
char c;
if (channel < 0 || channel > 15)
error(3);
fd = _fdtable[channel].fd;
if (fd == 0)
1984-11-29 14:22:02 +00:00
error(3);
c = fgetc(fd);
if (feof(_fdtable[channel].fd))
return (-1);
ungetc(c, fd);
return (0);
1984-11-29 14:22:02 +00:00
}
void _closeall(void)
1984-11-29 14:22:02 +00:00
{
/* close all open files */
int i;
for (i = 1; i < 16; i++)
if (_fdtable[i].fd)
_clochn(i);
1984-11-29 14:22:02 +00:00
}