blob: 90c56a1f027a1cd2408e4b7b7f97145b428e5679 [file] [log] [blame]
Dieter Baron5149dfb2003-10-05 16:05:25 +00001/*
Thomas Klausnerd0147e92004-11-17 21:55:17 +00002 $NiH: zip_error_strerror.c,v 1.2 2003/10/06 02:50:06 dillo Exp $
Dieter Baron5149dfb2003-10-05 16:05:25 +00003
4 zip_error_sterror.c -- get string representation of struct zip_error
5 Copyright (C) 1999, 2003 Dieter Baron and Thomas Klausner
6
7 This file is part of libzip, a library to manipulate ZIP archives.
8 The authors can be contacted at <nih@giga.or.at>
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13 1. Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15 2. Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in
17 the documentation and/or other materials provided with the
18 distribution.
19 3. The names of the authors may not be used to endorse or promote
20 products derived from this software without specific prior
21 written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
24 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
27 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36
37
38#include <errno.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42
43#include "zip.h"
44#include "zipint.h"
45
46
47
48const char *
49_zip_error_strerror(struct zip_error *err)
50{
Dieter Barone3f91ef2003-10-06 02:50:14 +000051 const char *zs, *ss;
52 char buf[128], *s;
53
Dieter Baron5149dfb2003-10-05 16:05:25 +000054 _zip_error_fini(err);
55
Dieter Barone3f91ef2003-10-06 02:50:14 +000056 if (err->zip_err < 0 || err->zip_err >= _zip_nerr_str) {
57 sprintf(buf, "Unknown error %d", err->zip_err);
58 zs = NULL;
59 ss = buf;
60 }
61 else {
62 zs = _zip_err_str[err->zip_err];
63
64 switch (_zip_err_type[err->zip_err]) {
65 case ZIP_ET_SYS:
66 ss = strerror(err->sys_err);
67 break;
68
Thomas Klausnerd0147e92004-11-17 21:55:17 +000069 case ZIP_ET_ZLIB:
Dieter Barone3f91ef2003-10-06 02:50:14 +000070 ss = zError(err->sys_err);
71 break;
72
73 default:
74 ss = NULL;
75 }
Dieter Baron5149dfb2003-10-05 16:05:25 +000076 }
77
Dieter Barone3f91ef2003-10-06 02:50:14 +000078 if (ss == NULL)
79 return zs;
80 else {
81 if ((s=malloc(strlen(ss) + (zs ? strlen(zs)+2 : 0) + 1)) == NULL)
Thomas Klausnerd0147e92004-11-17 21:55:17 +000082 return _zip_err_str[ZIP_ER_MEMORY];
Dieter Barone3f91ef2003-10-06 02:50:14 +000083
84 sprintf(s, "%s%s%s",
85 (zs ? zs : ""),
86 (zs ? ": " : ""),
87 ss);
88 err->str = s;
Dieter Baron5149dfb2003-10-05 16:05:25 +000089
Dieter Barone3f91ef2003-10-06 02:50:14 +000090 return ss;
Dieter Baron5149dfb2003-10-05 16:05:25 +000091 }
92}