/* * Copyright 2003,2004,2005 Kevin Smathers, All Rights Reserved * * This file may be used or modified without the need for a license. * * Redistribution of this file in either its original form, or in an * updated form may be done under the terms of the GNU LIBRARY GENERAL * PUBLIC LICENSE. If this license is unacceptable to you then you * may not redistribute this work. * * See the file COPYING.LGPL for details. */ #include "config.h" #include #include #include #if !WIN32 # if HAVE_UNISTD_H # include # endif # if HAVE_FCNTL_H # include # endif # include #endif #include #include #if WIN32 # include # include # define MAXPATHLEN 255 # define MKDIR(p,f) mkdir(p) #else # define MKDIR(p,f) mkdir(p,f) #endif #include "bterror.h" #include "util.h" #undef malloc #undef calloc #undef realloc #undef free #ifdef WITH_DMALLOC #include #endif /* WITH_DMALLOC */ /* Globals */ #define FILESETSIZE 50 static struct { int fd; int options; char *path; } c_fileset[ FILESETSIZE ]; static int c_index = 0; void *btcalloc( size_t nmemb, size_t size) { void *region = calloc( nmemb, size); if (!region) SYSDIE("calloc"); return region; } void *btmalloc( size_t size) { void *region = malloc( size); if (!region) SYSDIE("malloc"); return region; } void *btrealloc( void *ptr, size_t size) { void *region = realloc( ptr, size); if (!region) SYSDIE("realloc"); return region; } void btfree( void *ptr) { free( ptr); } char *bts_strerror( int err) { if (err) { if (err >= BTERR_BASE && err < BTERR_LAST) { return bterror_string[err - BTERR_BASE]; } else { return strerror(err); } } return "Not an error"; } void bts_perror( int err, char *msg) { fprintf( stderr, "%s: [%d] %s\n", msg, err, bts_strerror(err)); } int die( char *file, int line, char *msg, int err) { if (err) { fprintf( stderr, "%s+%d: (FATAL ERROR) %s ([%d] %s)\n", file, line, msg, err, bts_strerror( err)); } else { fprintf( stderr, "%s+%d: (FATAL ERROR) %s\n", file, line, msg); } abort(); } int cacheopen( char *path, int options, int mode) { int i; int fd; int err; static int last_i = 0; if (c_fileset[last_i].path == path && c_fileset[last_i].options == options) { return c_fileset[last_i].fd; } for (i=0; i 0) { err = close(c_fileset[c_index].fd); if (err) { bts_perror( errno, "Error closing file"); fprintf( stderr, "File '%s' may be corrupt\n", c_fileset[c_index].path); } } c_fileset[c_index].fd = fd; c_fileset[c_index].path = path; c_fileset[c_index].options = options; c_index = (c_index + 1) % FILESETSIZE; return fd; } void cacheclose( void) { int err; int i; for (i=0; i= EXITMAX) return 1; if (__exitindex == 0) { atexit( on_exit_exit); } __exitlist[ __exitindex].exitfn = exitfn; __exitlist[ __exitindex].data = data; __exitindex++; return 0; } #endif