Back to index

php5  5.3.10
zip_error_strerror.c
Go to the documentation of this file.
00001 /*
00002   zip_error_sterror.c -- get string representation of struct zip_error
00003   Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
00004 
00005   This file is part of libzip, a library to manipulate ZIP archives.
00006   The authors can be contacted at <libzip@nih.at>
00007 
00008   Redistribution and use in source and binary forms, with or without
00009   modification, are permitted provided that the following conditions
00010   are met:
00011   1. Redistributions of source code must retain the above copyright
00012      notice, this list of conditions and the following disclaimer.
00013   2. Redistributions in binary form must reproduce the above copyright
00014      notice, this list of conditions and the following disclaimer in
00015      the documentation and/or other materials provided with the
00016      distribution.
00017   3. The names of the authors may not be used to endorse or promote
00018      products derived from this software without specific prior
00019      written permission.
00020  
00021   THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
00022   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
00025   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00027   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00029   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00030   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00031   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 */
00033 
00034 
00035 
00036 #include <errno.h>
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 
00041 #include "zipint.h"
00042 
00043 
00044 
00045 const char *
00046 _zip_error_strerror(struct zip_error *err)
00047 {
00048     const char *zs, *ss;
00049     char buf[128], *s;
00050 
00051     _zip_error_fini(err);
00052 
00053     if (err->zip_err < 0 || err->zip_err >= _zip_nerr_str) {
00054        sprintf(buf, "Unknown error %d", err->zip_err);
00055        zs = NULL;
00056        ss = buf;
00057     }
00058     else {
00059        zs = _zip_err_str[err->zip_err];
00060        
00061        switch (_zip_err_type[err->zip_err]) {
00062        case ZIP_ET_SYS:
00063            ss = strerror(err->sys_err);
00064            break;
00065 
00066        case ZIP_ET_ZLIB:
00067            ss = zError(err->sys_err);
00068            break;
00069 
00070        default:
00071            ss = NULL;
00072        }
00073     }
00074 
00075     if (ss == NULL)
00076        return zs;
00077     else {
00078        if ((s=(char *)malloc(strlen(ss)
00079                            + (zs ? strlen(zs)+2 : 0) + 1)) == NULL)
00080            return _zip_err_str[ZIP_ER_MEMORY];
00081        
00082        sprintf(s, "%s%s%s",
00083               (zs ? zs : ""),
00084               (zs ? ": " : ""),
00085               ss);
00086        err->str = s;
00087 
00088        return s;
00089     }
00090 }