~reviczky/luatex/luatex-svn

« back to all changes in this revision

Viewing changes to source/libs/zziplib/zziplib-0.13.62/bins/unzzipcat.c

  • Committer: Adam Reviczky
  • Date: 2015-03-29 18:56:26 UTC
  • Revision ID: adam.reviczky@kclalumni.net-20150329185626-7j7tmwyfpa69lqwo
Revision 5213

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      Copyright (c) 2003 Guido Draheim <guidod@gmx.de>
 
3
 *      Use freely under the restrictions of the ZLIB license.
 
4
 *
 
5
 *      This file is used as an example to clarify zzipmmap api usage.
 
6
 */
 
7
 
 
8
#include <zzip/mmapped.h>
 
9
#include <stdio.h>
 
10
#include <stdlib.h>
 
11
#include <string.h>
 
12
 
 
13
#ifdef ZZIP_HAVE_UNISTD_H
 
14
#include <unistd.h>
 
15
#endif
 
16
#ifdef ZZIP_HAVE_IO_H
 
17
#include <io.h>
 
18
#endif
 
19
 
 
20
#ifdef ZZIP_HAVE_FNMATCH_H
 
21
#include <fnmatch.h>
 
22
#else
 
23
#define fnmatch(x,y,z) strcmp(x,y)
 
24
#endif
 
25
 
 
26
#ifndef O_BINARY
 
27
#define O_BINARY 0
 
28
#endif
 
29
 
 
30
static const char usage[] = 
 
31
{
 
32
    "unzzipdir <zip> [names].. \n"
 
33
    "  - unzzip data content of files contained in a zip archive.\n"
 
34
};
 
35
 
 
36
static void zzip_disk_entry_fprint(ZZIP_DISK* disk, 
 
37
                                   ZZIP_DISK_ENTRY* entry, FILE* out)
 
38
{
 
39
    ZZIP_DISK_FILE* file = zzip_disk_entry_fopen (disk, entry);
 
40
    if (file) 
 
41
    {
 
42
        char buffer[1024]; int len;
 
43
        while ((len = zzip_disk_fread (buffer, 1024, 1, file)))
 
44
            fwrite (buffer, len, 1, out);
 
45
        
 
46
        zzip_disk_fclose (file);
 
47
    }
 
48
}
 
49
 
 
50
static void zzip_disk_cat_file(ZZIP_DISK* disk, char* name, FILE* out)
 
51
{
 
52
    ZZIP_DISK_FILE* file = zzip_disk_fopen (disk, name);
 
53
    if (file) 
 
54
    {
 
55
        char buffer[1024]; int len;
 
56
        while ((len = zzip_disk_fread (buffer, 1, 1024, file))) 
 
57
        {
 
58
            fwrite (buffer, 1, len, out);
 
59
        }
 
60
        
 
61
        zzip_disk_fclose (file);
 
62
    }
 
63
}
 
64
 
 
65
int 
 
66
main (int argc, char ** argv)
 
67
{
 
68
    int argn;
 
69
    ZZIP_DISK* disk;
 
70
 
 
71
    if (argc <= 1 || ! strcmp (argv[1], "--help"))
 
72
    {
 
73
        printf (usage);
 
74
        return 0;
 
75
    }
 
76
    if (! strcmp (argv[1], "--version"))
 
77
    {
 
78
        printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
 
79
        return 0;
 
80
    }
 
81
 
 
82
    disk = zzip_disk_open (argv[1]);
 
83
    if (! disk) {
 
84
        perror(argv[1]);
 
85
        return -1;
 
86
    }
 
87
 
 
88
    if (argc == 2)
 
89
    {  /* print directory list */
 
90
        ZZIP_DISK_ENTRY* entry = zzip_disk_findfirst(disk);
 
91
        for (; entry ; entry = zzip_disk_findnext(disk, entry))
 
92
        {
 
93
            char* name = zzip_disk_entry_strdup_name (disk, entry);
 
94
            printf ("%s\n", name);
 
95
            free (name);
 
96
        }
 
97
        return 0;
 
98
    }
 
99
 
 
100
    if (argc == 3)
 
101
    {  /* list from one spec */
 
102
        ZZIP_DISK_ENTRY* entry = 0;
 
103
        while ((entry = zzip_disk_findmatch(disk, argv[2], entry, 0, 0)))
 
104
             zzip_disk_entry_fprint (disk, entry, stdout);
 
105
 
 
106
        return 0;
 
107
    }
 
108
 
 
109
    for (argn=1; argn < argc; argn++)
 
110
    {   /* list only the matching entries - each in order of commandline */
 
111
        ZZIP_DISK_ENTRY* entry = zzip_disk_findfirst(disk);
 
112
        for (; entry ; entry = zzip_disk_findnext(disk, entry))
 
113
        {
 
114
            char* name = zzip_disk_entry_strdup_name (disk, entry);
 
115
            if (! fnmatch (argv[argn], name, 
 
116
                           FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD))
 
117
                zzip_disk_cat_file (disk, name, stdout);
 
118
            free (name);
 
119
        }
 
120
    }
 
121
    return 0;
 
122
 
123
 
 
124
/* 
 
125
 * Local variables:
 
126
 * c-file-style: "stroustrup"
 
127
 * End:
 
128
 */