LIST: Improve wording of error messages.
[pspp.git] / tests / libpspp / zip-test.c
blobeded13e8bf67dccefeab4a547776716c96807c13
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* A simple program to zip or unzip a file */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <string.h>
25 #include <stdio.h>
26 #include "libpspp/assertion.h"
27 #include <libpspp/compiler.h>
28 #include <libpspp/zip-writer.h>
29 #include <libpspp/zip-reader.h>
30 #include <libpspp/str.h>
32 #include <errno.h>
33 #include "xalloc.h"
35 /* Exit with a failure code.
36 (Place a breakpoint on this function while debugging.) */
37 static void
38 check_die (void)
40 exit (EXIT_FAILURE);
43 int
44 main (int argc, char **argv)
46 if ( argc < 4)
48 fprintf (stderr, "Usage zip-test: {r|w} archive file0 file1 ... filen\n");
49 check_die ();
52 if ( 0 == strcmp ("w", argv[1]))
54 int i;
55 struct zip_writer *zw = zip_writer_create (argv[2]);
56 for (i = 3; i < argc; ++i)
58 FILE *fp = fopen (argv[i], "r");
59 if (!fp ) check_die ();
60 zip_writer_add (zw, fp, argv[i]);
62 zip_writer_close (zw);
64 else if ( 0 == strcmp ("r", argv[1]))
66 const int BUFSIZE=256;
67 char buf[BUFSIZE];
68 int i;
69 struct string str;
70 struct zip_reader *zr = zip_reader_create (argv[2], &str);
71 if ( NULL == zr)
73 fprintf (stderr, "Could not create zip reader: %s\n", ds_cstr (&str));
74 check_die ();
76 for (i = 3; i < argc; ++i)
78 int x = 0;
79 struct zip_member *zm ;
80 FILE *fp = fopen (argv[i], "w");
81 if ( NULL == fp)
83 int e = errno;
84 fprintf (stderr, "Could not create file %s: %s\n", argv[i], strerror(e));
85 check_die ();
87 zm = zip_member_open (zr, argv[i]);
88 if ( NULL == zm)
90 fprintf (stderr, "Could not open zip member %s from archive: %s\n",
91 argv[i], ds_cstr (&str));
92 check_die ();
95 while ((x = zip_member_read (zm, buf, BUFSIZE)) > 0)
97 fwrite (buf, x, 1, fp);
99 fclose (fp);
100 if ( x < 0)
102 fprintf (stderr, "Unzip failed: %s\n", ds_cstr (&str));
103 check_die ();
106 zip_reader_destroy (zr);
108 else
109 exit (1);
111 return 0;