T
tm
Is there a portable solution to find out how many chars are
buffered in FILE *aFile. This interests me for a file in read
mode and when the file is a FIFO (opened with popen()).
The background is:
I want to write the function inputReady(FILE *aFile) which should
return 1 when reading from aFile will not block. Assume that aFile
is opened with popen("someProgram", "r"). This way it is a FIFO
file. Depending on someProgram reading from aFile may block or may
succeed immediate. This should be determined with inputReady().
Currently I use the following definition of inputReady:
int inputReady (FILE *aFile)
{
int file_no;
int nfds;
fd_set readfds;
struct timeval timeout;
int select_result;
int result;
/* inputReady */
file_no = fileno(aFile);
if (file_no != -1) {
FD_ZERO(&readfds);
FD_SET(file_no, &readfds);
nfds = (int) file_no + 1;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
/* printf("select(%d, %d)\n", nfds, file_no); */
select_result = select(nfds, &readfds, NULL, NULL, &timeout);
/* printf("select_result: %d\n", select_result); */
if (unlikely(select_result < 0)) {
raise_error(FILE_ERROR);
result = 0;
} else {
result = FD_ISSET(file_no, &readfds);
} /* if */
} else {
raise_error(FILE_ERROR);
result = 0;
} /* if */
/* printf("filInputReady --> %d\n", result); */
return result;
} /* inputReady */
This function does not work correctly: It may return 0 although
a getc() would not block. This happens because of the characters
buffered by FILE *aFile. Since all the logic should be inside of
inputReady() using
setvbuf(aFile, NULL, _IONBF, 0)
to switch of buffering off is not an option. Instead I would like
to use something like fbufsize(aFile) which should tell me how many
chars are buffered in aFile. On my Linux system the include
file "libio.h" contains the macro:
#define _IO_getc_unlocked(_fp) \
(_IO_BE ((_fp)->_IO_read_ptr >= (_fp)->_IO_read_end, 0) \
? __uflow (_fp) : *(unsigned char *) (_fp)->_IO_read_ptr++)
From this definition I can see that fbufsize(aFile) could be
defined as:
#define fbufsize(_fp) ((_fp)->_IO_read_end - (_fp)->_IO_read_ptr)
The problem is: This is highly unportable.
My question is: Is there a portable solution for my problem?
Many thanks in advance.
Greetings Thomas Mertes
--
Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
buffered in FILE *aFile. This interests me for a file in read
mode and when the file is a FIFO (opened with popen()).
The background is:
I want to write the function inputReady(FILE *aFile) which should
return 1 when reading from aFile will not block. Assume that aFile
is opened with popen("someProgram", "r"). This way it is a FIFO
file. Depending on someProgram reading from aFile may block or may
succeed immediate. This should be determined with inputReady().
Currently I use the following definition of inputReady:
int inputReady (FILE *aFile)
{
int file_no;
int nfds;
fd_set readfds;
struct timeval timeout;
int select_result;
int result;
/* inputReady */
file_no = fileno(aFile);
if (file_no != -1) {
FD_ZERO(&readfds);
FD_SET(file_no, &readfds);
nfds = (int) file_no + 1;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
/* printf("select(%d, %d)\n", nfds, file_no); */
select_result = select(nfds, &readfds, NULL, NULL, &timeout);
/* printf("select_result: %d\n", select_result); */
if (unlikely(select_result < 0)) {
raise_error(FILE_ERROR);
result = 0;
} else {
result = FD_ISSET(file_no, &readfds);
} /* if */
} else {
raise_error(FILE_ERROR);
result = 0;
} /* if */
/* printf("filInputReady --> %d\n", result); */
return result;
} /* inputReady */
This function does not work correctly: It may return 0 although
a getc() would not block. This happens because of the characters
buffered by FILE *aFile. Since all the logic should be inside of
inputReady() using
setvbuf(aFile, NULL, _IONBF, 0)
to switch of buffering off is not an option. Instead I would like
to use something like fbufsize(aFile) which should tell me how many
chars are buffered in aFile. On my Linux system the include
file "libio.h" contains the macro:
#define _IO_getc_unlocked(_fp) \
(_IO_BE ((_fp)->_IO_read_ptr >= (_fp)->_IO_read_end, 0) \
? __uflow (_fp) : *(unsigned char *) (_fp)->_IO_read_ptr++)
From this definition I can see that fbufsize(aFile) could be
defined as:
#define fbufsize(_fp) ((_fp)->_IO_read_end - (_fp)->_IO_read_ptr)
The problem is: This is highly unportable.
My question is: Is there a portable solution for my problem?
Many thanks in advance.
Greetings Thomas Mertes
--
Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.