/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#include "sfhdr.h"2223/* Fill the buffer of a stream with data.24** If n < 0, sffilbuf() attempts to fill the buffer if it's empty.25** If n == 0, if the buffer is not empty, just return the first byte;26** otherwise fill the buffer and return the first byte.27** If n > 0, even if the buffer is not empty, try a read to get as28** close to n as possible. n is reset to -1 if stack pops.29**30** Written by Kiem-Phong Vo31*/3233#if __STD_C34int _sffilbuf(Sfio_t* f, reg int n)35#else36int _sffilbuf(f,n)37Sfio_t* f; /* fill the read buffer of this stream */38reg int n; /* see above */39#endif40{41reg ssize_t r;42reg int first, local, rcrv, rc, justseek;43SFMTXDECL(f); /* declare a local stream variable for multithreading */4445SFMTXENTER(f,-1);4647GETLOCAL(f,local);4849/* any peek data must be preserved across stacked streams */50rcrv = f->mode&(SF_RC|SF_RV|SF_LOCK);51rc = f->getr;5253justseek = f->bits&SF_JUSTSEEK; f->bits &= ~SF_JUSTSEEK;5455for(first = 1;; first = 0, (f->mode &= ~SF_LOCK) )56{ /* check mode */57if(SFMODE(f,local) != SF_READ && _sfmode(f,SF_READ,local) < 0)58SFMTXRETURN(f,-1);59SFLOCK(f,local);6061/* current extent of available data */62if((r = f->endb-f->next) > 0)63{ /* on first iteration, n is amount beyond current buffer;64afterward, n is the exact amount requested */65if((first && n <= 0) || (!first && n <= r) ||66(f->flags&SF_STRING))67break;6869/* try shifting left to make room for new data */70if(!(f->bits&SF_MMAP) && f->next > f->data &&71n > (f->size - (f->endb-f->data)) )72{ ssize_t s = r;7374/* try to maintain block alignment */75if(f->blksz > 0 && (f->here%f->blksz) == 0 )76{ s = ((r + f->blksz-1)/f->blksz)*f->blksz;77if(s+n > f->size)78s = r;79}8081memmove(f->data, f->endb-s, s);82f->next = f->data + (s-r);83f->endb = f->data + s;84}85}86else if(!(f->flags&SF_STRING) && !(f->bits&SF_MMAP) )87f->next = f->endb = f->endr = f->data;8889if(f->bits&SF_MMAP)90r = n > 0 ? n : f->size;91else if(!(f->flags&SF_STRING) )92{ r = f->size - (f->endb - f->data); /* available buffer */93if(n > 0)94{ if(r > n && f->extent < 0 && (f->flags&SF_SHARE) )95r = n; /* read only as much as requested */96else if(justseek && n <= f->iosz && f->iosz <= f->size)97r = f->iosz; /* limit buffer filling */98}99}100101/* SFRD takes care of discipline read and stack popping */102f->mode |= rcrv;103f->getr = rc;104if((r = SFRD(f,f->endb,r,f->disc)) >= 0)105{ r = f->endb - f->next;106break;107}108}109110SFOPEN(f,local);111112rcrv = (n == 0) ? (r > 0 ? (int)(*f->next++) : EOF) : (int)r;113114SFMTXRETURN(f,rcrv);115}116117118