b41eea889cc223638befc7bff72bb846adf6e8d0
[platform/upstream/libzip.git] / regress / tryopen.c
1 /*
2   tryopen.c -- tool for tests that try opening zip archives
3   Copyright (C) 1999-2014 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 #include "config.h"
35
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42
43 #ifndef HAVE_GETOPT
44 #include "getopt.h"
45 #endif
46
47 #include "zip.h"
48 #include "compat.h"
49
50 const char *usage = "usage: %s [-cent] file\n\n"
51     "\t-c\tcheck consistency\n"
52     "\t-e\texclusively open archive\n"
53     "\t-n\tcreate new file\n"
54     "\t-t\ttruncate file to size 0\n";
55
56
57 int
58 main(int argc, char *argv[])
59 {
60     const char *fname;
61     zip_t *z;
62     int c, flags, ze;
63     zip_int64_t count;
64     int error;
65
66     flags = 0;
67
68     while ((c=getopt(argc, argv, "cent")) != -1) {
69         switch (c) {
70         case 'c':
71             flags |= ZIP_CHECKCONS;
72             break;
73         case 'e':
74             flags |= ZIP_EXCL;
75             break;
76         case 'n':
77             flags |= ZIP_CREATE;
78             break;
79         case 't':
80             flags |= ZIP_TRUNCATE;
81             break;
82
83         default:
84             fprintf(stderr, usage, argv[0]);
85             return 1;
86         }
87     }
88
89     error = 0;
90     for (; optind < argc; optind++) {
91         fname = argv[optind];
92         errno = 0;
93
94         if ((z=zip_open(fname, flags, &ze)) != NULL) {
95             count = zip_get_num_entries(z, 0);
96             printf("opening '%s' succeeded, %"PRIu64" entries\n", fname, count);
97             zip_close(z);
98             continue;
99         }
100         
101         printf("opening '%s' returned error %d", fname, ze);
102         if (zip_error_get_sys_type(ze) == ZIP_ET_SYS)
103             printf("/%d", errno);
104         printf("\n");
105         error++;
106     }
107
108     if (error > 0)
109         fprintf(stderr, "%d errors\n", error);
110
111     return error ? 1 : 0;
112 }