home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / SRC / E2CAT.C < prev    next >
C/C++ Source or Header  |  1996-01-09  |  3KB  |  139 lines

  1. /***************************************************************************
  2.  * e2cp.c - Copy an ext2 file to standard out
  3.  *
  4.  * Copyright (C) 1995 Claus Tondering, ct@login.dknet.dk
  5.  * This file may be redistributed under the terms of the GNU Public License.
  6.  ***************************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12. #include <fcntl.h>
  13.  
  14. #include "ext2_fs.h"
  15. #include "ext2fs/ext2fs.h"
  16. #include "ldisk.h"
  17. #include "istat.h"
  18. #include "e2err.h"
  19.  
  20. extern io_manager msdos_io_manager;
  21.  
  22. char *blockbuf;
  23.  
  24. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  25.  
  26. /**********************************************************************
  27.  * usage prints usage information and exits
  28.  **********************************************************************/
  29.  
  30. void
  31. usage()
  32. {
  33.     fprintf(stderr, "usage: e2cat [-bt] file\n");
  34.     exit(1);
  35. }
  36.  
  37.  
  38. /**********************************************************************
  39.  * myproc is called once for every block in the file
  40.  **********************************************************************/
  41.  
  42. static int myproc(ext2_filsys fs,
  43.                   blk_t *blocknr,
  44.                   int blockcnt,
  45.                   void *private)
  46. {
  47.     int err;
  48.     struct ext2_inode *ino = private;
  49.     static nextblock = 0;
  50.  
  51.     if (blockcnt < 0)            /* We're reading an inderect block */
  52.         return 0;
  53.  
  54.     if (nextblock+1 < blockcnt)
  55.         memset(blockbuf, 0, fs->blocksize);
  56.  
  57.     while (nextblock++ < blockcnt)
  58.         if (fwrite(blockbuf, fs->blocksize, 1, stdout) == 0) {
  59.             fprintf(stderr,"Cannot write destination file\n");
  60.             return BLOCK_ABORT;
  61.         }
  62.  
  63.     err = io_channel_read_blk(fs->io, *blocknr, 1, blockbuf);
  64.     if (err) {
  65.         fprintf(stderr,"Cannot read source file\n");
  66.         return BLOCK_ABORT;
  67.     }
  68.  
  69.     if (fwrite(blockbuf, MIN(fs->blocksize, ino->i_size - blockcnt*fs->blocksize), 1, stdout) == 0) {
  70.         fprintf(stderr,"Cannot write destination file\n");
  71.         return BLOCK_ABORT;
  72.     }
  73.     
  74.     return 0;
  75. }
  76.  
  77.  
  78. /**********************************************************************
  79.  * main routine
  80.  **********************************************************************/
  81.  
  82. main(int argc, char **argv)
  83. {
  84.     int err, c;
  85.     char *src;
  86.     ext2_filsys fs;
  87.     ino_t ino;
  88.     struct ext2_inode e2ino;
  89.  
  90.     opterr = 0;
  91.     while ((c=getopt(argc, argv, "bt")) != -1) {
  92.         switch (c) {
  93.          case 'b': setmode(1, O_BINARY); break;
  94.          case 't': setmode(1, O_TEXT); break;
  95.          case '?': usage();
  96.         }
  97.     }
  98.  
  99.     if (argc!=optind+1)
  100.         usage();
  101.  
  102.     src = argv[optind];
  103.  
  104.  
  105.     /* Open file system */
  106.     err = ext2fs_open(0, 0, 0, 0, msdos_io_manager, &fs);
  107.     if (err)
  108.         e2_err("Cannot open ext2 file system",err);
  109.  
  110.     /* Allocate file buffer */
  111.     blockbuf = malloc(fs->blocksize);
  112.     if (!blockbuf) {
  113.         fprintf(stderr,"Cannot malloc buffer\n");
  114.         exit(1);
  115.     }
  116.  
  117.     /* Lookup specified name */
  118.     err = ext2fs_namei(fs, 2, cwdino, src, &ino);
  119.     if (err)
  120.         e2_err("Cannot find source file",err);
  121.  
  122.     /* Read specified inode */
  123.     err = ext2fs_read_inode(fs, ino, &e2ino);
  124.     if (err)
  125.         e2_err("Cannot read inode information",err);
  126.  
  127.     /* Is it a regular file? */
  128.     if (!S_ISREG(e2ino.i_mode)) {
  129.         fprintf(stderr,"%s is not a regular file\n",src);
  130.         exit(1);
  131.     }
  132.  
  133.     err = ext2fs_block_iterate(fs, ino, 0, 0, myproc, &e2ino);
  134.     if (err)
  135.         e2_err("Cannot read source file",err);
  136.  
  137.     return 0;
  138. }
  139.