2 * rpmunpack for busybox
4 * rpmunpack.c - Utility program to unpack an RPM archive
6 * Gero Kuhlmann <gero@gkminix.han.de> 1998
8 * This program is public domain software; you can do whatever you like
9 * with this source, including modifying and redistributing it.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 * Some general definitions
23 #define RPM_MAGIC "\355\253\356\333"
24 #define GZ_MAGIC_1 '\037'
25 #define GZ_MAGIC_2 '\213'
30 static char buffer[BUFSIZE];
31 static char *progname;
32 static int infile, outfile;
35 * Read a specified number of bytes from input file
37 static void myread(int num)
41 if ((err = read(infile, buffer, num)) != num) {
43 perror_msg_and_die(progname);
45 error_msg_and_die("Unexpected end of input file!\n");
52 int rpmunpack_main(int argc, char **argv)
56 /* Get our own program name */
57 if ((progname = strrchr(argv[0], '/')) == NULL)
62 /* Check for command line parameters */
63 if (argc>=2 && *argv[1]=='-') {
64 usage(rpmunpack_usage);
69 infile = STDIN_FILENO;
70 else if ((infile = open(argv[1], O_RDONLY)) < 0)
71 perror_msg_and_die("%s", argv[1]);
73 /* Read magic ID and output filename */
75 if (strncmp(buffer, RPM_MAGIC, 4)) {
76 fprintf(stderr, "Input file is not in RPM format!\n");
79 myread(6); /* Skip flags */
83 /* Open output file */
84 strcat(buffer, ".cpio.gz");
85 if (infile == STDIN_FILENO)
86 outfile = STDOUT_FILENO;
87 else if ((outfile = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
88 perror_msg_and_die("%s", buffer);
91 * Now search for the GZIP signature. This is rather awkward, but I don't
92 * know any other way how to find out the exact starting position of the
93 * archive within the input file. There are a couple of data structures
94 * and texts (obviously descriptions, installation shell scripts etc.)
95 * coming before the archive, but even they start at different offsets
96 * with different RPM files. However, it looks like the GZIP signature
97 * never appears before offset 0x200, so we skip these first couple of
98 * bytes to make the signature scan a little more reliable.
103 if (status == 0 && buffer[0] == GZ_MAGIC_1)
105 else if (status == 1 && buffer[0] == GZ_MAGIC_2)
110 buffer[0] = GZ_MAGIC_1;
111 buffer[1] = GZ_MAGIC_2;
112 if (write(outfile, buffer, 2) < 0)
113 perror_msg_and_die("write");
115 /* Now simply copy the GZIP archive into the output file */
116 while ((len = read(infile, buffer, BUFSIZE)) > 0) {
117 if (write(outfile, buffer, len) < 0)
118 perror_msg_and_die("write");
121 perror_msg_and_die("read");