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