diff options
Diffstat (limited to 'Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-big.c')
-rw-r--r-- | Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-big.c | 93 |
1 files changed, 48 insertions, 45 deletions
diff --git a/Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-big.c b/Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-big.c index 3774939aacb..7ab6bad9b15 100644 --- a/Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-big.c +++ b/Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-big.c @@ -8,27 +8,18 @@ #include <zzip/fseeko.h> #include <stdlib.h> #include <string.h> -#include "unzzip.h" - -#ifdef ZZIP_HAVE_FNMATCH_H -#include <fnmatch.h> -#else -#define fnmatch(x,y,z) strcmp(x,y) -#endif - -#ifndef O_BINARY -#define O_BINARY 0 -#endif - -#ifdef DEBUG -#define debug1(msg) do { fprintf(stderr, "%s : " msg "\n", __func__); } while(0) -#define debug2(msg, arg1) do { fprintf(stderr, "%s : " msg "\n", __func__, arg1); } while(0) -#define debug3(msg, arg1, arg2) do { fprintf(stderr, "%s : " msg "\n", __func__, arg1, arg2); } while(0) -#else -#define debug1(msg) -#define debug2(msg, arg1) -#define debug3(msg, arg1, arg2) -#endif +#include <sys/stat.h> +#include <zzip/__mkdir.h> +#include <zzip/__string.h> +#include <zzip/__debug.h> +#include <zzip/__fnmatch.h> +#include "unzzipcat-zip.h" +#include "unzzip-states.h" + +static int exitcode(int e) +{ + return EXIT_ERRORS; +} static void unzzip_big_entry_fprint(ZZIP_ENTRY* entry, FILE* out) { @@ -38,14 +29,14 @@ static void unzzip_big_entry_fprint(ZZIP_ENTRY* entry, FILE* out) char buffer[1024]; int len; while ((len = zzip_entry_fread (buffer, 1024, 1, file))) { - debug2("entry read %i", len); + DBG2("entry read %i", len); fwrite (buffer, len, 1, out); } - debug2("entry done %s", strerror(errno)); + DBG2("entry done %s", strerror(errno)); zzip_entry_fclose (file); } else { - debug2("could not open entry: %s", strerror(errno)); + DBG2("could not open entry: %s", strerror(errno)); } } @@ -62,21 +53,20 @@ static void unzzip_cat_file(FILE* disk, char* name, FILE* out) } } -#if !defined(ZZIP_HAVE_STRNDUP) -static char * -strndup(char *p, size_t maxlen) +static void makedirs(const char* name) { - char *r; - if (!p) - return NULL; - r = malloc(maxlen + 1); - if (!r) - return NULL; - strncpy(r, p, maxlen); - r[maxlen] = '\0'; - return r; + char* p = strrchr(name, '/'); + if (p) { + char* dir_name = _zzip_strndup(name, p-name); + makedirs(dir_name); + free (dir_name); + } + if (_zzip_mkdir(name, 0775) == -1 && errno != EEXIST) + { + DBG3("while mkdir %s : %s", name, strerror(errno)); + } + errno = 0; } -#endif static FILE* create_fopen(char* name, char* mode, int subdirs) { @@ -84,8 +74,8 @@ static FILE* create_fopen(char* name, char* mode, int subdirs) { char* p = strrchr(name, '/'); if (p) { - char* dir_name = strndup(name, p-name); - // makedirs(dir_name); // TODO + char* dir_name = _zzip_strndup(name, p-name); + makedirs(dir_name); free (dir_name); } } @@ -95,13 +85,14 @@ static FILE* create_fopen(char* name, char* mode, int subdirs) static int unzzip_cat (int argc, char ** argv, int extract) { + int done = 0; int argn; FILE* disk; disk = fopen (argv[1], "rb"); if (! disk) { perror(argv[1]); - return -1; + return exitcode(errno); } if (argc == 2) @@ -111,12 +102,20 @@ static int unzzip_cat (int argc, char ** argv, int extract) { char* name = zzip_entry_strdup_name (entry); FILE* out = stdout; + if (! name) { + done = EXIT_WARNINGS; + continue; + } if (extract) out = create_fopen(name, "wb", 1); + if (! out) { + if (errno != EISDIR) done = EXIT_ERRORS; + continue; + } unzzip_cat_file (disk, name, out); if (extract) fclose(out); free (name); } - return 0; + return done; } if (argc == 3 && !extract) @@ -135,12 +134,16 @@ static int unzzip_cat (int argc, char ** argv, int extract) for (; entry ; entry = zzip_entry_findnext(entry)) { char* name = zzip_entry_strdup_name (entry); - debug3(".. check '%s' to zip '%s'", argv[argn], name); - if (! fnmatch (argv[argn], name, - FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD)) + DBG3(".. check '%s' to zip '%s'", argv[argn], name); + if (! _zzip_fnmatch (argv[argn], name, + FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD)) { FILE* out = stdout; if (extract) out = create_fopen(name, "wb", 1); + if (! out) { + if (errno != EISDIR) done = EXIT_ERRORS; + continue; + } unzzip_cat_file (disk, name, out); if (extract) fclose(out); break; /* match loop */ @@ -148,7 +151,7 @@ static int unzzip_cat (int argc, char ** argv, int extract) free (name); } } - return 0; + return done; } int unzzip_print (int argc, char ** argv) |