home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / tapes / cpio.c < prev    next >
Internet Message Format  |  1989-03-21  |  3KB

  1. From: Dan Kary <ihnp4!mgnetp!dicomed!ndsuvax!nckary@Ucb-Vax.ARPA>
  2. Newsgroups: net.sources
  3. Subject: "Public Domain cpio.c"
  4. Date: 12 Jun 85 13:32:32 GMT
  5.  
  6. A while back there was a discussion about reading cpio format tapes on
  7. 4.2BSD systems.  We lost news for a while and I never found out how or
  8. if the problem was resolved.  Faced with a need to read a cpio tape I
  9. finally decided to write something that would read cpio format.  It
  10. turned out to be a much smaller problem than I thought it would be.
  11. No guarantees about portability are made, but I was able to read my
  12. AT&T System V distribution tape on our VAX 11/780 running 4.2BSD with
  13. this program.  I hope this is useful to someone.
  14.                         
  15.                         Dan Kary
  16.  
  17. (one file only)
  18. --------------------- cut here -----------------------------------
  19. /* A public domain program by Daniel D. Kary
  20.  *                            300 Minard Hall
  21.  *                            North Dakota State University
  22.  *                            Fargo, ND   58105
  23.  *                            ihnp4!dicomed!ndsuvax!nckary
  24.  *
  25.  * This program will read cpio format files (tapes)
  26.  * use dd(1) to read the tape into a file then  'cpio file'
  27.  * to read the files out of the tape file.  This program will
  28.  * create directories and files using the correct mode and
  29.  * ignoring time, user and group info.  It is useful for reading
  30.  * distribution tapes (AT&T) on systems lacking cpio (Berkeley).
  31.  * The process's effective user ID must be super-user.
  32.  */
  33.  
  34. #include    <sys/types.h>
  35. #include    <sys/file.h>
  36. #include    <stdio.h>
  37.  
  38. struct header{  
  39.     ushort  magic_number,
  40.     device,
  41.     inode,
  42.     mode,
  43.     uid,
  44.     gid,
  45.     h_nlink,
  46.     dev_type,
  47.     modify_time[2],
  48.     name_length,
  49.     file_size[2];
  50. hdr;
  51.  
  52. char buf[10240], fname[512];
  53. int fd;
  54.  
  55. main(argc, argv)
  56. int argc;
  57. char *argv[];
  58. {
  59.     if(argc != 2){
  60.         fprintf(stderr,"Usage: cpio filename\n");
  61.         exit();
  62.     }
  63.     fd = open(argv[1], O_RDONLY);
  64.     while(fd > 0){
  65.         read_hdr();
  66.         if(strcmp("TRAILER!!!", fname) == 0) exit();
  67.     }
  68. }
  69.  
  70. read_hdr()
  71. {
  72.     char c;
  73.     int f, i, size;
  74.  
  75.     read(fd, &hdr.magic_number,2);
  76.     read(fd, &hdr.device,2);
  77.     read(fd, &hdr.inode,2);
  78.     read(fd, &hdr.mode,2);
  79.     read(fd, &hdr.uid,2);
  80.     read(fd, &hdr.gid,2);
  81.     read(fd, &hdr.h_nlink,2);
  82.     read(fd, &hdr.dev_type ,2);
  83.     read(fd, hdr.modify_time,4);
  84.     read(fd, &hdr.name_length,2);
  85.     read(fd, &hdr.file_size[1],2);
  86.     read(fd, &hdr.file_size[0],2);
  87.     i = hdr.name_length;
  88.     if(((i/2)*2) != i) i++;
  89.     read(fd, fname, i);
  90.     size = hdr.file_size[1] * 65536 + hdr.file_size[0];
  91.     if(((size/2)*2) != size) size++;
  92.     if(size == 0)
  93.         mkdir(fname, hdr.mode);
  94.     else{
  95.         f = open(fname, O_CREAT | O_WRONLY, hdr.mode);
  96.         while(size >= 10240){
  97.             read(fd, buf, 10240);
  98.             write(f, buf, 10240);
  99.             size -= 10240;
  100.         }
  101.         if(size > 0){
  102.             read(fd, buf, size);
  103.             write(f, buf, size);
  104.         close(f);
  105.         }
  106.     }
  107. }
  108.