Avoid deprecated Perl syntax.
[pspp] / tests / libpspp / zip-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17
18 /* A simple program to zip or unzip a file */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
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>
31
32 #include "xalloc.h"
33 \f
34 /* Exit with a failure code.
35    (Place a breakpoint on this function while debugging.) */
36 static void
37 check_die (void)
38 {
39   exit (EXIT_FAILURE);
40 }
41
42 int
43 main (int argc, char **argv)
44 {
45   if ( argc < 4)
46     {
47       fprintf (stderr, "Usage zip-test: {r|w} archive file0 file1 ... filen\n");
48       check_die ();
49     }
50
51   if ( 0 == strcmp ("w", argv[1]))
52     {
53       int i;
54       struct zip_writer *zw = zip_writer_create (argv[2]);
55       for (i = 3; i < argc; ++i)
56         {
57           FILE *fp = fopen (argv[i], "r");
58           if (!fp ) check_die ();
59           zip_writer_add (zw, fp, argv[i]);
60         }
61       zip_writer_close (zw);
62     }
63   else if ( 0  == strcmp ("r", argv[1]))
64     {
65       const int BUFSIZE=256;
66       char buf[BUFSIZE];
67       int i;
68       struct string str;
69       struct zip_reader *zr = zip_reader_create (argv[2], &str);
70       for (i = 3; i < argc; ++i)
71         {
72           int x = 0;
73           struct zip_member *zm = zip_member_open (zr, argv[i]);
74           FILE *fp = fopen (argv[i], "w");
75
76           while ((x = zip_member_read (zm, buf, BUFSIZE)) > 0)
77             {
78               fwrite (buf, x, 1, fp);
79             }
80           fclose (fp);
81           if ( x < 0)
82             {
83               fprintf (stderr, "Unzip failed: %s", ds_cstr (&str));
84               check_die ();
85             }
86           
87           zip_member_unref (zm);
88         }
89       zip_reader_destroy (zr);
90     }
91   else 
92     exit (1);
93
94   return 0;
95 }