home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / misc / main.cpp < prev    next >
C/C++ Source or Header  |  1993-11-17  |  6KB  |  194 lines

  1. /*
  2.     FIPS - the First nondestructive Interactive Partition Splitting program
  3.  
  4.     Module main.cpp
  5.  
  6.     RCS - Header:
  7.     $Header: c:/daten/c/fips/source/cpp/RCS/main.cpp 0.9.1.1 1993/11/17 17:51:10 schaefer Exp schaefer $
  8.  
  9.     Copyright (C) 1993 Arno Schaefer
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25.  
  26.     Report problems and direct all questions to:
  27.  
  28.     schaefer@rbg.informatik.th-darmstadt.de
  29. */
  30.  
  31. #include <stdlib.h>
  32. #include "logdr_st.h"
  33. #include "global.h"
  34. #include "input.h"
  35. #include "fat.h"
  36. #include "fipsspec.h"
  37.  
  38. void main (int argc,char *argv[])
  39. {
  40.     evaluate_argument_vector (argc,argv);
  41.  
  42.     atexit (exit_function);
  43.  
  44.     if (global.debug_mode) global.open_debugfile(argc,argv);
  45.  
  46.     if (!global.quiet_mode) notice();
  47.  
  48.     int drive_number;
  49.     if (global.drive_number_cmdline) drive_number = global.drive_number_cmdline;
  50.     else drive_number = ask_for_drive_number ();
  51.  
  52.     fips_harddrive harddrive (drive_number);
  53.  
  54.     if (harddrive.errorcode)
  55.         error ("Error reading Drive Geometry: Errorcode %u",harddrive.errorcode);
  56.  
  57.     harddrive.reset();
  58.  
  59.     if (harddrive.errorcode)
  60.         error ("Drive Initialization Failure: Errorcode %u",harddrive.errorcode);
  61.  
  62.     if (harddrive.rootsector->read ())
  63.         error ("Error reading Root Sector");
  64.  
  65.     if (global.debug_mode)
  66.     {
  67.         fprintf (global.debugfile,"\nRoot Sector Drive %02Xh:\n\n",drive_number);
  68.         hexwrite (harddrive.rootsector->data,512,global.debugfile);
  69.     }
  70.  
  71.     fips_partition *partition;
  72.  
  73.     while (true)
  74.     {
  75.         fips_harddrive hd = harddrive;
  76.  
  77.         hd.partition_table().get (hd.rootsector);
  78.         if (!global.quiet_mode)
  79.         {
  80.             printx ("\nPartition Table:\n\n");
  81.             hd.print_partition_table ();
  82.         }
  83.         hd.check();
  84.  
  85.         int partition_number;
  86.         if (global.partition_number_cmdline) partition_number = global.partition_number_cmdline - 1;
  87.         else partition_number = ask_for_partition_number (hd.partition_table().partition_info);
  88.  
  89.         partition = new fips_partition (&hd,partition_number);
  90.  
  91.         int system = partition->partition_info->system;
  92.         if (system == 5)
  93.             error ("Can't split extended Partitions");
  94.         if (system == 0)
  95.             error ("Invalid Partition selected: %u",partition_number + 1);
  96.         if ((system != 1) && (system != 4) && (system != 6))
  97.             error ("Unknown Filesystem: %02Xh",system);
  98.  
  99.         if (partition->bootsector->read ())
  100.             error ("Error reading Boot Sector");
  101.  
  102.         if (global.debug_mode)
  103.         {
  104.             fprintf (global.debugfile,"\nBoot Sector Drive %02Xh, Partition %u:\n\n",hd.number,partition->number + 1);
  105.             hexwrite (partition->bootsector->data,512,global.debugfile);
  106.         }
  107.  
  108.         partition->bpb().get (partition->bootsector);
  109.  
  110.         if (!global.quiet_mode)
  111.         {
  112.             printx ("\nBootsector:\n\n");
  113.             partition->print_bpb ();
  114.         }
  115.  
  116.         partition->info().get (partition->bpb());
  117.         if (global.debug_mode) partition->write_info_debugfile ();
  118.  
  119.         partition->check();
  120.  
  121.         fat16 fat1 (partition,1);
  122.         fat16 fat2 (partition,2);
  123.  
  124.         fat1.check_against (&fat2);
  125.  
  126.         dword new_part_min_sector = partition->info().start_data + (dword) 4085 * partition->bpb().sectors_per_cluster;
  127.         dword new_part_min_cylinder = (new_part_min_sector + partition->partition_info->start_sector_abs - 1) / (hd.geometry.heads * hd.geometry.sectors) + 1;
  128.  
  129.         if (new_part_min_cylinder > partition->partition_info->end_cylinder)
  130.             error ("Partition too small - can't split");
  131.  
  132.         if (ask_if_save()) save_root_and_boot(&hd,partition);
  133.  
  134.         dword new_start_cylinder;
  135.         if (global.new_start_cylinder_cmdline)
  136.         {
  137.             new_start_cylinder = global.new_start_cylinder_cmdline;
  138.             if ((new_start_cylinder < new_part_min_cylinder) || (new_start_cylinder > partition->partition_info->end_cylinder))
  139.                 error ("Invalid new start cylinder: %lu",new_start_cylinder);
  140.         }
  141.         else new_start_cylinder = ask_for_new_start_cylinder (new_part_min_cylinder,partition->partition_info->end_cylinder);
  142.  
  143.         fat2.check_empty (new_start_cylinder * hd.geometry.heads * hd.geometry.sectors - partition->partition_info->start_sector_abs);
  144.  
  145.         hd.calculate_new_root (new_start_cylinder,partition_number);
  146.  
  147.         hd.partition_table().put(hd.rootsector);
  148.  
  149.         hd.partition_table().get (hd.rootsector);
  150.         if (!global.quiet_mode)
  151.         {
  152.             printx ("\nNew Partition Table:\n\n");
  153.             hd.print_partition_table ();
  154.         }
  155.         hd.check();
  156.  
  157.         if (ask_if_continue ())
  158.         {
  159.             harddrive = hd;
  160.             break;
  161.         }
  162.     }
  163.  
  164.     partition->calculate_new_boot ();
  165.  
  166.     partition->bpb().put (partition->bootsector);
  167.  
  168.     partition->bpb().get (partition->bootsector);
  169.  
  170.     if (!global.quiet_mode)
  171.     {
  172.         printx ("\nNew Bootsector:\n\n");
  173.         partition->print_bpb ();
  174.     }
  175.  
  176.     partition->info().get (partition->bpb());
  177.     if (global.debug_mode) partition->write_info_debugfile ();
  178.  
  179.     partition->check();
  180.  
  181.     if (!global.test_mode)
  182.     {
  183.         ask_for_write_permission();
  184.  
  185.         if (harddrive.rootsector->write())
  186.             error ("Error writing Root Sector");
  187.  
  188.         if (partition->bootsector->write ())
  189.             error ("Error writing Boot Sector");
  190.  
  191.         if (!global.quiet_mode) printx ("Repartitioning complete\n");
  192.     }
  193. }
  194.