sfreserve.c revision 7c2fbfb345896881c631598ee3852ce9ce33fb07
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* Copyright (c) 1985-2008 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Phong Vo <kpv@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Reserve a segment of data or buffer.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** Written by Kiem-Phong Vo.
7c2fbfb345896881c631598ee3852ce9ce33fb07April ChinVoid_t* sfreserve(Sfio_t* f, ssize_t size, int type)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* see if we need to bias toward SF_WRITE instead of the default SF_READ */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* return the last record */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if((f->mode&SF_RDWR) != f->mode && _sfmode(f,0,0) < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* iterate until get to a stream that has data or buffer space */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin /* set amount to perform IO */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin else if(size < 0 && n == 0 && f->push) /* maybe stack-pop */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin { iosz = sz - n; /* get enough to fulfill requirement */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin iosz = f->size - n; /* get as much as possible */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* do a buffer refill or flush */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(type == SF_LOCKR && f->extent < 0 && (f->flags&SF_SHARE) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(n == 0) /* peek-read only if there is no buffered data */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin { /* sfreserve(f,0,0) == sfread(f, sfreserve(f,-1,SF_LOCKR), 0) */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(n == now || sferror(f) || sfeof(f)) /* no progress */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* request was only to assess data availability */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(size == 0 || n == 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(n > 0) /* got data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(type == SF_LOCKR && size == 0 && (rsrv = _sfrsrv(f,0)) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(f->flags&SF_STRING) /* try extending string buffer */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(type != SF_LOCKR && sz > f->size && (rsrv = _sfrsrv(f,sz)) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if((n = SFREAD(f,(Void_t*)rsrv->data,sz)) >= sz) /* read side buffer */