File: unzzipshow.c

package info (click to toggle)
zziplib 0.13.78%2Bdfsg.1-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,544 kB
  • sloc: python: 12,101; ansic: 9,092; sh: 6,293; perl: 2,376; makefile: 1,014; sed: 47
file content (116 lines) | stat: -rw-r--r-- 2,918 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 *	Copyright (c) 2003 Guido Draheim <guidod@gmx.de>
 *      Use freely under the restrictions of the ZLIB license.
 *
 *      This file is used as an example to clarify zzipfseeko api usage.
 */

#include <zzip/fseeko.h>
#include <stdlib.h>
#include <string.h>
#include <zzip/__fnmatch.h>

static const char usage[] = /* .. */
    {"unzzipshow <zip> [names].. \n"
     "  - unzzip data content of files contained in a zip archive.\n"};

static void
zzip_entry_fprint(ZZIP_ENTRY* entry, FILE* out)
{
    ZZIP_ENTRY_FILE* file = zzip_entry_fopen(entry, 0);
    if (file) {
        char buffer[1024];
        int  len;
        while (0 < (len = zzip_entry_fread(buffer, 1024, 1, file)))
            fwrite(buffer, len, 1, out);

        zzip_entry_fclose(file);
    }
}

static void
zzip_cat_file(FILE* disk, char* name, FILE* out)
{
    ZZIP_ENTRY_FILE* file = zzip_entry_ffile(disk, name);
    if (file) {
        char buffer[1024];
        int  len;
        while (0 < (len = zzip_entry_fread(buffer, 1024, 1, file)))
            fwrite(buffer, len, 1, out);

        zzip_entry_fclose(file);
    }
}

#define BASENAME(x) (strchr((x), '/') ? strrchr((x), '/') + 1 : (x))

static int
unzzip_version()
{
    printf("%s version %s %s\n", BASENAME(__FILE__), ZZIP_PACKAGE_NAME, ZZIP_PACKAGE_VERSION);
    return 0;
}

static int
unzzip_help()
{
    printf(usage);
    return 0;
}

int
main(int argc, char** argv)
{
    int   argn;
    FILE* disk;

    if (argc <= 1 || ! strcmp(argv[1], "--help")) {
        return unzzip_help();
    }
    if (! strcmp(argv[1], "--version")) {
        return unzzip_version();
    }

    disk = fopen(argv[1], "r");
    if (! disk) {
        perror(argv[1]);
        return -1;
    }

    if (argc == 2) { /* print directory list */
        ZZIP_ENTRY* entry = zzip_entry_findfirst(disk);
        if (! entry)
            puts("no first entry!\n");
        for (; entry; entry = zzip_entry_findnext(entry)) {
            char* name = zzip_entry_strdup_name(entry);
	    if (name) {
            printf("%s\n", name);
            free(name);
	    }
        }
        return 0;
    }

    if (argc == 3) { /* list from one spec */
        ZZIP_ENTRY* entry = 0;
        while ((entry = zzip_entry_findmatch(disk, argv[2], entry, 0, 0)))
            zzip_entry_fprint(entry, stdout);

        return 0;
    }

    for (argn = 1; argn < argc;
         argn++) { /* list only the matching entries - each in order of commandline */
        ZZIP_ENTRY* entry = zzip_entry_findfirst(disk);
        for (; entry; entry = zzip_entry_findnext(entry)) {
            char* name = zzip_entry_strdup_name(entry);
            if (name) {
            if (! _zzip_fnmatch(argv[argn], name,
                                _zzip_FNM_NOESCAPE | _zzip_FNM_PATHNAME | _zzip_FNM_PERIOD))
                zzip_cat_file(disk, name, stdout);
            free(name);
            }
        }
    }
    return 0;
}