Rizin
unix-like reverse engineering framework and cli tools
tryopen.c
Go to the documentation of this file.
1 /*
2  tryopen.c -- tool for tests that try opening zip archives
3  Copyright (C) 1999-2021 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 
39 #ifndef HAVE_GETOPT
40 #include "getopt.h"
41 #endif
42 
43 #include "zip.h"
44 #define TRYOPEN_USAGE \
45  "usage: %s [-cent] file\n\n" \
46  "\t-c\tcheck consistency\n" \
47  "\t-e\texclusively open archive\n" \
48  "\t-n\tcreate new file\n" \
49  "\t-t\ttruncate file to size 0\n"
50 
51 
52 int
53 main(int argc, char *argv[]) {
54  const char *fname;
55  zip_t *z;
56  int c, flags, ze;
58  int error;
59 
60  flags = 0;
61 
62  while ((c = getopt(argc, argv, "cent")) != -1) {
63  switch (c) {
64  case 'c':
66  break;
67  case 'e':
68  flags |= ZIP_EXCL;
69  break;
70  case 'n':
71  flags |= ZIP_CREATE;
72  break;
73  case 't':
75  break;
76 
77  default:
78  fprintf(stderr, TRYOPEN_USAGE, argv[0]);
79  return 1;
80  }
81  }
82 
83  error = 0;
84  for (; optind < argc; optind++) {
85  fname = argv[optind];
86  errno = 0;
87 
88  if ((z = zip_open(fname, flags, &ze)) != NULL) {
89  count = zip_get_num_entries(z, 0);
90  printf("opening '%s' succeeded, %" PRIu64 " entries\n", fname, count);
91  zip_close(z);
92  continue;
93  }
94 
95  printf("opening '%s' returned error %d", fname, ze);
96  switch (zip_error_get_sys_type(ze)) {
97  case ZIP_ET_SYS:
98  case ZIP_ET_LIBZIP:
99  printf("/%d", errno);
100  break;
101 
102  default:
103  break;
104  }
105  printf("\n");
106  error++;
107  }
108 
109  if (error > 0)
110  fprintf(stderr, "%d errors\n", error);
111 
112  return error ? 1 : 0;
113 }
int getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.h:20
int optind
Definition: getopt.h:6
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
#define PRIu64
Definition: macros.h:18
ZIP_EXTERN int zip_error_get_sys_type(int)
#define ZIP_EXCL
Definition: zip.h:68
#define ZIP_CHECKCONS
Definition: zip.h:69
#define ZIP_CREATE
Definition: zip.h:67
#define ZIP_ET_LIBZIP
Definition: zip.h:144
ZIP_EXTERN int zip_close(zip_t *_Nonnull)
Definition: zip_close.c:52
#define ZIP_ET_SYS
Definition: zip.h:142
#define ZIP_TRUNCATE
Definition: zip.h:70
ZIP_EXTERN zip_t *_Nullable zip_open(const char *_Nonnull, int, int *_Nullable)
Definition: zip_open.c:54
ZIP_EXTERN zip_int64_t zip_get_num_entries(zip_t *_Nonnull, zip_flags_t)
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define c(i)
Definition: sha256.c:43
Definition: zipint.h:278
int main(int argc, char *argv[])
Definition: tryopen.c:53
#define TRYOPEN_USAGE
Definition: tryopen.c:44
void error(const char *msg)
Definition: untgz.c:593
int64_t zip_int64_t
Definition: zipconf.h:38