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