/src/zziplib/zzip/err.c
C | 168 lines | 110 code | 17 blank | 41 comment | 13 complexity | 5c970cc48d1b72496fc31ad212aaa386 MD5 | raw file
1 2/* 3 * Author: 4 * Guido Draheim <guidod@gmx.de> 5 * Tomi Ollila <Tomi.Ollila@iki.fi> 6 * 7 * Copyright (c) 1999,2000,2001,2002,2003 Guido Draheim 8 * All rights reserved, 9 * use under the restrictions of the 10 * Lesser GNU General Public License 11 * or alternatively the restrictions 12 * of the Mozilla Public License 1.1 13 */ 14 15#include <zzip/lib.h> /* exported... */ 16#include <zlib.h> 17 18#include <string.h> 19#include <errno.h> 20 21#include <zzip/file.h> 22 23/* *INDENT-OFF* */ 24static struct errlistentry { int code; const char* mesg; } 25errlist[] = 26{ 27 { ZZIP_NO_ERROR, "No error" }, 28 { ZZIP_OUTOFMEM, 29 "could not get temporary memory for internal structures" }, 30 { ZZIP_DIR_OPEN, "Failed to open zip-file %s" }, 31 { ZZIP_DIR_STAT, "Failed to fstat zip-file %s" }, 32 { ZZIP_DIR_SEEK, "Failed to lseek zip-file %s" }, 33 { ZZIP_DIR_READ, "Failed to read zip-file %s"}, 34 { ZZIP_DIR_TOO_SHORT, "zip-file %s too short" }, 35 { ZZIP_DIR_EDH_MISSING, "zip-file central directory not found" }, 36 { ZZIP_DIRSIZE, "Directory size too big..." }, 37 { ZZIP_ENOENT, "No such file found in zip-file %s" }, 38 { ZZIP_UNSUPP_COMPR, "Unsupported compression format" }, 39 { ZZIP_CORRUPTED, "Zipfile corrupted" }, 40 { ZZIP_UNDEF, "Some undefined error occurred" }, 41 { ZZIP_DIR_LARGEFILE, "Directory is largefile variant" }, 42 { 0, 0 }, 43}; 44/* *INDENT-ON* */ 45 46 47#define errlistSIZE (sizeof(errlist)/sizeof(*errlist)) 48 49/** 50 * returns the static string for the given error code. The 51 * error code can be either a normal system error (a 52 * positive error code will flag this), it can be => libz 53 * error code (a small negative error code will flag this) 54 * or it can be an error code from => libzzip, which is an 55 * negative value lower than => ZZIP_ERROR 56 */ 57zzip_char_t * 58zzip_strerror(int errcode) 59{ 60 if (errcode < ZZIP_ERROR && errcode > ZZIP_ERROR - 32) 61 { 62 struct errlistentry *err = errlist; 63 64 for (; err->mesg; err++) 65 { 66 if (err->code == errcode) 67 return err->mesg; 68 } 69 errcode = EINVAL; 70 } 71 72 if (errcode < 0) 73 { 74 if (errcode == -1) 75 return strerror(errcode); 76 else 77 return zError(errcode); 78 } 79 80 return strerror(errcode); 81} 82 83/** => zzip_strerror 84 * This function fetches the errorcode from the => DIR-handle and 85 * runs it through => zzip_strerror to obtain the static string 86 * describing the error. 87 */ 88zzip_char_t * 89zzip_strerror_of(ZZIP_DIR * dir) 90{ 91 if (! dir) 92 return strerror(errno); 93 return zzip_strerror(dir->errcode); 94} 95 96/* *INDENT-OFF* */ 97static struct errnolistentry { int code; int e_no; } 98errnolist[] = 99{ 100 { Z_STREAM_ERROR, EPIPE }, 101 { Z_DATA_ERROR, ESPIPE }, 102 { Z_MEM_ERROR, ENOMEM }, 103 { Z_BUF_ERROR, EMFILE }, 104 { Z_VERSION_ERROR, ENOEXEC }, 105 106 { ZZIP_DIR_OPEN, ENOTDIR }, 107 { ZZIP_DIR_STAT, EREMOTE }, 108 { ZZIP_DIR_SEEK, ESPIPE }, 109# ifdef ESTRPIPE 110 { ZZIP_DIR_READ, ESTRPIPE}, 111# else 112 { ZZIP_DIR_READ, EPIPE}, 113# endif 114 { ZZIP_DIR_TOO_SHORT, ENOEXEC }, 115# ifdef ENOMEDIUM 116 { ZZIP_DIR_EDH_MISSING, ENOMEDIUM }, 117# else 118 { ZZIP_DIR_EDH_MISSING, EIO }, 119# endif 120 { ZZIP_DIRSIZE, EFBIG }, 121 { ZZIP_OUTOFMEM, ENOMEM }, 122 { ZZIP_ENOENT, ENOENT }, 123# ifdef EPFNOSUPPORT 124 { ZZIP_UNSUPP_COMPR, EPFNOSUPPORT }, 125# else 126 { ZZIP_UNSUPP_COMPR, EACCES }, 127# endif 128# ifdef EILSEQ 129 { ZZIP_CORRUPTED, EILSEQ }, 130# else 131 { ZZIP_CORRUPTED, ELOOP }, 132# endif 133 { ZZIP_UNDEF, EINVAL }, 134 { 0, 0 }, 135}; 136/* *INDENT-ON* */ 137 138/** 139 * map the error code to a system error code. This is used 140 * for the drop-in replacement functions to return a value 141 * that can be interpreted correctly by code sections that 142 * are unaware of the fact they their => open(2) call had been 143 * diverted to a file inside a zip-archive. 144 */ 145int 146zzip_errno(int errcode) 147{ 148 if (errcode >= -1) 149 { 150 return errno; 151 } else 152 { 153 struct errnolistentry *err = errnolist; 154 155 for (; err->code; err++) 156 { 157 if (err->code == errcode) 158 return err->e_no; 159 } 160 return EINVAL; 161 } 162} 163 164/* 165 * Local variables: 166 * c-file-style: "stroustrup" 167 * End: 168 */