Changeset 1478


Ignore:
Timestamp:
Dec 15, 2006, 11:06:30 AM (14 years ago)
Author:
Sam Hocevar
Message:
  • Divert close, fclose, lseek64.
Location:
zzuf/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/TODO

    r1470 r1478  
    1   * Only very basic support for [f]open/[f]read, no seek, no close, etc.
     1  * Parallelise operations.
     2  * Only very basic support for [f]open/[f]read, etc. see fscanf, fgets...
  • zzuf/trunk/src/preload.c

    r1477 r1478  
    1818
    1919#include "config.h"
     20
     21/* Can't remember what that's for */
    2022#define _GNU_SOURCE
     23/* Use this to get lseek64() */
     24#define _LARGEFILE64_SOURCE
    2125
    2226#if defined HAVE_STDINT_H
     
    2630#endif
    2731#include <stdio.h>
     32#include <sys/types.h>
    2833#include <unistd.h>
    2934#include <stdlib.h>
     
    4651static size_t  (*fread_orig)   (void *ptr, size_t size, size_t nmemb,
    4752                                FILE *stream);
     53static int     (*fclose_orig)  (FILE *fp);
     54
    4855static int     (*open_orig)    (const char *file, int oflag, ...);
    4956static int     (*open64_orig)  (const char *file, int oflag, ...);
    5057static ssize_t (*read_orig)    (int fd, void *buf, size_t count);
     58static off64_t (*lseek64_orig) (int fd, off64_t offset, int whence);
     59static int     (*close_orig)   (int fd);
    5160
    5261#define STR(x) #x
     
    6675    LOADSYM(fseek);
    6776    LOADSYM(fread);
     77    LOADSYM(fclose);
     78
    6879    LOADSYM(open);
    6980    LOADSYM(open64);
    7081    LOADSYM(read);
     82    LOADSYM(lseek64);
     83    LOADSYM(close);
    7184
    7285    debug("libzzuf initialised");
     
    7689
    7790/* Our function wrappers */
    78 #define FOPEN(ret, fn, path, mode) \
     91#define FOPEN(fn, path, mode) \
    7992    do \
    8093    { \
     
    104117FILE *fopen(const char *path, const char *mode)
    105118{
    106     FILE *f; FOPEN(f, fopen, path, mode); return f;
     119    FILE *ret; FOPEN(fopen, path, mode); return ret;
    107120}
    108121
    109122FILE *fopen64(const char *path, const char *mode)
    110123{
    111     FILE *f; FOPEN(f, fopen64, path, mode); return f;
     124    FILE *ret; FOPEN(fopen64, path, mode); return ret;
    112125}
    113126
     
    164177}
    165178
    166 #define OPEN(ret, fn, file, oflag) \
     179int fclose(FILE *fp)
     180{
     181    int ret, fd;
     182
     183    if(!_zzuf_ready)
     184        LOADSYM(fclose);
     185    fd = fileno(fp);
     186    ret = fclose_orig(fp);
     187    if(!_zzuf_ready)
     188        return ret;
     189
     190    if(!files[fd].managed)
     191        return ret;
     192
     193    debug("fclose(%p) = %i", fp, ret);
     194    files[fd].managed = 0;
     195
     196    return ret;
     197}
     198
     199#define OPEN(fn, file, oflag) \
    167200    do \
    168201    { \
     
    208241int open(const char *file, int oflag, ...)
    209242{
    210     int ret; OPEN(ret, open, file, oflag); return ret;
     243    int ret; OPEN(open, file, oflag); return ret;
    211244}
    212245
    213246int open64(const char *file, int oflag, ...)
    214247{
    215     int ret; OPEN(ret, open64, file, oflag); return ret;
     248    int ret; OPEN(open64, file, oflag); return ret;
    216249}
    217250
     
    221254
    222255    if(!_zzuf_ready)
    223         LOADSYM(fread);
     256        LOADSYM(read);
    224257    ret = read_orig(fd, buf, count);
    225258    if(!_zzuf_ready)
     
    238271}
    239272
     273off64_t lseek64(int fd, off64_t offset, int whence)
     274{
     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    {
     289        switch(whence)
     290        {
     291            case SEEK_SET: files[fd].pos = (int64_t)offset; break;
     292            case SEEK_CUR: files[fd].pos += (int64_t)offset; break;
     293            /* FIXME */
     294            //case SEEK_END: files[fd].pos = ftell(stream); break;
     295        }
     296    }
     297    return ret;
     298}
     299
     300int close(int fd)
     301{
     302    int ret;
     303
     304    if(!_zzuf_ready)
     305        LOADSYM(close);
     306    ret = close_orig(fd);
     307    if(!_zzuf_ready)
     308        return ret;
     309
     310    if(!files[fd].managed)
     311        return ret;
     312
     313    debug("close(%i) = %i", fd, ret);
     314    files[fd].managed = 0;
     315
     316    return ret;
     317}
     318
Note: See TracChangeset for help on using the changeset viewer.