Build on macosx.
[platform/upstream/rpm.git] / rpm2cpio.c
1 /* rpmarchive: spit out the main archive portion of a package */
2
3 #include "system.h"
4 const char *__progname;
5
6 #include <rpmlib.h>
7 #include <rpmpgp.h>
8
9 #include "depends.c"
10
11 #include "debug.h"
12
13 int main(int argc, char **argv)
14 {
15     FD_t fdi, fdo;
16     Header h;
17     char * rpmio_flags;
18     rpmRC rc;
19     FD_t gzdi;
20     
21     setprogname(argv[0]);       /* Retrofit glibc __progname */
22     if (argc == 1)
23         fdi = fdDup(STDIN_FILENO);
24     else
25         fdi = Fopen(argv[1], "r.ufdio");
26
27     if (Ferror(fdi)) {
28         fprintf(stderr, "%s: %s: %s\n", argv[0],
29                 (argc == 1 ? "<stdin>" : argv[1]), Fstrerror(fdi));
30         exit(EXIT_FAILURE);
31     }
32     fdo = fdDup(STDOUT_FILENO);
33
34     {   rpmts ts = rpmtsCreate();
35         rpmVSFlags vsflags = 0;
36
37         /* XXX retain the ageless behavior of rpm2cpio */
38         vsflags |= _RPMVSF_NODIGESTS;
39         vsflags |= _RPMVSF_NOSIGNATURES;
40         vsflags |= RPMVSF_NOHDRCHK;
41         (void) rpmtsSetVSFlags(ts, vsflags);
42
43         /*@-mustmod@*/      /* LCL: segfault */
44         rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
45         /*@=mustmod@*/
46
47         ts = rpmtsFree(ts);
48     }
49
50     switch (rc) {
51     case RPMRC_OK:
52     case RPMRC_NOKEY:
53     case RPMRC_NOTTRUSTED:
54         break;
55     case RPMRC_NOTFOUND:
56         fprintf(stderr, _("argument is not an RPM package\n"));
57         exit(EXIT_FAILURE);
58         break;
59     case RPMRC_FAIL:
60     default:
61         fprintf(stderr, _("error reading header from package\n"));
62         exit(EXIT_FAILURE);
63         break;
64     }
65
66     /* Retrieve type of payload compression. */
67     {   const char * payload_compressor = NULL;
68         char * t;
69
70         if (!headerGetEntry(h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
71                             (void **) &payload_compressor, NULL))
72             payload_compressor = "gzip";
73         rpmio_flags = t = alloca(sizeof("r.gzdio"));
74         *t++ = 'r';
75         if (!strcmp(payload_compressor, "gzip"))
76             t = stpcpy(t, ".gzdio");
77         if (!strcmp(payload_compressor, "bzip2"))
78             t = stpcpy(t, ".bzdio");
79     }
80
81     gzdi = Fdopen(fdi, rpmio_flags);    /* XXX gzdi == fdi */
82     if (gzdi == NULL) {
83         fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
84         exit(EXIT_FAILURE);
85     }
86
87     rc = ufdCopy(gzdi, fdo);
88     rc = (rc <= 0) ? EXIT_FAILURE : EXIT_SUCCESS;
89     Fclose(fdo);
90
91     Fclose(gzdi);       /* XXX gzdi == fdi */
92
93     return rc;
94 }