Changeset 1482


Ignore:
Timestamp:
12/15/06 14:20:01 (6 years ago)
Author:
sam
Message:
  • Implemented lseek() and factored lseek64() code.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/preload.c

    r1479 r1482  
    3434#include <stdlib.h> 
    3535#include <fcntl.h> 
    36 #include <errno.h> 
    3736#include <regex.h> 
    3837 
     
    5655static int     (*open64_orig)  (const char *file, int oflag, ...); 
    5756static ssize_t (*read_orig)    (int fd, void *buf, size_t count); 
     57static off_t   (*lseek_orig)   (int fd, off_t offset, int whence); 
    5858static off64_t (*lseek64_orig) (int fd, off64_t offset, int whence); 
    5959static int     (*close_orig)   (int fd); 
     
    8080    LOADSYM(open64); 
    8181    LOADSYM(read); 
     82    LOADSYM(lseek); 
    8283    LOADSYM(lseek64); 
    8384    LOADSYM(close); 
     
    8990 
    9091/* Our function wrappers */ 
    91 #define FOPEN(fn, path, mode) \ 
     92#define FOPEN(fn) \ 
    9293    do \ 
    9394    { \ 
     
    117118FILE *fopen(const char *path, const char *mode) 
    118119{ 
    119     FILE *ret; FOPEN(fopen, path, mode); return ret; 
     120    FILE *ret; FOPEN(fopen); return ret; 
    120121} 
    121122 
    122123FILE *fopen64(const char *path, const char *mode) 
    123124{ 
    124     FILE *ret; FOPEN(fopen64, path, mode); return ret; 
     125    FILE *ret; FOPEN(fopen64); return ret; 
    125126} 
    126127 
     
    197198} 
    198199 
    199 #define OPEN(fn, file, oflag) \ 
     200#define OPEN(fn) \ 
    200201    do \ 
    201202    { \ 
     
    241242int open(const char *file, int oflag, ...) 
    242243{ 
    243     int ret; OPEN(open, file, oflag); return ret; 
     244    int ret; OPEN(open); return ret; 
    244245} 
    245246 
    246247int open64(const char *file, int oflag, ...) 
    247248{ 
    248     int ret; OPEN(open64, file, oflag); return ret; 
     249    int ret; OPEN(open64); return ret; 
    249250} 
    250251 
     
    268269        files[fd].pos += ret; 
    269270    } 
     271 
     272    /* Sanity check */ 
     273    if((uint64_t)lseek64_orig(fd, 0, SEEK_CUR) != files[fd].pos) 
     274        fprintf(stderr, "ZZUF ERROR: OFFSET INCONSISTENCY\n"); 
     275 
     276    return ret; 
     277} 
     278 
     279#define LSEEK(fn, off_t) \ 
     280    do { \ 
     281        if(!_zzuf_ready) \ 
     282            LOADSYM(fn); \ 
     283        ret = ORIG(fn)(fd, offset, whence); \ 
     284        if(!_zzuf_ready) \ 
     285            return ret; \ 
     286        if(!files[fd].managed) \ 
     287            return ret; \ 
     288        debug(STR(fn)"(%i, %lli, %i) = %lli", \ 
     289              fd, (long long int)offset, whence, (long long int)ret); \ 
     290        if(ret != (off_t)-1) \ 
     291            files[fd].pos = (int64_t)ret; \ 
     292    } while(0) 
     293 
     294off_t lseek(int fd, off_t offset, int whence) 
     295{ 
     296    off_t ret; 
     297    LSEEK(lseek, off_t); 
    270298    return ret; 
    271299} 
     
    273301off64_t lseek64(int fd, off64_t offset, int whence) 
    274302{ 
    275     int ret; 
    276  
    277     if(!_zzuf_ready) 
    278         LOADSYM(lseek64); 
    279     ret = lseek64_orig(fd, offset, whence); 
    280     if(!_zzuf_ready) 
    281         return ret; 
    282  
    283     if(!files[fd].managed) 
    284         return ret; 
    285  
    286     debug("lseek64(%i, %lli, %i) = %i", fd, (long long int)offset, whence, ret); 
    287     if(ret != (off64_t)-1) 
    288         files[fd].pos = (int64_t)ret; 
    289  
     303    off64_t ret; 
     304    LSEEK(lseek64, off64_t); 
    290305    return ret; 
    291306} 
Note: See TracChangeset for help on using the changeset viewer.