From 42a25e6f9b9f01ae28931ea123f7a541ee763825 Mon Sep 17 00:00:00 2001 From: Guido Draheim Date: Mon, 16 May 2005 10:40:47 +0000 Subject: [PATCH] memdisk-stuff (.) --- bins/unzzipcat-mem.c | 128 +++++++++++++++++++++++++++++++++++++++++++ bins/unzzipdir-mem.c | 101 ++++++++++++++++++++++++++++++++++ configure | 2 +- zzip/Makefile.am | 2 +- zzip/Makefile.in | 2 +- zzip/__fnmatch.h | 28 ++++++++++ zzip/fseeko.c | 14 ++--- zzip/fseeko.h | 2 +- zzip/memdisk.c | 12 ++-- zzip/memdisk.h | 2 - zzip/mmapped.c | 35 +++--------- zzip/mmapped.h | 2 +- 12 files changed, 283 insertions(+), 47 deletions(-) create mode 100644 bins/unzzipcat-mem.c create mode 100644 bins/unzzipdir-mem.c create mode 100644 zzip/__fnmatch.h diff --git a/bins/unzzipcat-mem.c b/bins/unzzipcat-mem.c new file mode 100644 index 0000000..7474854 --- /dev/null +++ b/bins/unzzipcat-mem.c @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2003 Guido Draheim + * Use freely under the restrictions of the ZLIB license. + * + * This file is used as an example to clarify zzipmmap api usage. + */ + +#include +#include +#include +#include + +#ifdef ZZIP_HAVE_UNISTD_H +#include +#endif +#ifdef ZZIP_HAVE_IO_H +#include +#endif + +#ifdef ZZIP_HAVE_FNMATCH_H +#include +#else +#define fnmatch(x,y,z) strcmp(x,y) +#endif + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +static const char usage[] = +{ + "unzzipdir-mem [names].. \n" + " - unzzip data content of files contained in a zip archive.\n" +}; + +static void zzip_mem_entry_fprint(ZZIP_MEM_DISK* disk, + ZZIP_MEM_ENTRY* entry, FILE* out) +{ + ZZIP_DISK_FILE* file = zzip_mem_entry_fopen (disk, entry); + if (file) + { + char buffer[1024]; int len; + while ((len = zzip_mem_disk_fread (buffer, 1024, 1, file))) + fwrite (buffer, len, 1, out); + + zzip_mem_disk_fclose (file); + } +} + +static void zzip_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; + while ((len = zzip_mem_disk_fread (buffer, 1, 1024, file))) + { + fwrite (buffer, 1, len, out); + } + + zzip_mem_disk_fclose (file); + } +} + +int +main (int argc, char ** argv) +{ + int argn; + ZZIP_MEM_DISK* disk; + + if (argc <= 1 || ! strcmp (argv[1], "--help")) + { + printf (usage); + return 0; + } + if (! strcmp (argv[1], "--version")) + { + printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n"); + return 0; + } + + disk = zzip_mem_disk_open (argv[1]); + if (! disk) { + perror(argv[1]); + return -1; + } + + if (argc == 2) + { /* print directory list */ + ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk); + for (; entry ; entry = zzip_mem_disk_findnext(disk, entry)) + { + char* name = zzip_mem_entry_to_name (entry); + printf ("%s\n", name); + } + return 0; + } + + if (argc == 3) + { /* list from one spec */ + ZZIP_MEM_ENTRY* entry = 0; + while ((entry = zzip_mem_disk_findmatch(disk, argv[2], entry, 0, 0))) + { + zzip_mem_entry_fprint (disk, entry, stdout); + } + + return 0; + } + + for (argn=1; argn < argc; argn++) + { /* list only the matching entries - each in order of commandline */ + ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk); + 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)) + zzip_mem_disk_cat_file (disk, name, stdout); + } + } + return 0; +} + +/* + * Local variables: + * c-file-style: "stroustrup" + * End: + */ diff --git a/bins/unzzipdir-mem.c b/bins/unzzipdir-mem.c new file mode 100644 index 0000000..dc02077 --- /dev/null +++ b/bins/unzzipdir-mem.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2003 Guido Draheim + * Use freely under the restrictions of the ZLIB license. + * + * This file is used as an example to clarify zzipmmap api usage. + */ + +#include +#include +#include +#include + +#ifdef ZZIP_HAVE_UNISTD_H +#include +#endif +#ifdef ZZIP_HAVE_IO_H +#include +#endif + +#ifdef ZZIP_HAVE_FNMATCH_H +#include +#else +#define fnmatch(x,y,z) strcmp(x,y) +#endif + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +static const char usage[] = +{ + "unzzipdir [names].. \n" + " - unzzip a listing of files contained in a zip archive.\n" +}; + +int +main (int argc, char ** argv) +{ + int argn; + ZZIP_MEM_DISK* disk; + + if (argc <= 1 || ! strcmp (argv[1], "--help")) + { + printf (usage); + return 0; + } + if (! strcmp (argv[1], "--version")) + { + printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n"); + return 0; + } + + disk = zzip_mem_disk_open (argv[1]); + if (! disk) { + perror(argv[1]); + return -1; + } + + if (argc == 2) + { /* list all */ + ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk); + for (; entry ; entry = zzip_mem_disk_findnext(disk, entry)) + { + char* name = zzip_mem_entry_to_name (entry); + printf ("%s\n", name); + } + return 0; + } + + if (argc == 3) + { /* list from one spec */ + ZZIP_MEM_ENTRY* entry = 0; + while ((entry = zzip_mem_disk_findmatch(disk, argv[2], entry, 0, 0))) + { + char* name = zzip_mem_entry_to_name (entry); + printf ("%s\n", name); + } + return 0; + } + + { /* list only the matching entries - in order of zip directory */ + ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk); + for (; entry ; entry = zzip_mem_disk_findnext(disk, entry)) + { + char* name = zzip_mem_entry_to_name (entry); + for (argn=1; argn < argc; argn++) + { + if (! fnmatch (argv[argn], name, + FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD)) + printf ("%s\n", name); + } + } + return 0; + } +} + +/* + * Local variables: + * c-file-style: "stroustrup" + * End: + */ diff --git a/configure b/configure index 456c6eb..ce272ce 100755 --- a/configure +++ b/configure @@ -1,5 +1,5 @@ #! /bin/sh -# From configure.ac Revision: 1.3 . +# From configure.ac Revision: 1.4 . # Guess values for system-dependent variables and create Makefiles. # Generated by GNU Autoconf 2.59. # diff --git a/zzip/Makefile.am b/zzip/Makefile.am index 3533dba..951210b 100644 --- a/zzip/Makefile.am +++ b/zzip/Makefile.am @@ -8,7 +8,7 @@ zzipdir = ${includedir}/zzip zzip_HEADERS = $(libzzip_la_headers) \ $(libzzipmmapped_la_headers) \ $(libzzipfseeko_la_headers) -noinst_HEADERS = __debug.h __hints.h __mmap.h __dirent.h +noinst_HEADERS = __debug.h __hints.h __mmap.h __dirent.h __fnmatch.h # VERSION_INFO=@VERSION_INFO@ RELEASE_INFO=@RELEASE_INFO@ diff --git a/zzip/Makefile.in b/zzip/Makefile.in index 11ac9ec..fb232f7 100644 --- a/zzip/Makefile.in +++ b/zzip/Makefile.in @@ -234,7 +234,7 @@ zzip_HEADERS = $(libzzip_la_headers) \ $(libzzipmmapped_la_headers) \ $(libzzipfseeko_la_headers) -noinst_HEADERS = __debug.h __hints.h __mmap.h __dirent.h +noinst_HEADERS = __debug.h __hints.h __mmap.h __dirent.h __fnmatch.h # libzzip_la_SOURCES = \ zip.c \ diff --git a/zzip/__fnmatch.h b/zzip/__fnmatch.h new file mode 100644 index 0000000..3456a56 --- /dev/null +++ b/zzip/__fnmatch.h @@ -0,0 +1,28 @@ +#ifndef __ZZIP__FNMATCH_H +#define __ZZIP__FNMATCH_H +/** included by fseeko.c, mmapped.c, memdisk.c */ + +#include + +#ifdef ZZIP_HAVE_FNMATCH_H +#include +#endif + +#ifdef ZZIP_HAVE_FNMATCH_H +#define _zzip_fnmatch fnmatch +# ifdef FNM_CASEFOLD +# define _zzip_fnmatch_CASEFOLD FNM_CASEFOLD +# else +# define _zzip_fnmatch_CASEFOLD 0 +# endif +#else +# define _zzip_fnmatch_CASEFOLD 0 +/* if your system does not have fnmatch, we fall back to strcmp: */ +static int _zzip_fnmatch(char* pattern, char* string, int flags) +{ + puts (""); + return strcmp (pattern, string); +} +#endif + +#endif diff --git a/zzip/fseeko.c b/zzip/fseeko.c index 7daab33..1f026e9 100644 --- a/zzip/fseeko.c +++ b/zzip/fseeko.c @@ -28,17 +28,15 @@ * of the Mozilla Public License 1.1 */ -#define _LARGEFILE_SOURCE +#define _LARGEFILE_SOURCE 1 +#define _ZZIP_ENTRY_STRUCT 1 + +#include -#include #include #include #include -#ifdef ZZIP_HAVE_FNMATCH_H -#include -#endif - #if defined ZZIP_HAVE_STRING_H #include #elif defined ZZIP_HAVE_STRINGS_H @@ -47,8 +45,10 @@ #include #include +#include #include #include +#include #if __STDC_VERSION__+0 > 199900L #define ___ @@ -397,7 +397,7 @@ zzip_entry_findmatch(FILE* disk, char* filespec, { /* filenames within zip files are often not null-terminated! */ char* realname = zzip_entry_strdup_name (entry); if (! realname) continue; - if (! compare (filespec, realname, flags)) + if (! compare (filespec, realname, flags)) { free (realname); return entry; } else { free (realname); continue; diff --git a/zzip/fseeko.h b/zzip/fseeko.h index 34ce3c5..59ca576 100644 --- a/zzip/fseeko.h +++ b/zzip/fseeko.h @@ -72,7 +72,7 @@ zzip_entry_fclose (ZZIP_ENTRY_FILE* file); int zzip_entry_feof (ZZIP_ENTRY_FILE* file); -# ifdef _ZZIP_MEM_ENTRY_PRIVATE +# ifdef _ZZIP_ENTRY_STRUCT # ifdef __cplusplus # define __zzip_entry_extends_zzip_disk_entry struct zzip_entry : public struct zzip_disk_entry diff --git a/zzip/memdisk.c b/zzip/memdisk.c index fab5087..1abd043 100644 --- a/zzip/memdisk.c +++ b/zzip/memdisk.c @@ -19,20 +19,20 @@ * or alternatively the restrictions * of the Mozilla Public License 1.1 */ -#define _ZZIP_MEM_DISK_PRIVATE 1 -#define _ZZIP_MMAPPED_PRIVATE 1 +#define _ZZIP_DISK_FILE_STRUCT 1 -#include /* archive handling */ -#include -#include -#include +#include #include #include #include +#include +#include +#include #include #include +#include #define ___ { #define ____ } diff --git a/zzip/memdisk.h b/zzip/memdisk.h index eb3d9c5..2922267 100644 --- a/zzip/memdisk.h +++ b/zzip/memdisk.h @@ -9,10 +9,8 @@ typedef struct _zzip_mem_entry ZZIP_MEM_ENTRY; struct _zzip_mem_disk { ZZIP_DISK* disk; -# ifdef _ZZIP_MEM_DISK_PRIVATE ZZIP_MEM_ENTRY* list; ZZIP_MEM_ENTRY* last; -# endif }; #ifndef zzip_mem_disk_extern diff --git a/zzip/mmapped.c b/zzip/mmapped.c index 3fd736c..76b6000 100644 --- a/zzip/mmapped.c +++ b/zzip/mmapped.c @@ -26,21 +26,13 @@ #define _GNU_SOURCE _glibc_developers_are_idiots_to_call_this_gnu_specific_ #endif -#define _ZZIP_MMAPPED_PRIVATE 1 +#define _ZZIP_DISK_FILE_STRUCT 1 -#include -#include -#include -#include +#include -#include #include #include -#ifdef ZZIP_HAVE_FNMATCH_H -#include -#endif - #if defined ZZIP_HAVE_UNISTD_H #include #elif defined ZZIP_HAVE_IO_H @@ -53,6 +45,12 @@ #include #endif +#include +#include +#include +#include +#include +#include #if __STDC_VERSION__+0 > 199900L #define ___ @@ -407,23 +405,6 @@ zzip_disk_findfile(ZZIP_DISK* disk, char* filename, return 0; } -#ifdef ZZIP_HAVE_FNMATCH_H -#define _zzip_fnmatch fnmatch -# ifdef FNM_CASEFOLD -# define _zzip_fnmatch_CASEFOLD FNM_CASEFOLD -# else -# define _zzip_fnmatch_CASEFOLD 0 -# endif -#else -# define _zzip_fnmatch_CASEFOLD 0 -/* if your system does not have fnmatch, we fall back to strcmp: */ -static int _zzip_fnmatch(char* pattern, char* string, int flags) -{ - puts (""); - return strcmp (pattern, string); -} -#endif - /** => zzip_disk_findfile * * This function uses a compare-function with an additional argument diff --git a/zzip/mmapped.h b/zzip/mmapped.h index f27869f..47a750c 100644 --- a/zzip/mmapped.h +++ b/zzip/mmapped.h @@ -100,7 +100,7 @@ zzip_disk_fclose (ZZIP_DISK_FILE* file); int zzip_disk_feof (ZZIP_DISK_FILE* file); -#ifdef _ZZIP_MMAPPED_PRIVATE +#ifdef _ZZIP_DISK_FILE_STRUCT /** * typedef struct zzip_disk_file ZZIP_DISK_FILE; */ -- 2.40.0