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