44 lines
913 B
C
44 lines
913 B
C
#include <stdio.h>
|
|
#include "fileutil.h"
|
|
|
|
int fgetc(FILE *fp)
|
|
{
|
|
if(!fp->readable) {
|
|
fp->error = 1;
|
|
return EOF;
|
|
}
|
|
|
|
/* For this function we inline __fp_fread2() and__fp_buffered_read() in
|
|
order to maintain decent performance */
|
|
unsigned char c;
|
|
__fp_buffer_mode_read(fp);
|
|
|
|
/* No buffered data available, non-buffered mode */
|
|
if(!fp->buf) {
|
|
ssize_t rc = __fp_read(fp, &c, 1);
|
|
return (rc == 1) ? c : EOF;
|
|
}
|
|
|
|
/* If there is no data, get some */
|
|
if(fp->bufpos >= fp->bufread) {
|
|
ssize_t rc = __fp_read(fp, fp->buf, fp->bufsize);
|
|
if(rc <= 0)
|
|
return EOF;
|
|
fp->bufread = rc;
|
|
}
|
|
|
|
/* Get a byte from the buffer */
|
|
c = fp->buf[fp->bufpos++];
|
|
fp->bufungetc -= (fp->bufungetc > 0);
|
|
|
|
/* Rewind the buffer if at end, and clear _IONBF ungetc() buffers */
|
|
if(fp->bufpos >= fp->bufread) {
|
|
fp->bufread = 0;
|
|
fp->bufpos = 0;
|
|
if(fp->bufmode == _IONBF)
|
|
__fp_remove_buffer(fp);
|
|
}
|
|
|
|
return c;
|
|
}
|