| 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 <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <sys/stat.h> |
| 13 |
#include <zzip/__mkdir.h> |
| 14 |
#include <zzip/__string.h> |
| 15 |
#include <zzip/__fnmatch.h> |
| 16 |
#include <zzip/__debug.h> |
| 17 |
#include "unzzipcat-zip.h" |
| 18 |
#include "unzzip-states.h" |
| 19 |
|
| 20 |
#ifdef ZZIP_HAVE_UNISTD_H |
| 21 |
#include <unistd.h> |
| 22 |
#endif |
| 23 |
#ifdef ZZIP_HAVE_IO_H |
| 24 |
#include <io.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
/* Functions in unzzip.c: */ |
| 28 |
extern int exitcode(int); |
| 29 |
extern FILE* create_fopen(char*, char*, int); |
| 30 |
|
| 31 |
static void unzzip_cat_file(ZZIP_DIR* disk, char* name, FILE* out) |
| 32 |
{ |
| 33 |
ZZIP_FILE* file = zzip_file_open (disk, name, 0); |
| 34 |
if (file) |
| 35 |
{ |
| 36 |
char buffer[1024]; int len; |
| 37 |
while (0 < (len = zzip_file_read (file, buffer, 1024))) |
| 38 |
{ |
| 39 |
fwrite (buffer, 1, len, out); |
| 40 |
} |
| 41 |
|
| 42 |
zzip_file_close (file); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
static int unzzip_cat (int argc, char ** argv, int extract) |
| 47 |
{ |
| 48 |
int done = 0; |
| 49 |
int argn; |
| 50 |
ZZIP_DIR* disk; |
| 51 |
zzip_error_t error; |
| 52 |
|
| 53 |
if (argc == 1) |
| 54 |
{ |
| 55 |
printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n"); |
| 56 |
return EXIT_OK; /* better provide an archive argument */ |
| 57 |
} |
| 58 |
|
| 59 |
disk = zzip_dir_open (argv[1], &error); |
| 60 |
if (! disk) { |
| 61 |
fprintf(stderr, "%s: %s\n", argv[1], zzip_strerror(error)); |
| 62 |
return exitcode(error); |
| 63 |
} |
| 64 |
|
| 65 |
if (argc == 2) |
| 66 |
{ /* list all */ |
| 67 |
ZZIP_DIRENT entry; |
| 68 |
while(zzip_dir_read(disk, &entry)) |
| 69 |
{ |
| 70 |
char* name = entry.d_name; |
| 71 |
FILE* out = stdout; |
| 72 |
if (extract) out = create_fopen(name, "wb", 1); |
| 73 |
if (! out) { |
| 74 |
DBG3("fopen' %s : %s", name, strerror(errno)); |
| 75 |
if (errno != EISDIR) done = EXIT_ERRORS; |
| 76 |
continue; |
| 77 |
} |
| 78 |
unzzip_cat_file (disk, name, out); |
| 79 |
if (extract) fclose(out); |
| 80 |
} |
| 81 |
} |
| 82 |
else |
| 83 |
{ /* list only the matching entries - in order of zip directory */ |
| 84 |
ZZIP_DIRENT entry; |
| 85 |
while(zzip_dir_read(disk, &entry)) |
| 86 |
{ |
| 87 |
char* name = entry.d_name; |
| 88 |
for (argn=1; argn < argc; argn++) |
| 89 |
{ |
| 90 |
if (! _zzip_fnmatch (argv[argn], name, |
| 91 |
_zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD)) |
| 92 |
{ |
| 93 |
FILE* out = stdout; |
| 94 |
if (extract) out = create_fopen(name, "wb", 1); |
| 95 |
if (! out) { |
| 96 |
DBG3("fopen. %s : %s", name, strerror(errno)); |
| 97 |
if (errno != EISDIR) done = EXIT_ERRORS; |
| 98 |
continue; |
| 99 |
} |
| 100 |
unzzip_cat_file (disk, name, out); |
| 101 |
if (extract) fclose(out); |
| 102 |
break; /* match loop */ |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
zzip_dir_close(disk); |
| 108 |
return done; |
| 109 |
} |
| 110 |
|
| 111 |
int unzzip_print (int argc, char ** argv) |
| 112 |
{ |
| 113 |
return unzzip_cat(argc, argv, 0); |
| 114 |
} |
| 115 |
|
| 116 |
int unzzip_extract (int argc, char ** argv) |
| 117 |
{ |
| 118 |
return unzzip_cat(argc, argv, 1); |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
* Local variables: |
| 123 |
* c-file-style: "stroustrup" |
| 124 |
* End: |
| 125 |
*/ |