scsh-0.5/scsh/linux/stdio_dep.c

79 lines
1.7 KiB
C

/* Copyright (c) 1994 by Olin Shivers.
** Copyright (c) 1994-1995 by Brian D. Carlstrom.
**
** This file implements the char-ready? procedure for file descriptors
** and Scsh's fdports. It is not Posix, so it must be implemented for
** each OS to which scsh is ported.
**
** This version assumes two things:
** - the existence of select to tell if there is data
** available for the file descriptor.
** - the existence of the _cnt field in the stdio FILE struct, telling
** if there is any buffered input in the struct.
**
** Most Unixes have these things, so this file should work for them.
** However, Your Mileage May Vary.
**
** You could also replace the select() with a iotctl(FIONREAD) call, if you
** had one but not the other.
** -Olin&Brian
*/
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include "libcig.h"
#include <errno.h>
/* These two procs return #t if data ready, #f data not ready,
** and errno if error.
*/
scheme_value char_ready_fdes(int fd)
{
fd_set readfds;
struct timeval timeout;
int result;
FD_ZERO(&readfds);
FD_SET(fd,&readfds);
timeout.tv_sec=0;
timeout.tv_usec=0;
result=select(fd+1, &readfds, NULL, NULL, &timeout);
if(result == -1 )
return(ENTER_FIXNUM(errno));
if(result)
return(SCHTRUE);
return(SCHFALSE);
}
scheme_value stream_char_readyp(FILE *f)
{
int fd = fileno(f);
return (f->_IO_read_ptr < f->_IO_read_end) ? SCHTRUE : char_ready_fdes(fd);
}
void setfileno(FILE *fs, int fd)
{
fs->_fileno = fd;
}
int fbufcount(FILE *fs)
{
return((fs->_IO_read_end)-(fs->_IO_read_ptr));
}
int ibuf_empty(FILE *fs)
{
return((fs->_IO_read_end)-(fs->_IO_read_ptr) <= 0);
}
int obuf_full(FILE *fs)
{
return((fs->_IO_write_end)-(fs->_IO_write_ptr) <= 0);
}