| 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 zzipfseeko api usage. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <zzip/fseeko.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <sys/stat.h> |
| 12 |
#include <zzip/__mkdir.h> |
| 13 |
#include <zzip/__string.h> |
| 14 |
#include <zzip/__debug.h> |
| 15 |
#include <zzip/__fnmatch.h> |
| 16 |
#include "unzzipcat-zip.h" |
| 17 |
#include "unzzip-states.h" |
| 18 |
|
| 19 |
/* Functions in unzzip.c: */ |
| 20 |
extern int exitcode(int); |
| 21 |
extern FILE* create_fopen(char*, char*, int); |
| 22 |
|
| 23 |
static void unzzip_big_entry_fprint(ZZIP_ENTRY* entry, FILE* out) |
| 24 |
{ |
| 25 |
ZZIP_ENTRY_FILE* file = zzip_entry_fopen (entry, 0); |
| 26 |
if (file) |
| 27 |
{ |
| 28 |
char buffer[1024]; int len; |
| 29 |
while (0 < (len = zzip_entry_fread (buffer, 1024, 1, file))) |
| 30 |
{ |
| 31 |
DBG2("entry read %i", len); |
| 32 |
fwrite (buffer, len, 1, out); |
| 33 |
} |
| 34 |
DBG2("entry done %s", strerror(errno)); |
| 35 |
zzip_entry_fclose (file); |
| 36 |
} else |
| 37 |
{ |
| 38 |
DBG2("could not open entry: %s", strerror(errno)); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
static void unzzip_cat_file(FILE* disk, char* name, FILE* out) |
| 43 |
{ |
| 44 |
ZZIP_ENTRY_FILE* file = zzip_entry_ffile (disk, name); |
| 45 |
if (file) |
| 46 |
{ |
| 47 |
char buffer[1024]; int len; |
| 48 |
while (0 < (len = zzip_entry_fread (buffer, 1024, 1, file))) |
| 49 |
fwrite (buffer, len, 1, out); |
| 50 |
|
| 51 |
zzip_entry_fclose (file); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
static int unzzip_cat (int argc, char ** argv, int extract) |
| 57 |
{ |
| 58 |
int done = 0; |
| 59 |
int argn; |
| 60 |
FILE* disk; |
| 61 |
|
| 62 |
disk = fopen (argv[1], "rb"); |
| 63 |
if (! disk) { |
| 64 |
perror(argv[1]); |
| 65 |
return exitcode(errno); |
| 66 |
} |
| 67 |
|
| 68 |
if (argc == 2) |
| 69 |
{ /* print directory list */ |
| 70 |
ZZIP_ENTRY* entry = zzip_entry_findfirst(disk); |
| 71 |
for (; entry ; entry = zzip_entry_findnext(entry)) |
| 72 |
{ |
| 73 |
char* name = zzip_entry_strdup_name (entry); |
| 74 |
FILE* out = stdout; |
| 75 |
if (! name) { |
| 76 |
done = EXIT_WARNINGS; |
| 77 |
continue; |
| 78 |
} |
| 79 |
if (extract) out = create_fopen(name, "wb", 1); |
| 80 |
if (! out) { |
| 81 |
if (errno != EISDIR) done = EXIT_ERRORS; |
| 82 |
continue; |
| 83 |
} |
| 84 |
unzzip_cat_file (disk, name, out); |
| 85 |
if (extract) fclose(out); |
| 86 |
free (name); |
| 87 |
} |
| 88 |
return done; |
| 89 |
} |
| 90 |
|
| 91 |
if (argc == 3 && !extract) |
| 92 |
{ /* list from one spec */ |
| 93 |
ZZIP_ENTRY* entry = 0; |
| 94 |
while ((entry = zzip_entry_findmatch(disk, argv[2], entry, 0, 0))) |
| 95 |
{ |
| 96 |
unzzip_big_entry_fprint (entry, stdout); |
| 97 |
} |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
for (argn=1; argn < argc; argn++) |
| 102 |
{ /* list only the matching entries - each in order of commandline */ |
| 103 |
ZZIP_ENTRY* entry = zzip_entry_findfirst(disk); |
| 104 |
for (; entry ; entry = zzip_entry_findnext(entry)) |
| 105 |
{ |
| 106 |
char* name = zzip_entry_strdup_name (entry); |
| 107 |
DBG3(".. check '%s' to zip '%s'", argv[argn], name); |
| 108 |
if (! _zzip_fnmatch (argv[argn], name, |
| 109 |
_zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD)) |
| 110 |
{ |
| 111 |
FILE* out = stdout; |
| 112 |
if (extract) out = create_fopen(name, "wb", 1); |
| 113 |
if (! out) { |
| 114 |
if (errno != EISDIR) done = EXIT_ERRORS; |
| 115 |
continue; |
| 116 |
} |
| 117 |
unzzip_cat_file (disk, name, out); |
| 118 |
if (extract) fclose(out); |
| 119 |
break; /* match loop */ |
| 120 |
} |
| 121 |
free (name); |
| 122 |
} |
| 123 |
} |
| 124 |
return done; |
| 125 |
} |
| 126 |
|
| 127 |
int unzzip_print (int argc, char ** argv) |
| 128 |
{ |
| 129 |
return unzzip_cat(argc, argv, 0); |
| 130 |
} |
| 131 |
|
| 132 |
int unzzip_extract (int argc, char ** argv) |
| 133 |
{ |
| 134 |
return unzzip_cat(argc, argv, 1); |
| 135 |
} |
| 136 |
|
| 137 |
/* |
| 138 |
* Local variables: |
| 139 |
* c-file-style: "stroustrup" |
| 140 |
* End: |
| 141 |
*/ |