diff options
Diffstat (limited to 'Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-mem.c')
-rw-r--r-- | Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-mem.c | 77 |
1 files changed, 43 insertions, 34 deletions
diff --git a/Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-mem.c b/Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-mem.c index f7d9e6d152f..13cca3aaf7b 100644 --- a/Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-mem.c +++ b/Build/source/libs/zziplib/zziplib-src/bins/unzzipcat-mem.c @@ -9,7 +9,13 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "unzzip.h" +#include <sys/stat.h> +#include <zzip/__mkdir.h> +#include <zzip/__fnmatch.h> +#include <zzip/__string.h> +#include <zzip/__debug.h> +#include "unzzipcat-zip.h" +#include "unzzip-states.h" #ifdef ZZIP_HAVE_UNISTD_H #include <unistd.h> @@ -18,15 +24,10 @@ #include <io.h> #endif -#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 +static int exitcode(int e) +{ + return EXIT_ERRORS; +} static void unzzip_mem_entry_fprint(ZZIP_MEM_DISK* disk, ZZIP_MEM_ENTRY* entry, FILE* out) @@ -47,7 +48,7 @@ static void unzzip_mem_disk_cat_file(ZZIP_MEM_DISK* disk, char* name, FILE* out) ZZIP_DISK_FILE* file = zzip_mem_disk_fopen (disk, name); if (file) { - char buffer[1024]; int len; + char buffer[1025]; int len; while ((len = zzip_mem_disk_fread (buffer, 1, 1024, file))) { fwrite (buffer, 1, len, out); @@ -57,21 +58,20 @@ static void unzzip_mem_disk_cat_file(ZZIP_MEM_DISK* 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) { @@ -79,45 +79,50 @@ 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); } } return fopen(name, mode); } - static int unzzip_cat (int argc, char ** argv, int extract) { + int done; int argn; ZZIP_MEM_DISK* disk; if (argc == 1) { printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n"); - return -1; /* better provide an archive argument */ + return EXIT_OK; /* better provide an archive argument */ } disk = zzip_mem_disk_open (argv[1]); if (! disk) { + DBG3("disk_open failed [%i] %s", errno, strerror(errno)); perror(argv[1]); - return -1; + return exitcode(errno); } if (argc == 2) { /* print directory list */ ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk); - fprintf(stderr, "..%p\n", entry); + DBG2("findfirst %p\n", entry); for (; entry ; entry = zzip_mem_disk_findnext(disk, entry)) { char* name = zzip_mem_entry_to_name (entry); FILE* out = stdout; if (extract) out = create_fopen(name, "wb", 1); + if (! out) { + if (errno != EISDIR) done = EXIT_ERRORS; + continue; + } unzzip_mem_disk_cat_file (disk, name, out); if (extract) fclose(out); } - return 0; + return done; } if (argc == 3 && !extract) @@ -137,18 +142,22 @@ static int unzzip_cat (int argc, char ** argv, int extract) for (; entry ; entry = zzip_mem_disk_findnext(disk, entry)) { char* name = zzip_mem_entry_to_name (entry); - if (! fnmatch (argv[argn], name, - FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD)) + 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_mem_disk_cat_file (disk, name, out); if (extract) fclose(out); break; /* match loop */ } } } - return 0; + return done; } int unzzip_print (int argc, char ** argv) |