Changeset 1135 for Whitix

Show
Ignore:
Timestamp:
10/14/08 19:39:48 (1 month ago)
Author:
mwhitworth
Message:

Add to fseek, various checks, optimize case where we can seek inside the buffer.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • Whitix/branches/keobject/user/libc/stdio/file.c

    r910 r1135  
    248248int fseek(FILE* stream,long offset,int whence) 
    249249{ 
     250        long pos; 
     251         
    250252        if (!stream) 
    251                 return 0; 
    252  
    253         int position=SysSeek(stream->fd,offset,whence); 
    254  
    255         if (position >= 0) 
    256         { 
    257                 stream->position=position; 
     253                return -1; 
     254                 
     255        stream->flags &= ~FILE_EOF; 
     256         
     257        if (stream->buf && !(stream->flags & _IONBF)) 
     258        { 
     259                pos = ftell(stream); 
     260                 
     261                if (whence == SEEK_CUR) 
     262                { 
     263                        offset += pos; 
     264                        whence = SEEK_SET; 
     265                } 
     266                 
     267                if (whence == SEEK_SET && pos-offset <= stream->ptr - stream->buf && offset-pos <= stream->count) 
     268                { 
     269                        stream->ptr += offset-pos; 
     270                        stream->count += pos-offset; 
     271                         
     272                        stream->position = offset; 
     273                         
     274                        return 0; 
     275                } 
     276        } 
     277         
     278        pos = SysSeek(stream->fd,offset,whence); 
     279 
     280        if (pos >= 0) 
     281        { 
     282                stream->position=pos; 
    258283                stream->count=0; /* Force fread to re-read buffer. */ 
     284                stream->ptr = stream->buf; 
    259285        }else{ 
    260                 errno=-position; 
     286                errno=-pos; 
    261287                stream->flags |= FILE_ERROR; 
    262                 return 1; 
     288                return -1; 
    263289        } 
    264290 
     
    402428size_t fwrite_unlocked(const void* ptr, size_t size, size_t n, FILE* stream) 
    403429{ 
    404         printf("fwrite_unlocked\n"); 
    405         return 0; 
     430        return fwrite(ptr, size, n, stream); 
    406431} 
    407432