Rizin
unix-like reverse engineering framework and cli tools
add_from_filep.c
Go to the documentation of this file.
1 /*
2  add_from_filep.c -- test case for adding file to archive
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 
35 #include <errno.h>
36 #include <stdio.h>
37 #include <string.h>
38 
39 #include "zip.h"
40 
41 static const char *prg;
42 
43 int
44 main(int argc, char *argv[]) {
45  const char *archive;
46  const char *file;
47  const char *name;
48  zip_t *za;
49  zip_source_t *zs;
50  int err;
51  FILE *fp;
52 
53  prg = argv[0];
54 
55  if (argc != 3) {
56  fprintf(stderr, "usage: %s archive file\n", prg);
57  return 1;
58  }
59 
60  archive = argv[1];
61  file = argv[2];
62 
63  if ((za = zip_open(archive, ZIP_CREATE, &err)) == NULL) {
66  fprintf(stderr, "%s: can't open zip archive '%s': %s\n", prg, archive, zip_error_strerror(&error));
68  return 1;
69  }
70 
71  if ((fp = fopen(file, "rb")) == NULL) {
72  fprintf(stderr, "%s: can't open input file '%s': %s\n", prg, file, strerror(errno));
73  return 1;
74  }
75 
76  if ((zs = zip_source_filep(za, fp, 0, -1)) == NULL) {
77  fprintf(stderr, "%s: error creating file source for '%s': %s\n", prg, file, zip_strerror(za));
78  return 1;
79  }
80 
81  if ((name = strrchr(file, '/')) == NULL)
82  name = file;
83 
84  if (zip_add(za, name, zs) == -1) {
85  zip_source_free(zs);
86  fprintf(stderr, "%s: can't add file '%s': %s\n", prg, file, zip_strerror(za));
87  return 1;
88  }
89 
90  if (zip_close(za) == -1) {
91  fprintf(stderr, "%s: can't close zip archive '%s': %s\n", prg, archive, zip_strerror(za));
92  return 1;
93  }
94 
95  return 0;
96 }
static const char * prg
int main(int argc, char *argv[])
static bool err
Definition: armass.c:435
#define NULL
Definition: cris-opc.c:27
ZIP_EXTERN void zip_error_init_with_code(zip_error_t *_Nonnull, int)
Definition: zip_error.c:66
ZIP_EXTERN zip_source_t *_Nullable zip_source_filep(zip_t *_Nonnull, FILE *_Nonnull, zip_uint64_t, zip_int64_t)
ZIP_EXTERN zip_int64_t zip_add(zip_t *_Nonnull, const char *_Nonnull, zip_source_t *_Nonnull)
Definition: zip_add.c:47
#define ZIP_CREATE
Definition: zip.h:67
ZIP_EXTERN int zip_close(zip_t *_Nonnull)
Definition: zip_close.c:52
ZIP_EXTERN void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52
ZIP_EXTERN void zip_source_free(zip_source_t *_Nullable)
ZIP_EXTERN zip_t *_Nullable zip_open(const char *_Nonnull, int, int *_Nullable)
Definition: zip_open.c:54
ZIP_EXTERN const char *_Nonnull zip_error_strerror(zip_error_t *_Nonnull)
ZIP_EXTERN const char *_Nonnull zip_strerror(zip_t *_Nonnull)
Definition: zip_strerror.c:39
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
string FILE
Definition: benchmark.py:21
const char * name
Definition: op.c:541
Definition: zipcmp.c:60
Definition: gzappend.c:170
Definition: z80asm.h:102
Definition: zip.h:284
Definition: zipint.h:278
void error(const char *msg)
Definition: untgz.c:593
static int file
Definition: z80asm.c:58
zip_t * za
Definition: ziptool.c:79