home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / dos / lread10.exe / MAIN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-24  |  3.6 KB  |  136 lines

  1. /* Here lies the main part of lread, a DOS program designed to read
  2.  * the Linux ext2 file system.  It was written in April of 1995 by a
  3.  * group of three:  Jason Hunter, Kris Hett, and David Lutz.
  4.  * It is the first really cool program produced for Ed Loyot's
  5.  * Operating Systems class.  Kris unfortunately had too little time
  6.  * available to devote to this project, so none of his code is included
  7.  * in this package.
  8. */
  9.  
  10. /* This file's contents were written by both Jason and Dave.
  11. */
  12.  
  13. /* Still to do: (you're welcome to help!)
  14.  *   add more robust modes--like for devices
  15.  *   allow chasing softlinks
  16.  *   make ldir dates shorter (like ls ???)
  17.  *   add more error handling
  18.  *   don't let it overwrite a DOS file ???
  19.  *
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <fcntl.h>
  24. #include <stdlib.h>
  25. #include <ctype.h>       /* for tolower() */
  26. #ifdef __TURBOC__
  27. #include <sys\stat.h>
  28. #include <io.h>          /* for creat/write/close */
  29. #else
  30. #include <sys/stat.h>
  31. #endif
  32. #include <string.h>
  33. #include "ext2.h"
  34. #include "proto.h"
  35.  
  36. #define DEBUG 0  /* true==1 */
  37.  
  38. void main( int argc, char **argv )
  39. {
  40.   inode *i;
  41.   byte buf[1024];
  42.   char argv1bak[1024];
  43.   int bytes, fd;
  44.   long inode_num;
  45.   int x;
  46.  
  47.   /* change the argv[0] to lowercase */
  48.   for( x=0; x < strlen( argv[0] ); x++ )
  49.     argv[0][x] = tolower( argv[0][x] );
  50.  
  51.  
  52.   if( argc < 2 )
  53.     if( strstr( argv[0], "ldir" ) != NULL )  /* See if a dir is requested */
  54.     {
  55.       printf( "format: ldir /LinuxPath/LinuxDir\n" );
  56.       exit(-1);
  57.     }
  58.     else                                     /* it was a lread */
  59.     {
  60.       printf( "format: lread /LinuxPath/LinuxFile [DosFile]\n" );
  61.       exit(-1);
  62.     }
  63.  
  64.  
  65. #ifdef __TURBOC__
  66.   _fmode = O_BINARY;   /* we want all files binary for this */
  67. #endif
  68.  
  69. #ifdef __TURBOC__
  70.   examine_drive();
  71. #else
  72.   open_dev( "/dev/hda4" );
  73. #endif
  74.  
  75.   if( load_super() == -1 )           /* Couldn't get superblock */
  76.     exit( -1 );
  77.   if (DEBUG) print_super();
  78.  
  79.   if( load_groups() == -1 )           /* Couldn't read groups */
  80.     exit( -1 );
  81.   if (DEBUG) print_groups();
  82.  
  83.   strcpy( argv1bak, argv[1] );
  84.   inode_num = eatpath( argv[1] );
  85.   if( inode_num == 0 ) exit( -1 );         /* Linux path was invalid */
  86.   i = load_inode( inode_num );
  87.   if( i == NULL ) exit( -1 );              /* Problem with the inode */
  88.  
  89.   if( strstr( argv[0], "ldir" ) != NULL )  /* See if a dir is requested */
  90.   {
  91.     list_dir( i );
  92.     exit( 1 );
  93.   }
  94.  
  95.   if (DEBUG) print_inode( i );
  96.   if (DEBUG) print_blocks( i );
  97.  
  98.   if( argc == 3)
  99.   {
  100.     fd = creat( argv[2], S_IREAD | S_IWRITE );
  101.     if (fd == -1)
  102.     {
  103.       perror( "Problem opening second argument file (DOS file)" );
  104.       exit( -1 );
  105.     }
  106.   }
  107.   else /* argc == 2 and we want output to stdout */
  108.   {
  109.     fd = 1;
  110.   }
  111.  
  112.  
  113.   /* No malloc needed since it's char buf[1024] */
  114.   bytes = read_inode( i, buf, 1024 ); /* read first block */
  115.   if( bytes == -1 ) exit( -1 ); /* problem reading */
  116.   do
  117.     {
  118.       write( fd, buf, bytes );    /* write to disk */
  119.       bytes = read_inode( NULL, buf, 1024 );
  120.       if( bytes == -1) exit( -1 ); /* problem reading */
  121.     } while( bytes != 0 );
  122.  
  123.   if( bytes == 0 && argc == 3 )
  124.     printf( "%s copied to %s\n",argv1bak, argv[2]);
  125.   else if( bytes == 0 && argc == 2 )
  126.     printf( "*** No errors ***\n" );
  127.   else if( bytes == -1 && argc == 3 )
  128.     printf( "Problem reading file... check %s for partial data.\n",argv[2]);
  129.   else if( bytes == -1 && argc == 2 )
  130.     printf( "*** Problem encountered reading file ***\n");
  131.   else
  132.     printf( "You should NEVER see this error message.  :-)\n");  
  133.     
  134.   close( fd );
  135. }
  136.