ae978d3df501b277de13793453fddf10cc8a0110
[platform/upstream/rpm.git] / rpm2cpio.c
1 /* rpmarchive: spit out the main archive portion of a package */
2
3 #include "system.h"
4
5 #include "intl.h"
6 #include "rpmlib.h"
7
8 char *zlib_err [] = {
9    "No",
10    "Unix",
11    "Data",
12    "Memory",
13    "Buffer",
14    "Version"
15 };
16
17 int main(int argc, char **argv)
18 {
19     int fd;
20     Header hd;
21     int rc, isSource;
22     char buffer[1024];
23     int ct;
24     gzFile stream;
25     
26     if (argc == 1) {
27         fd = 0;
28     } else {
29         fd = open(argv[1], O_RDONLY, 0644);
30     }
31
32     if (fd < 0) {
33         perror("cannot open package");
34         exit(1);
35     }
36
37     rc = rpmReadPackageHeader(fd, &hd, &isSource, NULL, NULL);
38     if (rc == 1) {
39         fprintf(stderr, _("argument is not an RPM package\n"));
40         exit(1);
41     } else if (rc) {
42         fprintf(stderr, _("error reading header from package\n"));
43         exit(1);
44     }
45
46     stream = gzdopen(fd, "r");
47
48     while ((ct = gzread(stream, &buffer, 1024)) > 0) {
49         write(1, &buffer, ct);
50     }
51     if (ct < 0){
52         int zerror;
53        
54         gzerror (stream, &zerror);
55         if (zerror == Z_ERRNO){
56             perror ("While uncompressing");
57             gzclose(stream);
58             return 1;
59         }
60         fprintf (stderr, "rpm2cpio: zlib: %s error\n", zlib_err [-zerror - 1]);
61     }
62
63     gzclose(stream);
64
65     return 0;
66 }