| 1 |
/* |
| 2 |
* Copyright (c) 2003 Guido Draheim <[email protected]> |
| 3 |
* Use freely under the restrictions of the ZLIB license. |
| 4 |
* |
| 5 |
* This file is used as an example to clarify zzip api usage. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <zzip/lib.h> |
| 9 |
#include <zzip/__fnmatch.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include "unzzipdir-zip.h" |
| 14 |
#include "unzzip-states.h" |
| 15 |
|
| 16 |
#ifdef ZZIP_HAVE_UNISTD_H |
| 17 |
#include <unistd.h> |
| 18 |
#endif |
| 19 |
#ifdef ZZIP_HAVE_IO_H |
| 20 |
#include <io.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
static const char* comprlevel[] = { |
| 24 |
"stored", "shrunk", "redu:1", "redu:2", "redu:3", "redu:4", |
| 25 |
"impl:N", "toknze", "defl:N", "defl:B", "impl:B" }; |
| 26 |
|
| 27 |
static int exitcode(int e) |
| 28 |
{ |
| 29 |
switch (e) |
| 30 |
{ |
| 31 |
case ZZIP_NO_ERROR: |
| 32 |
return EXIT_OK; |
| 33 |
case ZZIP_OUTOFMEM: /* out of memory */ |
| 34 |
return EXIT_ENOMEM; |
| 35 |
case ZZIP_DIR_OPEN: /* failed to open zipfile, see errno for details */ |
| 36 |
return EXIT_ZIP_NOT_FOUND; |
| 37 |
case ZZIP_DIR_STAT: /* failed to fstat zipfile, see errno for details */ |
| 38 |
case ZZIP_DIR_SEEK: /* failed to lseek zipfile, see errno for details */ |
| 39 |
case ZZIP_DIR_READ: /* failed to read zipfile, see errno for details */ |
| 40 |
case ZZIP_DIR_TOO_SHORT: |
| 41 |
case ZZIP_DIR_EDH_MISSING: |
| 42 |
return EXIT_FILEFORMAT; |
| 43 |
case ZZIP_DIRSIZE: |
| 44 |
return EXIT_EARLY_END_OF_FILE; |
| 45 |
case ZZIP_ENOENT: |
| 46 |
return EXIT_FILE_NOT_FOUND; |
| 47 |
case ZZIP_UNSUPP_COMPR: |
| 48 |
return EXIT_UNSUPPORTED_COMPRESSION; |
| 49 |
case ZZIP_CORRUPTED: |
| 50 |
case ZZIP_UNDEF: |
| 51 |
case ZZIP_DIR_LARGEFILE: |
| 52 |
return EXIT_FILEFORMAT; |
| 53 |
} |
| 54 |
return EXIT_ERRORS; |
| 55 |
} |
| 56 |
|
| 57 |
static int |
| 58 |
unzzip_list (int argc, char ** argv, int verbose) |
| 59 |
{ |
| 60 |
int argn; |
| 61 |
ZZIP_DIR* disk; |
| 62 |
zzip_error_t error; |
| 63 |
|
| 64 |
if (argc == 1) |
| 65 |
{ |
| 66 |
printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n"); |
| 67 |
return EXIT_OK; /* better provide an archive argument */ |
| 68 |
} |
| 69 |
|
| 70 |
disk = zzip_dir_open (argv[1], &error); |
| 71 |
if (! disk) { |
| 72 |
perror(argv[1]); |
| 73 |
return exitcode(error); |
| 74 |
} |
| 75 |
|
| 76 |
if (argc == 2) |
| 77 |
{ /* list all */ |
| 78 |
ZZIP_DIRENT entry; |
| 79 |
while(zzip_dir_read(disk, &entry)) |
| 80 |
{ |
| 81 |
char* name = entry.d_name; |
| 82 |
long long usize = entry.st_size; |
| 83 |
if (!verbose) |
| 84 |
{ |
| 85 |
printf ("%22lli %s\n", usize, name); |
| 86 |
} else |
| 87 |
{ |
| 88 |
long long csize = entry.d_csize; |
| 89 |
unsigned compr = entry.d_compr; |
| 90 |
const char* defl = (compr < sizeof(comprlevel)) ? comprlevel[compr] : "(redu)"; |
| 91 |
printf ("%lli/%lli %s %s\n", usize, csize, defl, name); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
else |
| 96 |
{ /* list only the matching entries - in order of zip directory */ |
| 97 |
ZZIP_DIRENT entry; |
| 98 |
while(zzip_dir_read(disk, &entry)) |
| 99 |
{ |
| 100 |
char* name = entry.d_name; |
| 101 |
for (argn=1; argn < argc; argn++) |
| 102 |
{ |
| 103 |
if (! _zzip_fnmatch (argv[argn], name, |
| 104 |
_zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD)) |
| 105 |
{ |
| 106 |
long long usize = entry.st_size; |
| 107 |
if (!verbose) |
| 108 |
{ |
| 109 |
printf ("%22lli %s\n", usize, name); |
| 110 |
} else |
| 111 |
{ |
| 112 |
long long csize = entry.d_csize; |
| 113 |
unsigned compr = entry.d_compr; |
| 114 |
const char* defl = (compr < sizeof(comprlevel)) ? comprlevel[compr] : "(redu)"; |
| 115 |
printf ("%lli/%lli %s %s\n", usize, csize, defl, name); |
| 116 |
} |
| 117 |
break; /* match loop */ |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
zzip_dir_close(disk); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
int |
| 127 |
unzzip_long_list (int argc, char ** argv) |
| 128 |
{ |
| 129 |
return unzzip_list(argc, argv, 1); |
| 130 |
} |
| 131 |
|
| 132 |
int |
| 133 |
unzzip_show_list (int argc, char ** argv) |
| 134 |
{ |
| 135 |
return unzzip_list(argc, argv, 0); |
| 136 |
} |
| 137 |
|
| 138 |
/* |
| 139 |
* Local variables: |
| 140 |
* c-file-style: "stroustrup" |
| 141 |
* End: |
| 142 |
*/ |