blob: 7d44fe4c51229b716b4382c158e1d0881fce9fa1 [file] [log] [blame]
Thomas Klausner44c40132011-03-16 12:19:18 +01001/*
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 Baronb5a0e3f2012-08-31 18:18:36 +020035#include "config.h"
Thomas Klausner44c40132011-03-16 12:19:18 +010036
37#include <errno.h>
38#include <stdio.h>
39#include <stdlib.h>
Dieter Baronb5a0e3f2012-08-31 18:18:36 +020040#ifdef HAVE_UNISTD_H
41#include <unistd.h>
42#endif
43
Thomas Klausner44c40132011-03-16 12:19:18 +010044#ifndef HAVE_GETOPT
45#include "getopt.h"
46#endif
47
48#include "zip.h"
Dieter Baronb5a0e3f2012-08-31 18:18:36 +020049#include "compat.h"
Thomas Klausner44c40132011-03-16 12:19:18 +010050
Thomas Klausner79d02752012-10-12 22:54:28 +020051const 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 Klausner44c40132011-03-16 12:19:18 +010056
57
58
59int
60main(int argc, char *argv[])
61{
62 const char *fname;
63 struct zip *z;
Thomas Klausnerf7751172012-02-15 15:51:28 +010064 int c, flags, ze;
65 zip_uint64_t count;
Thomas Klausner48d7e2e2012-04-24 15:53:36 +020066 int error;
Thomas Klausner44c40132011-03-16 12:19:18 +010067
68 flags = 0;
Thomas Klausner44c40132011-03-16 12:19:18 +010069
Thomas Klausnera0259de2012-02-15 15:28:38 +010070 while ((c=getopt(argc, argv, "cent")) != -1) {
Thomas Klausner44c40132011-03-16 12:19:18 +010071 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 Klausnera0259de2012-02-15 15:28:38 +010081 case 't':
82 flags |= ZIP_TRUNCATE;
83 break;
Thomas Klausner44c40132011-03-16 12:19:18 +010084
85 default:
Thomas Klausner48d7e2e2012-04-24 15:53:36 +020086 fprintf(stderr, usage, argv[0]);
Thomas Klausner44c40132011-03-16 12:19:18 +010087 return 1;
88 }
89 }
Thomas Klausner48d7e2e2012-04-24 15:53:36 +020090
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 Klausnerc74d2672012-08-01 16:31:48 +020098 printf("opening `%s' succeeded, %"PRIu64" entries\n", fname, count);
Thomas Klausner48d7e2e2012-04-24 15:53:36 +020099 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 Klausner44c40132011-03-16 12:19:18 +0100108 }
109
Thomas Klausner48d7e2e2012-04-24 15:53:36 +0200110 if (error > 0)
111 fprintf(stderr, "%d errors\n", error);
Thomas Klausner44c40132011-03-16 12:19:18 +0100112
Thomas Klausner48d7e2e2012-04-24 15:53:36 +0200113 return error ? 1 : 0;
Thomas Klausner44c40132011-03-16 12:19:18 +0100114}