1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
4 // Public Domain C Library - http://pdclib.sourceforge.net
5 // This code is Public Domain. Use, modify, and redistribute at will.
6 // ----------------------------------------------------------------------------
8 char * fgets( char * restrict s, int n, FILE * restrict stream ) { /* TODO */ };
10 /* PDPC code - unreviewed
13 In fgets, we have the following possibilites...
15 1. we found a genuine '\n' that terminated the search.
16 2. we hit the '\n' at the endbuf.
17 3. we hit the '\n' sentinel.
21 char *fgets(char *s, int n, FILE *stream)
37 if (stream->quickText)
39 p = stream->upto + n - 1;
41 if (p < stream->endbuf)
49 while ((*u++ = *t++) != '\n') ; /* tight inner loop */
54 register unsigned int *i1;
55 register unsigned int *i2;
56 register unsigned int z;
58 i1 = (unsigned int *)t;
59 i2 = (unsigned int *)u;
63 if ((z & 0xffU) == '\n') break;
65 if ((z & 0xffU) == '\n') break;
67 if ((z & 0xffU) == '\n') break;
69 if ((z & 0xffU) == '\n') break;
74 while ((*u++ = *t++) != '\n') ;
80 if (*(t - 2) == '\r') /* t is protected, u isn't */
94 processed = (int)(t - stream->upto) - 1;
101 while ((*u++ = *t++) != '\n') ; /* tight inner loop */
102 if (t <= stream->endbuf)
104 if (*(t - 2) == '\r') /* t is protected, u isn't */
118 processed = (int)(t - stream->upto) - 1;
119 stream->upto = t - 1;
138 if (stream->ungetCh != -1)
141 *u++ = (char)stream->ungetCh;
142 stream->ungetCh = -1;
147 p = stream->upto + (n - processed) - 1;
148 if (p < stream->endbuf)
155 while (((*u++ = *t) != '\n') && (*t++ != '\r')) ;
156 if (*(u - 1) == '\n')
163 while ((*u++ = *t++) != '\n') ;
168 while ((*u++ = *t++) != '\n') ; /* tight inner loop */
170 if (p < stream->endbuf)
174 if (((t <= p) && (p < stream->endbuf))
175 || ((t <= stream->endbuf) && (p >= stream->endbuf)))
177 if (stream->textMode)
181 if ((*(t - 1) == '\r') || (*(t - 1) == '\n'))
190 else if (*(t - 2) == '\r') /* t is protected, u isn't */
201 if (stream->textMode)
203 stream->quickText = 1;
207 else if (((t > p) && (p < stream->endbuf))
208 || ((t > stream->endbuf) && (p >= stream->endbuf)))
212 if (stream->textMode)
214 if (t > stream->endbuf)
216 if ((t - stream->upto) > 1)
218 if (*(t - 2) == '\r') /* t is protected, u isn't */
220 processed -= 1; /* preparation for add */
227 if ((*(t - 2) == '\r') && (*(t - 1) == '\n'))
239 else if (t > stream->endbuf)
250 if (stream->textMode)
252 stream->quickText = 1;
257 processed += (int)(t - stream->upto) - 1;
259 stream->bufStartR += (stream->endbuf - stream->fbuf);
261 rc = DosRead(stream->hfile, stream->fbuf, stream->szfbuf, &actualRead);
265 stream->errorInd = 1;
270 actualRead = __read(stream->hfile,
278 stream->errorInd = 1;
281 stream->endbuf = stream->fbuf + actualRead;
282 *stream->endbuf = '\n';
296 stream->upto = stream->fbuf;