File: zzipself.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 (78 lines) | stat: -rw-r--r-- 2,123 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
/*
 *	Copyright (c) 2000,2001,2002 Guido Draheim <guidod@gmx.de>
 *      Use freely under the restrictions of the ZLIB License
 */

#include <zzip/zzip.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef O_BINARY
#define O_BINARY 0
#endif

static const char usage[] = /* .. */
    {" zzipself <file>... \n"
     "  - prints the file to stdout, so you may want to redirect the output; \n"
     " the file can be a normal file or an inflated part of a zip-archive, \n"
     " however in this case the zip-archive happens to be the executed\n"
     " binary itself, as if this program is a self-extract header of a zip. \n"
     "    zzipself README # same as # zzcat zzipself/README \n"};

int
main(int argc, char** argv)
{
    int status = 0;
    int argn;
    if (argc <= 1) {
        printf(usage);
        exit(0);
    }

    for (argn = 1; argn < argc; argn++) {
        /* ZZIP_FILE* fp = zzip_fopen (argv[0]+"/"+argv[argn], "rbi"); */
        /* .... = zzip_open (argv[0]+"/"+argv[argn], O_RDONLY|O_BINARY,
         *                   ZZIP_CASELESS|ZZIP_ONLYZIP, ext, 0); */

        static const char* ext[] = {"", ".exe", ".EXE", 0};
        ZZIP_FILE*         fp;
        ZZIP_DIR*          zip = zzip_opendir_ext_io(argv[0], /* .. */
                                                     ZZIP_CASELESS | ZZIP_ONLYZIP, ext, 0);

        if (! zip) {
            perror(argv[0]);
            break;
        }

        fp = zzip_file_open(zip, argv[argn], ZZIP_CASELESS);

        if (! fp) {
            perror(argv[argn]);
            continue;
        }
        else {
            char buf[17];
            int  n;

            /* read chunks of 16 bytes into buf and print them to stdout */
            while (0 < (n = zzip_read(fp, buf, 16))) {
                buf[n] = '\0';
#ifdef STDOUT_FILENO
                write(STDOUT_FILENO, buf, n);
#else
                fwrite(buf, 1, n, stdout);
#endif
            }

            if (n == -1) {
                perror(argv[argn]);
                status++;
            }
        }

        zzip_file_close(fp);
        zzip_closedir(zip);
    }

    return status;
}