source: trunk/sys/dietlibc/fgetc_unlocked.c @ 226

Last change on this file since 226 was 1, checked in by alain, 8 years ago

First import

File size: 1019 bytes
Line 
1#include "dietstdio.h"
2#include <unistd.h>
3
4int fgetc_unlocked(FILE *stream) {
5  unsigned char c;
6  if (!(stream->flags&CANREAD)) goto kaputt;
7
8  if (stream->ungotten) {
9    stream->ungotten=0;
10    return stream->ungetbuf;
11  }
12
13  log_msg("fgetc fd %d, mb %d, bs %d\n", stream->fd, stream->bm, stream->bs);
14  /* common case first */
15  if (stream->bm<stream->bs)
16  {
17    log_msg("fgetc fd %d, %c\n", stream->fd, stream->buf[stream->bm]);
18    return (unsigned char)stream->buf[stream->bm++];
19  }
20
21  if (feof_unlocked(stream))
22    return EOF;
23 
24  if (__fflush4(stream,BUFINPUT)) return EOF;
25  if (stream->bm>=stream->bs) {
26    ssize_t len= read(stream->fd,stream->buf,stream->buflen);
27    if (len==0) {
28      stream->flags|=EOFINDICATOR;
29      return EOF;
30    } else if (len<0) {
31kaputt:
32      stream->flags|=ERRORINDICATOR;
33      return EOF;
34    }
35    stream->bm=0;
36    stream->bs=len;
37  }
38  c=stream->buf[stream->bm];
39  ++stream->bm;
40  return c;
41}
42
43int fgetc(FILE* stream) __attribute__((weak,alias("fgetc_unlocked")));
Note: See TracBrowser for help on using the repository browser.