implement abstract fd type almost everywhere.
[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 "rpmlib.h"
6
7 char *zlib_err [] = {
8    "No",
9    "Unix",
10    "Data",
11    "Memory",
12    "Buffer",
13    "Version"
14 };
15
16 int main(int argc, char **argv)
17 {
18     FD_t fdi, fdo;
19     Header hd;
20     int rc, isSource;
21     char buffer[1024];
22     int ct;
23     gzFile stream;
24     
25     if (argc == 1) {
26         fdi = fdDup(0);
27     } else {
28         fdi = fdOpen(argv[1], O_RDONLY, 0644);
29     }
30
31     if (fdFileno(fdi) < 0) {
32         perror("cannot open package");
33         exit(1);
34     }
35     fdo = fdDup(1);
36
37     rc = rpmReadPackageHeader(fdi, &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(fdFileno(fdi), "r");
47
48     while ((ct = gzread(stream, &buffer, 1024)) > 0) {
49         fdWrite(fdo, &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 }