Changeset 1606 for zzuf


Ignore:
Timestamp:
Jan 7, 2007, 4:55:00 PM (14 years ago)
Author:
Sam Hocevar
Message:
  • Implemented BSD stdio by overriding srefill().
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/load-stream.c

    r1605 r1606  
    153153#endif
    154154
    155 #define FSEEK(fn, fn2) \
    156     do \
    157     { \
    158         int fd; \
    159         if(!_zz_ready) \
    160             LOADSYM(fn); \
    161         fd = fileno(stream); \
    162         if(!_zz_ready || !_zz_iswatched(fd)) \
    163             return ORIG(fn)(stream, offset, whence); \
    164         _zz_disabled = 1; \
    165         ret = ORIG(fn)(stream, offset, whence); \
    166         _zz_disabled = 0; \
    167         debug(STR(fn)"([%i], %lli, %i) = %i", \
    168               fd, (long long int)offset, whence, ret); \
     155#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
     156#   define FSEEK_FUZZ(fn2)
     157#else
     158#   define FSEEK_FUZZ(fn2) \
    169159        if(ret == 0) \
    170160        { \
     
    182172                    break; \
    183173            } \
    184         } \
     174        }
     175#endif
     176
     177#define FSEEK(fn, fn2) \
     178    do \
     179    { \
     180        int fd; \
     181        if(!_zz_ready) \
     182            LOADSYM(fn); \
     183        fd = fileno(stream); \
     184        if(!_zz_ready || !_zz_iswatched(fd)) \
     185            return ORIG(fn)(stream, offset, whence); \
     186        _zz_disabled = 1; \
     187        ret = ORIG(fn)(stream, offset, whence); \
     188        _zz_disabled = 0; \
     189        debug(STR(fn)"([%i], %lli, %i) = %i", \
     190              fd, (long long int)offset, whence, ret); \
     191        FSEEK_FUZZ(fn2) \
    185192    } while(0)
    186193
     
    215222    debug("rewind([%i])", fd);
    216223
     224#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
     225#else
    217226    /* FIXME: check what happens when rewind()ing a pipe */
    218227    _zz_setpos(fd, 0);
     228#endif
    219229}
    220230
    221231size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
    222232{
    223     long int pos, newpos;
     233    long int pos;
     234#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
     235#else
     236    long int newpos;
     237#endif
    224238    size_t ret;
    225239    int fd;
     
    238252          ptr, (long int)size, (long int)nmemb, fd, (long int)ret);
    239253
     254#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
     255#else
    240256    newpos = ftell(stream);
    241 #if defined HAVE___SREFILL /* Don't fuzz if we have __srefill() */
    242     if(newpos != pos)
    243         _zz_setpos(fd, ftell(stream));
    244 #else
    245257    /* XXX: the number of bytes read is not ret * size, because
    246258     * a partial read may have advanced the stream pointer. However,
     
    259271}
    260272
    261 #if defined HAVE___SREFILL /* Don't fuzz if we have __srefill() */
     273#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
    262274#   define FGETC_FUZZ
    263275#else
     
    319331        return fgets_orig(s, size, stream);
    320332
    321 #if defined HAVE___SREFILL /* Don't fuzz if we have __srefill() */
     333#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
    322334    _zz_disabled = 1;
    323335    ret = fgets_orig(s, size, stream);
    324336    _zz_disabled = 0;
    325     _zz_setpos(fd, ftell(stream));
    326337#else
    327338    if(size <= 0)
     
    375386        return ungetc_orig(c, stream);
    376387
     388#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
     389#else
    377390    _zz_addpos(fd, -1);
    378 #if defined HAVE___SREFILL /* Don't fuzz if we have __srefill() */
    379 #else
    380391    _zz_fuzz(fd, &ch, 1);
    381392#endif
     
    383394    ret = ungetc_orig((int)ch, stream);
    384395    _zz_disabled = 0;
     396
    385397    if(ret >= 0)
    386398        ret = c;
     399#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
     400#else
    387401    else
    388402        _zz_addpos(fd, 1); /* revert what we did */
     403#endif
     404
    389405    if(ret >= 0x20 && ret <= 0x7f)
    390406        debug("ungetc(0x%02x, [%i]) = 0x%02x '%c'", c, fd, ret, ret);
     
    493509{
    494510    char *ret;
    495 #if defined HAVE___SREFILL /* Don't fuzz if we have __srefill() */
     511#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
    496512#else
    497513    struct fuzz *fuzz;
     
    506522        return fgetln_orig(stream, len);
    507523
    508 #if defined HAVE___SREFILL /* Don't fuzz if we have __srefill() */
     524#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
    509525    _zz_disabled = 1;
    510526    ret = fgetln_orig(stream, len);
     
    547563int __srefill(FILE *fp)
    548564{
     565    off_t oldpos, newpos;
    549566    int ret, fd;
    550567
     
    552569        LOADSYM(__srefill);
    553570    fd = fileno(fp);
     571    if(!_zz_ready || !_zz_iswatched(fd))
     572        return __srefill_orig(fp);
     573
     574    oldpos = lseek(fd, 0, SEEK_CUR);
    554575    ret = __srefill_orig(fp);
    555     if(!_zz_ready || !_zz_iswatched(fd))
    556         return ret;
    557 
     576    newpos = lseek(fd, 0, SEEK_CUR);
    558577    if(ret != EOF)
     578    {
     579        if(newpos != -1)
     580            _zz_setpos(fd, newpos - fp->_r);
    559581        _zz_fuzz(fd, fp->_p, fp->_r);
     582        _zz_addpos(fd, fp->_r);
     583    }
    560584
    561585    if(!_zz_disabled)
Note: See TracChangeset for help on using the changeset viewer.