xref: /onnv-gate/usr/src/lib/libast/common/sfio/sfgetr.c (revision 12068:08a39a083754)
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 #include	"sfhdr.h"
234887Schin 
244887Schin /*	Read a record delineated by a character.
254887Schin **	The record length can be accessed via sfvalue(f).
264887Schin **
274887Schin **	Written by Kiem-Phong Vo
284887Schin */
294887Schin 
304887Schin #if __STD_C
sfgetr(Sfio_t * f,int rc,int type)318462SApril.Chin@Sun.COM char* sfgetr(Sfio_t *f, int rc, int type)
324887Schin #else
334887Schin char* sfgetr(f,rc,type)
348462SApril.Chin@Sun.COM Sfio_t*		f;	/* stream to read from	*/
358462SApril.Chin@Sun.COM int		rc;	/* record separator	*/
364887Schin int		type;
374887Schin #endif
384887Schin {
398462SApril.Chin@Sun.COM 	ssize_t		n, un;
408462SApril.Chin@Sun.COM 	uchar		*s, *ends, *us;
418462SApril.Chin@Sun.COM 	int		found;
428462SApril.Chin@Sun.COM 	Sfrsrv_t*	rsrv;
438462SApril.Chin@Sun.COM 	SFMTXDECL(f);
444887Schin 
458462SApril.Chin@Sun.COM 	SFMTXENTER(f, NIL(char*));
464887Schin 
474887Schin 	if(rc < 0 || (f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0) )
484887Schin 		SFMTXRETURN(f, NIL(char*));
494887Schin 	SFLOCK(f,0);
504887Schin 
514887Schin 	/* buffer to be returned */
524887Schin 	rsrv = NIL(Sfrsrv_t*);
534887Schin 	us = NIL(uchar*);
544887Schin 	un = 0;
554887Schin 	found = 0;
564887Schin 
574887Schin 	/* compatibility mode */
584887Schin 	type = type < 0 ? SF_LASTR : type == 1 ? SF_STRING : type;
594887Schin 
604887Schin 	if(type&SF_LASTR) /* return the broken record */
618462SApril.Chin@Sun.COM 	{	if((f->flags&SF_STRING) && (un = f->endb - f->next))
628462SApril.Chin@Sun.COM 		{	us = f->next;
638462SApril.Chin@Sun.COM 			f->next = f->endb;
648462SApril.Chin@Sun.COM 			found = 1;
658462SApril.Chin@Sun.COM 		}
668462SApril.Chin@Sun.COM 		else if((rsrv = f->rsrv) && (un = -rsrv->slen) > 0)
674887Schin 		{	us = rsrv->data;
684887Schin 			found = 1;
694887Schin 		}
704887Schin 		goto done;
714887Schin 	}
724887Schin 
734887Schin 	while(!found)
744887Schin 	{	/* fill buffer if necessary */
754887Schin 		if((n = (ends = f->endb) - (s = f->next)) <= 0)
764887Schin 		{	/* for unseekable devices, peek-read 1 record */
774887Schin 			f->getr = rc;
784887Schin 			f->mode |= SF_RC;
794887Schin 
804887Schin 			/* fill buffer the conventional way */
814887Schin 			if(SFRPEEK(f,s,n) <= 0)
824887Schin 			{	us = NIL(uchar*);
834887Schin 				goto done;
844887Schin 			}
854887Schin 			else
864887Schin 			{	ends = s+n;
874887Schin 				if(f->mode&SF_RC)
884887Schin 				{	s = ends[-1] == rc ? ends-1 : ends;
894887Schin 					goto do_copy;
904887Schin 				}
914887Schin 			}
924887Schin 		}
934887Schin 
944887Schin #if _lib_memchr
954887Schin 		if(!(s = (uchar*)memchr((char*)s,rc,n)))
964887Schin 			s = ends;
974887Schin #else
984887Schin 		while(*s != rc)
994887Schin 			if((s += 1) == ends)
1004887Schin 				break;
1014887Schin #endif
1024887Schin 	do_copy:
1034887Schin 		if(s < ends) /* found separator */
1044887Schin 		{	s += 1;		/* include the separator */
1054887Schin 			found = 1;
1064887Schin 
1074887Schin 			if(!us &&
1084887Schin 			   (!(type&SF_STRING) || !(f->flags&SF_STRING) ||
1094887Schin 			    ((f->flags&SF_STRING) && (f->bits&SF_BOTH) ) ) )
1104887Schin 			{	/* returning data in buffer */
1114887Schin 				us = f->next;
1124887Schin 				un = s - f->next;
1134887Schin 				f->next = s;
1144887Schin 				goto done;
1154887Schin 			}
1164887Schin 		}
1174887Schin 
1184887Schin 		/* amount to be read */
1194887Schin 		n = s - f->next;
1204887Schin 
1218462SApril.Chin@Sun.COM 		if(!found && (_Sfmaxr > 0 && un+n+1 >= _Sfmaxr || (f->flags&SF_STRING))) /* already exceed limit */
1224887Schin 		{	us = NIL(uchar*);
1234887Schin 			goto done;
1244887Schin 		}
1254887Schin 
1264887Schin 		/* get internal buffer */
1274887Schin 		if(!rsrv || rsrv->size < un+n+1)
1284887Schin 		{	if(rsrv)
1294887Schin 				rsrv->slen = un;
1304887Schin 			if((rsrv = _sfrsrv(f,un+n+1)) != NIL(Sfrsrv_t*))
1314887Schin 				us = rsrv->data;
1324887Schin 			else
1334887Schin 			{	us = NIL(uchar*);
1344887Schin 				goto done;
1354887Schin 			}
1364887Schin 		}
1374887Schin 
1384887Schin 		/* now copy data */
1394887Schin 		s = us+un;
1404887Schin 		un += n;
1414887Schin 		ends = f->next;
1424887Schin 		f->next += n;
1434887Schin 		MEMCPY(s,ends,n);
1444887Schin 	}
1454887Schin 
1464887Schin done:
1474887Schin 	_Sfi = f->val = un;
1484887Schin 	f->getr = 0;
1494887Schin 	if(found && rc != 0 && (type&SF_STRING) )
1504887Schin 	{	us[un-1] = '\0';
1514887Schin 		if(us >= f->data && us < f->endb)
1524887Schin 		{	f->getr = rc;
1534887Schin 			f->mode |= SF_GETR;
1544887Schin 		}
1554887Schin 	}
1564887Schin 
1574887Schin 	/* prepare for a call to get the broken record */
1584887Schin 	if(rsrv)
1594887Schin 		rsrv->slen = found ? 0 : -un;
1604887Schin 
1614887Schin 	SFOPEN(f,0);
1624887Schin 
1634887Schin 	if(us && (type&SF_LOCKR) )
1644887Schin 	{	f->mode |= SF_PEEK|SF_GETR;
1654887Schin 		f->endr = f->data;
1664887Schin 	}
1674887Schin 
1684887Schin 	SFMTXRETURN(f, (char*)us);
1694887Schin }
170