home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / rpm / rpm2cpio.c < prev    next >
C/C++ Source or Header  |  1997-09-17  |  1KB  |  69 lines

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