initio - initialize and wrap-up BASIC I/O


#include <basic/io.h>

extern struct biotab biotab[], *biofn;


struct biobuf *mkbiobuf(fd, isbuf, ln, s)
int fd, isbuf;
unsigned ln;
char *s;

int rembiobuf(b, flushflag)
struct biobuf *b;
int flushflag;


int doclose(bt)
struct biotab *bt;

int dezomb(p)
int p;

short fn;






Biotab is a list of biotab structures (see io(inc)).  Biotab[0] is reserved for standard input and output.  Biotab[1] through biotab[NFILES] are reserved for user files with the corresponding file numbers.  Biotab[NFILES+1] is reserved for the line printer (see filel(io)).  Biofn is the current file and points to an element of the biotab list or is zero which means the data area.  Most I/O functions use biofn as the file upon which to operate.  Upon completion, many I/O functions reset biofn to the standard input and output biotab

Initio initializes signals with trapsigs and initializes biofn and biobuf[0] for standard input and output using mkbiobuf

Mkbiobuf allocates, initializes for sequential I/O and returns a pointer to a biobuf structure.  Fd is the UNIX file descriptor.  If isbuf is set, a character buffer with length ln is needed; otherwise, a tty record is allocated using initty(io), the length of time to wait for input is set to ln seconds and the width of the device is set to STDWIDTH.  If a buffer is needed and s is not null, s is used; otherwise a buffer is allocated. 

Rembiobuf returns zero to indicate success closing and deallocating biobuf structure b.  If b refers to a tty, exitty is called (see initty(io)).  For buffered sequential files, any characters remaining in the buffer are written out, if flushflag is set, and the character buffer is deallocated.  Finally, the associated file descriptor is closed, if it is not the standard input or output, and the structure deallocated.  Rembiobuf performs the wrap-up on a biobuf structure, which represents either the input from or output to an open file. 

_Reset flushes and closes all files including standard input and output and the line printer using doclose.  Flushing and closing occurs only after a number of preliminary tasks: Ignsigs and errdisable are called to insure that no interruptions occur and no user code is executed in case of an error (see runerr(err)).  Also, the hang-up signal is sent to any process with which a pipe is open to ensure the proper disposal of such a process (see signal(2) and bopen(io)).  _Reset is called on exit or when the process receives a signal. 

Doclose returns zero to indicate success closing the file given by the biotab structure bt using rembiobuf to close the input and/or output elements of the file.  If the file is a pipe, the current process waits for the piped process to terminate using dezomb

Dezomb waits for the termination of the process with id p.  This function normally returns zero.  The waiting is performed using wait(2) which is called repeated until the given processís number is up.  As such, this function is used as an interface to wait(2).  In addition, if p is zero, this function uses fork(2) to create a process that immediately calls _exit(2) and the parent waits on that process.  This is intended to flush the zombie queue without blocking on the theory that such a first in, first out queue exists in all versions of UNIX and that this is the only reliable way to avoid ambiguities in the definition of zombie processes, specifically that kill(2) via signal zero can be used to detect whether or not a process has terminated, however, this state with respect to zombies varies amongst UNIX implementations.  To cope with this problem, the BASIC implementation calls dezomb with an argument of zero to finally put any zombies to rest and then calls kill with signal zero on the process id of interest to detect whether or not that process is running. 

Filen sets the current file to the file given by fnFiled sets the current file to the data area.  File0 sets the current file to the standard input and output. 

Trapsigs initializes signal handling.  All common signals are set to be trapped and will cause the hang-up signal to be sent to the line printer and _reset to be called.  The process will then commit seppuku by resetting the signal to its default condition and resending the signal to itself.  This method insures that the files are closed properly and the tty states are restored but still allows the original signal number to terminate the process. 

Ignsigs stipulates that all common signals are to be ignored.  This state is necessary during critical periods when the process is preparing for termination (see above) and when a chain is occurring (see chain(misc)). 

These functions support I/O initialization and wrap-up and do not directly implement any BASIC functions. 


io(inc), bclose(io), bopen(io), eof(io), filel(io), initty(io), chain(misc), run(misc), bend(err), runerr(err)
signal(2), exit(2) fork(2), kill(2), wait(2) in the UNIX Programmerís Manual


Rembiobuf, doclose and dezomb return non-zero in case of error. 

Filen generates error number 52 (Bad file number) if fn does not refer to an open file. 

from The Basmark QuickBASIC Programmerís Manual by Lawrence Leinweber