blob: 3cac11054367295bc3acbf9eb3bed1bbdf94bef1 [file] [log] [blame]
Dieter Baronbbb63691999-07-26 22:04:20 +00001/*
2 zip_name_locate.c -- get index by name
Dieter Baron88efa422011-02-08 22:04:11 +01003 Copyright (C) 1999-2011 Dieter Baron and Thomas Klausner
Dieter Baronbbb63691999-07-26 22:04:20 +00004
Dieter Barondd9afca2003-10-02 14:13:37 +00005 This file is part of libzip, a library to manipulate ZIP archives.
Dieter Baronb86c4332007-11-07 14:35:13 +01006 The authors can be contacted at <libzip@nih.at>
Dieter Baronbbb63691999-07-26 22:04:20 +00007
Dieter Barondd9afca2003-10-02 14:13:37 +00008 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in
15 the documentation and/or other materials provided with the
16 distribution.
17 3. The names of the authors may not be used to endorse or promote
18 products derived from this software without specific prior
19 written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Dieter Baronbbb63691999-07-26 22:04:20 +000032*/
33
34
35
Dieter Baron2d6db591999-07-25 18:26:20 +000036#include <string.h>
Dieter Baronc0866112012-08-08 16:54:19 +020037#ifdef HAVE_STRINGS_H
38#include <strings.h>
39#endif
Dieter Baron2d6db591999-07-25 18:26:20 +000040
Dieter Baron2d6db591999-07-25 18:26:20 +000041#include "zipint.h"
42
43
44
Dieter Baronabc6fd72012-07-22 15:49:45 +020045ZIP_EXTERN zip_int64_t
46zip_name_locate(struct zip *za, const char *fname, zip_flags_t flags)
Dieter Baron2d6db591999-07-25 18:26:20 +000047{
Thomas Klausner290861a2004-11-30 23:02:48 +000048 return _zip_name_locate(za, fname, flags, &za->error);
Thomas Klausnercdbc8d52004-11-30 22:19:38 +000049}
50
51
52
Dieter Baronabc6fd72012-07-22 15:49:45 +020053zip_int64_t
54_zip_name_locate(struct zip *za, const char *fname, zip_flags_t flags, struct zip_error *error)
Thomas Klausnercdbc8d52004-11-30 22:19:38 +000055{
56 int (*cmp)(const char *, const char *);
Thomas Klausner1e194f32005-01-11 18:52:42 +000057 const char *fn, *p;
Dieter Baron0e5eeab2012-04-24 18:47:12 +020058 zip_uint64_t i;
Thomas Klausnercdbc8d52004-11-30 22:19:38 +000059
Thomas Klausner687bf522011-01-03 19:49:16 +010060 if (za == NULL)
61 return -1;
62
Thomas Klausner290861a2004-11-30 23:02:48 +000063 if (fname == NULL) {
64 _zip_error_set(error, ZIP_ER_INVAL, 0);
65 return -1;
66 }
Dieter Baron88efa422011-02-08 22:04:11 +010067
Dieter Baron99d0b042004-05-16 00:50:25 +000068 cmp = (flags & ZIP_FL_NOCASE) ? strcasecmp : strcmp;
Dieter Baronb2ed74d2004-04-14 14:01:31 +000069
Dieter Baron0e5eeab2012-04-24 18:47:12 +020070 for (i=0; i<za->nentry; i++) {
Thomas Klausner76939b32012-02-20 01:35:20 +010071 fn = _zip_get_name(za, i, flags, error);
Thomas Klausnercdbc8d52004-11-30 22:19:38 +000072
Thomas Klausnerdc3b5bc2012-02-18 00:10:34 +010073 /* newly added (partially filled) entry or error */
Thomas Klausnercdbc8d52004-11-30 22:19:38 +000074 if (fn == NULL)
75 continue;
Dieter Barond36af892004-04-16 09:39:32 +000076
Dieter Baronb2ed74d2004-04-14 14:01:31 +000077 if (flags & ZIP_FL_NODIR) {
Dieter Barond36af892004-04-16 09:39:32 +000078 p = strrchr(fn, '/');
Thomas Klausner1e194f32005-01-11 18:52:42 +000079 if (p)
80 fn = p+1;
Dieter Barone3f91ef2003-10-06 02:50:14 +000081 }
Dieter Barone3f91ef2003-10-06 02:50:14 +000082
Thomas Klausner76939b32012-02-20 01:35:20 +010083 if (cmp(fname, fn) == 0) {
84 _zip_error_clear(error);
Dieter Baronabc6fd72012-07-22 15:49:45 +020085 return (zip_int64_t)i;
Thomas Klausner76939b32012-02-20 01:35:20 +010086 }
Dieter Barone3f91ef2003-10-06 02:50:14 +000087 }
88
Thomas Klausner290861a2004-11-30 23:02:48 +000089 _zip_error_set(error, ZIP_ER_NOENT, 0);
Dieter Baron2d6db591999-07-25 18:26:20 +000090 return -1;
91}