Use #include <x.h> syntax to include public headers.
[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 <rpmts.h>
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              /* LCL: segfault */
44         rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
45
46         ts = rpmtsFree(ts);
47     }
48
49     switch (rc) {
50     case RPMRC_OK:
51     case RPMRC_NOKEY:
52     case RPMRC_NOTTRUSTED:
53         break;
54     case RPMRC_NOTFOUND:
55         fprintf(stderr, _("argument is not an RPM package\n"));
56         exit(EXIT_FAILURE);
57         break;
58     case RPMRC_FAIL:
59     default:
60         fprintf(stderr, _("error reading header from package\n"));
61         exit(EXIT_FAILURE);
62         break;
63     }
64
65     /* Retrieve type of payload compression. */
66     {   const char * payload_compressor = NULL;
67         char * t;
68
69         if (!headerGetEntry(h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
70                             (void **) &payload_compressor, NULL))
71             payload_compressor = "gzip";
72         rpmio_flags = t = alloca(sizeof("r.gzdio"));
73         *t++ = 'r';
74         if (!strcmp(payload_compressor, "gzip"))
75             t = stpcpy(t, ".gzdio");
76         if (!strcmp(payload_compressor, "bzip2"))
77             t = stpcpy(t, ".bzdio");
78     }
79
80     gzdi = Fdopen(fdi, rpmio_flags);    /* XXX gzdi == fdi */
81     if (gzdi == NULL) {
82         fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
83         exit(EXIT_FAILURE);
84     }
85
86     rc = ufdCopy(gzdi, fdo);
87     rc = (rc <= 0) ? EXIT_FAILURE : EXIT_SUCCESS;
88     Fclose(fdo);
89
90     Fclose(gzdi);       /* XXX gzdi == fdi */
91
92     return rc;
93 }