beta-0.89.2
[luatex.git] / source / libs / zziplib / zziplib-0.13.62 / bins / unzzipcat-seeko.c
blobf16f0b965662671675f44256806ce8c1f0dc2606
1 /*
2 * Copyright (c) 2003 Guido Draheim <guidod@gmx.de>
3 * Use freely under the restrictions of the ZLIB license.
5 * This file is used as an example to clarify zzipfseeko api usage.
6 */
8 #include <zzip/fseeko.h>
9 #include <stdlib.h>
10 #include <string.h>
12 #ifdef ZZIP_HAVE_FNMATCH_H
13 #include <fnmatch.h>
14 #else
15 #define fnmatch(x,y,z) strcmp(x,y)
16 #endif
18 #ifndef O_BINARY
19 #define O_BINARY 0
20 #endif
22 static const char usage[] =
24 "unzzipcat-seeko <zip> [names].. \n"
25 " - unzzip data content of files contained in a zip archive.\n"
28 static void zzip_entry_fprint(ZZIP_ENTRY* entry, FILE* out)
30 ZZIP_ENTRY_FILE* file = zzip_entry_fopen (entry, 0);
31 if (file)
33 char buffer[1024]; int len;
34 while ((len = zzip_entry_fread (buffer, 1024, 1, file)))
35 fwrite (buffer, len, 1, out);
37 zzip_entry_fclose (file);
41 static void zzip_cat_file(FILE* disk, char* name, FILE* out)
43 ZZIP_ENTRY_FILE* file = zzip_entry_ffile (disk, name);
44 if (file)
46 char buffer[1024]; int len;
47 while ((len = zzip_entry_fread (buffer, 1024, 1, file)))
48 fwrite (buffer, len, 1, out);
50 zzip_entry_fclose (file);
54 int
55 main (int argc, char ** argv)
57 int argn;
58 FILE* disk;
60 if (argc <= 1 || ! strcmp (argv[1], "--help"))
62 printf (usage);
63 return 0;
65 if (! strcmp (argv[1], "--version"))
67 printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
68 return 0;
71 disk = fopen (argv[1], "r");
72 if (! disk) {
73 perror(argv[1]);
74 return -1;
77 if (argc == 2)
78 { /* print directory list */
79 ZZIP_ENTRY* entry = zzip_entry_findfirst(disk);
80 if (! entry) puts("no first entry!\n");
81 for (; entry ; entry = zzip_entry_findnext(entry))
83 char* name = zzip_entry_strdup_name (entry);
84 printf ("%s\n", name);
85 free (name);
87 return 0;
90 if (argc == 3)
91 { /* list from one spec */
92 ZZIP_ENTRY* entry = 0;
93 while ((entry = zzip_entry_findmatch(disk, argv[2], entry, 0, 0)))
94 zzip_entry_fprint (entry, stdout);
96 return 0;
99 for (argn=1; argn < argc; argn++)
100 { /* list only the matching entries - each in order of commandline */
101 ZZIP_ENTRY* entry = zzip_entry_findfirst(disk);
102 for (; entry ; entry = zzip_entry_findnext(entry))
104 char* name = zzip_entry_strdup_name (entry);
105 if (! fnmatch (argv[argn], name,
106 FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD))
107 zzip_cat_file (disk, name, stdout);
108 free (name);
111 return 0;
115 * Local variables:
116 * c-file-style: "stroustrup"
117 * End: