Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | tryopen.c -- tool for tests that try opening zip archives |
| 3 | Copyright (C) 1999-2011 Dieter Baron and Thomas Klausner |
| 4 | |
| 5 | This file is part of libzip, a library to manipulate ZIP archives. |
| 6 | The authors can be contacted at <libzip@nih.at> |
| 7 | |
| 8 | 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. |
| 32 | */ |
| 33 | |
| 34 | |
Dieter Baron | b5a0e3f | 2012-08-31 18:18:36 +0200 | [diff] [blame] | 35 | #include "config.h" |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 36 | |
| 37 | #include <errno.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
Dieter Baron | b5a0e3f | 2012-08-31 18:18:36 +0200 | [diff] [blame] | 40 | #ifdef HAVE_UNISTD_H |
| 41 | #include <unistd.h> |
| 42 | #endif |
| 43 | |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 44 | #ifndef HAVE_GETOPT |
| 45 | #include "getopt.h" |
| 46 | #endif |
| 47 | |
| 48 | #include "zip.h" |
Dieter Baron | b5a0e3f | 2012-08-31 18:18:36 +0200 | [diff] [blame] | 49 | #include "compat.h" |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 50 | |
Thomas Klausner | 79d0275 | 2012-10-12 22:54:28 +0200 | [diff] [blame] | 51 | const char *usage = "usage: %s [-cent] file\n\n" |
| 52 | "\t-c\tcheck consistency\n" |
| 53 | "\t-e\texclusively open archive\n" |
| 54 | "\t-n\tcreate new file\n" |
| 55 | "\t-t\ttruncate file to size 0\n"; |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 56 | |
| 57 | |
| 58 | |
| 59 | int |
| 60 | main(int argc, char *argv[]) |
| 61 | { |
| 62 | const char *fname; |
| 63 | struct zip *z; |
Thomas Klausner | f775117 | 2012-02-15 15:51:28 +0100 | [diff] [blame] | 64 | int c, flags, ze; |
| 65 | zip_uint64_t count; |
Thomas Klausner | 48d7e2e | 2012-04-24 15:53:36 +0200 | [diff] [blame] | 66 | int error; |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 67 | |
| 68 | flags = 0; |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 69 | |
Thomas Klausner | a0259de | 2012-02-15 15:28:38 +0100 | [diff] [blame] | 70 | while ((c=getopt(argc, argv, "cent")) != -1) { |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 71 | switch (c) { |
| 72 | case 'c': |
| 73 | flags |= ZIP_CHECKCONS; |
| 74 | break; |
| 75 | case 'e': |
| 76 | flags |= ZIP_EXCL; |
| 77 | break; |
| 78 | case 'n': |
| 79 | flags |= ZIP_CREATE; |
| 80 | break; |
Thomas Klausner | a0259de | 2012-02-15 15:28:38 +0100 | [diff] [blame] | 81 | case 't': |
| 82 | flags |= ZIP_TRUNCATE; |
| 83 | break; |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 84 | |
| 85 | default: |
Thomas Klausner | 48d7e2e | 2012-04-24 15:53:36 +0200 | [diff] [blame] | 86 | fprintf(stderr, usage, argv[0]); |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 87 | return 1; |
| 88 | } |
| 89 | } |
Thomas Klausner | 48d7e2e | 2012-04-24 15:53:36 +0200 | [diff] [blame] | 90 | |
| 91 | error = 0; |
| 92 | for (; optind < argc; optind++) { |
| 93 | fname = argv[optind]; |
| 94 | errno = 0; |
| 95 | |
| 96 | if ((z=zip_open(fname, flags, &ze)) != NULL) { |
| 97 | count = zip_get_num_entries(z, 0); |
Thomas Klausner | c74d267 | 2012-08-01 16:31:48 +0200 | [diff] [blame] | 98 | printf("opening `%s' succeeded, %"PRIu64" entries\n", fname, count); |
Thomas Klausner | 48d7e2e | 2012-04-24 15:53:36 +0200 | [diff] [blame] | 99 | zip_close(z); |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | printf("opening `%s' returned error %d", fname, ze); |
| 104 | if (zip_error_get_sys_type(ze) == ZIP_ET_SYS) |
| 105 | printf("/%d", errno); |
| 106 | printf("\n"); |
| 107 | error++; |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Thomas Klausner | 48d7e2e | 2012-04-24 15:53:36 +0200 | [diff] [blame] | 110 | if (error > 0) |
| 111 | fprintf(stderr, "%d errors\n", error); |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 112 | |
Thomas Klausner | 48d7e2e | 2012-04-24 15:53:36 +0200 | [diff] [blame] | 113 | return error ? 1 : 0; |
Thomas Klausner | 44c4013 | 2011-03-16 12:19:18 +0100 | [diff] [blame] | 114 | } |