X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tests%2Flibpspp%2Fzip-test.c;h=3556926d6c2c01b77c078eb0d6a8fcd4c406df80;hb=209f7e5ccf28c97641b9e69ef6b9df81efe4dc38;hp=7a2fcb10c47db74258d7dc3996809a10435f8b13;hpb=551202adbc334f9c33f8290a4e407b422651bbd5;p=pspp diff --git a/tests/libpspp/zip-test.c b/tests/libpspp/zip-test.c index 7a2fcb10c4..3556926d6c 100644 --- a/tests/libpspp/zip-test.c +++ b/tests/libpspp/zip-test.c @@ -29,7 +29,7 @@ #include #include -#include "xalloc.h" +#include /* Exit with a failure code. (Place a breakpoint on this function while debugging.) */ @@ -42,53 +42,74 @@ check_die (void) int main (int argc, char **argv) { - if ( argc < 4) + if (argc < 4) { fprintf (stderr, "Usage zip-test: {r|w} archive file0 file1 ... filen\n"); check_die (); } - if ( 0 == strcmp ("w", argv[1])) + if (0 == strcmp ("w", argv[1])) { int i; struct zip_writer *zw = zip_writer_create (argv[2]); for (i = 3; i < argc; ++i) { - FILE *fp = fopen (argv[i], "r"); - if (!fp ) check_die (); + FILE *fp = fopen (argv[i], "rb"); + if (!fp) check_die (); zip_writer_add (zw, fp, argv[i]); } zip_writer_close (zw); } - else if ( 0 == strcmp ("r", argv[1])) + else if (0 == strcmp ("r", argv[1])) { const int BUFSIZE=256; char buf[BUFSIZE]; int i; - struct string str; - struct zip_reader *zr = zip_reader_create (argv[2], &str); + struct zip_reader *zr; + char *error = zip_reader_create (argv[2], &zr); + if (error) + { + fprintf (stderr, "Could not create zip reader: %s\n", error); + check_die (); + } for (i = 3; i < argc; ++i) { int x = 0; - struct zip_member *zm = zip_member_open (zr, argv[i]); - FILE *fp = fopen (argv[i], "w"); + FILE *fp = fopen (argv[i], "wb"); + if (NULL == fp) + { + int e = errno; + fprintf (stderr, "Could not create file %s: %s\n", argv[i], strerror(e)); + check_die (); + } + + struct zip_member *zm ; + char *error = zip_member_open (zr, argv[i], &zm); + if (error) + { + fprintf (stderr, "Could not open zip member %s from archive: %s\n", + argv[i], error); + check_die (); + } while ((x = zip_member_read (zm, buf, BUFSIZE)) > 0) { fwrite (buf, x, 1, fp); } + error = zip_member_steal_error (zm); + zip_member_finish (zm); fclose (fp); - if ( x < 0) + + assert ((error != NULL) == (x < 0)); + if (x < 0) { - fprintf (stderr, "Unzip failed: %s", ds_cstr (&str)); + fprintf (stderr, "Unzip failed: %s\n", error); check_die (); } - - zip_member_unref (zm); } - zip_reader_destroy (zr); + zip_reader_unref (zr); } - else + else exit (1); return 0;