14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin
244887Schin #include "stdhdr.h"
254887Schin
264887Schin extern char*
_stdgets(Sfio_t * f,char * us,int n,int isgets)274887Schin _stdgets(Sfio_t* f, char* us, int n, int isgets)
284887Schin {
294887Schin int p;
304887Schin unsigned char* is;
314887Schin unsigned char* ps;
324887Schin
334887Schin if(n <= 0 || !us || (f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0))
344887Schin return NIL(char*);
354887Schin
364887Schin SFLOCK(f,0);
374887Schin
384887Schin n -= 1;
394887Schin is = (uchar*)us;
404887Schin
414887Schin while(n)
424887Schin { /* peek the read buffer for data */
434887Schin if((p = f->endb - (ps = f->next)) <= 0 )
444887Schin { f->getr = '\n';
454887Schin f->mode |= SF_RC;
464887Schin if(SFRPEEK(f,ps,p) <= 0)
474887Schin break;
484887Schin }
494887Schin
504887Schin if(p > n)
514887Schin p = n;
524887Schin
534887Schin #if _lib_memccpy
544887Schin if((ps = (uchar*)memccpy((char*)is,(char*)ps,'\n',p)) != NIL(uchar*))
554887Schin p = ps-is;
564887Schin is += p;
574887Schin ps = f->next+p;
584887Schin #else
594887Schin if(!(f->flags&(SF_BOTH|SF_MALLOC)))
604887Schin { while(p-- && (*is++ = *ps++) != '\n')
614887Schin ;
624887Schin p = ps-f->next;
634887Schin }
644887Schin else
654887Schin { reg int c = ps[p-1];
664887Schin if(c != '\n')
674887Schin ps[p-1] = '\n';
684887Schin while((*is++ = *ps++) != '\n')
694887Schin ;
704887Schin if(c != '\n')
714887Schin { f->next[p-1] = c;
724887Schin if((ps-f->next) >= p)
734887Schin is[-1] = c;
744887Schin }
754887Schin }
764887Schin #endif
774887Schin
784887Schin /* gobble up read data and continue */
794887Schin f->next = ps;
804887Schin if(is[-1] == '\n')
814887Schin break;
824887Schin else if(n > 0)
834887Schin n -= p;
844887Schin }
854887Schin
864887Schin if((_Sfi = is - ((uchar*)us)) <= 0)
874887Schin us = NIL(char*);
884887Schin else if(isgets && is[-1] == '\n')
894887Schin { is[-1] = '\0';
904887Schin _Sfi -= 1;
914887Schin }
924887Schin else *is = '\0';
934887Schin
944887Schin SFOPEN(f,0);
954887Schin return us;
964887Schin }
974887Schin
984887Schin char*
fgets(char * s,int n,Sfio_t * f)994887Schin fgets(char* s, int n, Sfio_t* f)
1004887Schin {
1014887Schin STDIO_PTR(f, "fgets", char*, (char*, int, Sfio_t*), (s, n, f))
1024887Schin
1034887Schin return _stdgets(f, s, n, 0);
1044887Schin }
1054887Schin
1064887Schin char*
gets(char * s)1074887Schin gets(char* s)
1084887Schin {
1094887Schin return _stdgets(sfstdin, s, BUFSIZ, 1);
1104887Schin }
111