summaryrefslogtreecommitdiff
path: root/rpm2cpio.c
blob: 37f8f7e83d657c2b4f01ee8881e6a374d6242eab (plain)
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
/* rpmarchive: spit out the main archive portion of a package */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <zlib.h>

#include "rpmlib.h"

char *zlib_err [] = {
   "No",
   "Unix",
   "Data",
   "Memory",
   "Buffer",
   "Version"
};

int main(int argc, char **argv)
{
    int fd;
    Header hd;
    int rc, isSource;
    char buffer[1024];
    int ct;
    gzFile stream;
    
    if (argc == 1) {
	fd = 0;
    } else {
	fd = open(argv[1], O_RDONLY, 0644);
    }

    if (fd < 0) {
	perror("cannot open package");
	exit(1);
    }

    rc = pkgReadHeader(fd, &hd, &isSource);
    if (rc == 1) {
	fprintf(stderr, "argument is not an RPM package\n");
	exit(1);
    } else if (rc) {
	fprintf(stderr, "error reading header from package\n");
	exit(1);
    }

    stream = gzdopen(fd, "r");

    while ((ct = gzread(stream, &buffer, 1024)) > 0) {
	write(1, &buffer, ct);
    }
    if (ct < 0){
        int zerror;
       
        gzerror (stream, &zerror);
        if (zerror == Z_ERRNO){
	    perror ("While uncompressing");
	    return 1;
	}
        fprintf (stderr, "rpm2cpio: zlib: %s error\n", zlib_err [-zerror]);
    }
    return 0;
}