⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcompute.c

📁 Fdisk 1.2.1 Freedos下硬盘分区软件
💻 C
📖 第 1 页 / 共 4 页
字号:
  int swap;

  int drive=flags.drive_number-0x80;

  Partition_Table *pDrive = &part_table[drive];


  long free_space_after_last_used_partition=0;
  long free_space_before_first_used_partition=0;
  long free_space_between_partitions_0_and_1=0;
  long free_space_between_partitions_1_and_2=0;
  long free_space_between_partitions_2_and_3=0;

  int pri_part_physical_order[4];
#ifdef DEBUG
  unsigned long cylinder_size = (pDrive->total_head+1)*(pDrive->total_sect);
#endif

  /* Reset the physical order to default */
  index=0;
  do
    {
    pri_part_physical_order[index]=index;

    index++;
    }while(index<4);

  /* Determine the location and size of the largest free space in the */
  /* primary partition.                                               */

  /* 1.  Sort the primary partitions based upon starting cylinder and their*/
  /*     contents...or lack of use.                                        */

  /* Place all empty partitions at the end of the table. */
  index=0;
  do
    {

    sub_index=0;
    do
      {
      if(pDrive->pri_part[pri_part_physical_order[sub_index]].num_type==0)
	{
	swap = pri_part_physical_order[sub_index];
	pri_part_physical_order[sub_index]
	 = pri_part_physical_order[(sub_index+1)];
	pri_part_physical_order[(sub_index+1)]=swap;
	}
      sub_index++;
      }while(sub_index<3);

    index++;
    }while(index<4);

#ifdef DEBUG
  if(debug.determine_free_space==TRUE)
    {
    Clear_Screen(NULL);
    Print_Centered(0,"Determine_Free_Space(int drive) debugging screen 1",BOLD);

    printf("\n\nCylinder Size (total heads * total sectors)=%d\n",cylinder_size);

    printf("\nContents after initial sorting of unused partitions to end:\n\n");

    index=0;
    do
      {
      printf("Partition %1d:  %1d    ",index,pri_part_physical_order[index]);
      printf("SC:  %4d    ",pDrive->pri_part[pri_part_physical_order[index]].start_cyl);
      printf("EC:  %4d    ",pDrive->pri_part[pri_part_physical_order[index]].end_cyl);
      printf("Size in MB:  %4d\n",pDrive->pri_part[pri_part_physical_order[index]].size_in_MB);

      index++;
      }while(index<4);
    Position_Cursor(4,20);
    Pause();
    }
#endif

  /* Order the partitions based upon starting cylinder */
  index=0;
  do
    {
    sub_index=0;
    do
      {
      if( (pDrive->pri_part[pri_part_physical_order[sub_index]].num_type != 0)
       && (pDrive->pri_part[pri_part_physical_order[(sub_index+1)]].num_type!=0)
       && (pDrive->pri_part[pri_part_physical_order[sub_index]].start_cyl
       > pDrive->pri_part[pri_part_physical_order[(sub_index+1)]].start_cyl) )
	{
	swap=pri_part_physical_order[sub_index];
	pri_part_physical_order[sub_index]=pri_part_physical_order[(sub_index+1)];
	pri_part_physical_order[(sub_index+1)]=swap;
	}
      sub_index++;
      }while(sub_index<3);
    index++;
    }while(index<4);

#ifdef DEBUG
  if(debug.determine_free_space==TRUE)
    {
    Clear_Screen(NULL);
    Print_Centered(0,"Determine_Free_Space(int drive) debugging screen 2",BOLD);

    printf("\n\nCylinder Size (total heads * total sectors)=%d\n",cylinder_size);
    printf("\nContents after sorting partitions by starting cylinder:\n\n");

    index=0;
    do
      {
      printf("Partition %d:  %1d    ",index,pri_part_physical_order[index]);
      printf("SC:  %4d    ",pDrive->pri_part[pri_part_physical_order[index]].start_cyl);
      printf("EC:  %4d    ",pDrive->pri_part[pri_part_physical_order[index]].end_cyl);
      printf("Size in MB:  %4d\n",pDrive->pri_part[pri_part_physical_order[index]].size_in_MB);

      index++;
      }while(index<4);
    }
#endif

  /* 2.  Is there any free space before the first partition? */

  /* Find the first used partition and the last used partition. */
  index=0;
  do
    {
    if( (first_used_partition==UNUSED)
     && (pDrive->pri_part[pri_part_physical_order[index]].num_type > 0) )
      {
      first_used_partition=index;
      }

    if(pDrive->pri_part[pri_part_physical_order[index]].num_type > 0)
      {
      last_used_partition=index;
      }

    index++;
    }while(index<4);

  if(first_used_partition!=UNUSED)
    {
    if(pDrive->pri_part[pri_part_physical_order[first_used_partition]].start_cyl > 0)
      {
      free_space_before_first_used_partition
       =(pDrive->pri_part[pri_part_physical_order[first_used_partition]].start_cyl)-1;
      }
    else free_space_before_first_used_partition=0;
    }

  /* 3.  Is there any free space after the last used partition? */
  if(first_used_partition!=UNUSED)
    {
    if(pDrive->pri_part[pri_part_physical_order[last_used_partition]].end_cyl
     <= pDrive->total_cyl)
      {
      free_space_after_last_used_partition
       =(pDrive->total_cyl-pDrive->pri_part
       [pri_part_physical_order[last_used_partition]].end_cyl)-1;

      if(free_space_after_last_used_partition < 0)
       free_space_after_last_used_partition = 0;
      }
    }

  /* 4.  Is there any free space between partitions?                    */
  /*                                                                    */
  if( (first_used_partition!=UNUSED) && (last_used_partition>=1) )
    {
    if( (pDrive->pri_part[pri_part_physical_order[0]].end_cyl+1)
     <(pDrive->pri_part[pri_part_physical_order[1]].start_cyl) )
      {
      free_space_between_partitions_0_and_1
       =(pDrive->pri_part[pri_part_physical_order[1]].start_cyl
       -pDrive->pri_part[pri_part_physical_order[0]].end_cyl)-2;
      }
    }

  if( (first_used_partition!=UNUSED) && (last_used_partition>=2) )
    {
    if( (pDrive->pri_part[pri_part_physical_order[1]].end_cyl+1)
     <(pDrive->pri_part[pri_part_physical_order[2]].start_cyl) )
      {
      free_space_between_partitions_1_and_2
       =(pDrive->pri_part[pri_part_physical_order[2]].start_cyl
       -pDrive->pri_part[pri_part_physical_order[1]].end_cyl)-2;
      }
    }

  if( (first_used_partition!=UNUSED) && (last_used_partition==3) )
    {
    if( (pDrive->pri_part[pri_part_physical_order[2]].end_cyl+1)
     <(pDrive->pri_part[pri_part_physical_order[3]].start_cyl) )
      {
      free_space_between_partitions_2_and_3
       =(pDrive->pri_part[pri_part_physical_order[3]].start_cyl
       -pDrive->pri_part[pri_part_physical_order[2]].end_cyl)-2;
      }
    }

  /* Locate the largest free space */
  if(first_used_partition!=UNUSED)
    {
    /* */
    pDrive->pp_largest_free_space_start_head=0;
    pDrive->pp_largest_free_space_start_sect=1;

    /* Default the largest free space to before the first used partition */
    pDrive->pri_part_largest_free_space
     =free_space_before_first_used_partition;
    pDrive->pp_largest_free_space_start_cyl=0;
    pDrive->pp_largest_free_space_end_cyl
     =pDrive->pri_part[pri_part_physical_order[first_used_partition]].start_cyl-1;

    /* If the largest free space is not before the first used partition  */
    /* make the correct adjustments.                                     */
    if(free_space_after_last_used_partition
     >pDrive->pri_part_largest_free_space)
      {
      pDrive->pri_part_largest_free_space
       =free_space_after_last_used_partition;
      pDrive->pp_largest_free_space_start_cyl
       =pDrive->pri_part[pri_part_physical_order[last_used_partition]].end_cyl+1;
      pDrive->pp_largest_free_space_end_cyl
       =pDrive->total_cyl;
      }

    if(free_space_between_partitions_0_and_1
     > pDrive->pri_part_largest_free_space)
      {
      pDrive->pri_part_largest_free_space
       =free_space_between_partitions_0_and_1;
      pDrive->pp_largest_free_space_start_cyl
       =pDrive->pri_part[pri_part_physical_order[0]].end_cyl+1;
      pDrive->pp_largest_free_space_end_cyl
       =pDrive->pri_part[pri_part_physical_order[1]].start_cyl-1;
      }

    if(free_space_between_partitions_1_and_2
     > pDrive->pri_part_largest_free_space)
      {
      pDrive->pri_part_largest_free_space
       =free_space_between_partitions_1_and_2;
      pDrive->pp_largest_free_space_start_cyl
       =pDrive->pri_part[pri_part_physical_order[1]].end_cyl+1;
      pDrive->pp_largest_free_space_end_cyl
       =pDrive->pri_part[pri_part_physical_order[2]].start_cyl-1;
      }

    if(free_space_between_partitions_2_and_3
     > pDrive->pri_part_largest_free_space)
      {
      pDrive->pri_part_largest_free_space
       =free_space_between_partitions_2_and_3;
      pDrive->pp_largest_free_space_start_cyl
       =pDrive->pri_part[pri_part_physical_order[2]].end_cyl+1;
      pDrive->pp_largest_free_space_end_cyl
       =pDrive->pri_part[pri_part_physical_order[3]].start_cyl-1;
      }
    }
  else
    {
    pDrive->pri_part_largest_free_space=pDrive->total_cyl;
    pDrive->pp_largest_free_space_start_cyl=0;
    pDrive->pp_largest_free_space_end_cyl=pDrive->total_cyl;
    }

  /* Make final adjustments to the computed free space size. */
  if(pDrive->pri_part_largest_free_space<=0)
   pDrive->pri_part_largest_free_space=0;

#ifdef DEBUG
  if(debug.determine_free_space==TRUE)
    {
    printf("\n\nFree space (in cylinders) in primary partition table:\n");
    printf("\nFree space before first used partition:  %d",free_space_before_first_used_partition);
    printf("\nFree space after last used partition:  %d",free_space_after_last_used_partition);
    printf("\nFree space between partitions 0 and 1:  %d",free_space_between_partitions_0_and_1);
    printf("\nFree space between partitions 1 and 2:  %d",free_space_between_partitions_1_and_2);
    printf("\nFree space between partitions 2 and 3:  %d",free_space_between_partitions_2_and_3);
    printf("\n\nLargest free space in primary partition table:  %d",pDrive->pri_part_largest_free_space);
    printf("\nStarting cylinder of largest free space:  %d",pDrive->pp_largest_free_space_start_cyl);
    printf("\nEnding cylinder of largest free space:  %d",pDrive->pp_largest_free_space_end_cyl);
    Pause();
    }
#endif

  /* Determine the location and size of the largest free space in the */
  /* extended partition, if it exists.                                */
  if(pDrive->ptr_ext_part)
    {
    pDrive->ext_part_largest_free_space=0;
    pDrive->log_drive_free_space_start_cyl=0;
    pDrive->log_drive_free_space_end_cyl=0;
    pDrive->log_drive_largest_free_space_location=0;

#ifdef DEBUG
    if(debug.determine_free_space==TRUE)
      {
      Clear_Screen(NULL);
      Print_Centered(0,"Determine_Free_Space(int drive) debugging screen 3",BOLD);
      printf("\n\n");
      }
#endif

    if(pDrive->num_of_log_drives>0)
      {
      /* If there are logical drives in the extended partition first find  */
      /* the largest free space between the logical drives...if it exists. */
      last_used_partition=UNUSED;
      index=0;

      /* Check to see if the first possible entry in the extended partition */
      /* is unused.  If it is unused and there is a logical drive after it  */
      /* then skip checking for free space between entry 0 and 1.           */
      if(pDrive->ptr_ext_part[0].num_type==0) index=1;

      do
	{
	if(pDrive->log_drive[index].num_type > 0)
	  {
	  last_used_partition=index;
	  }

	if( (pDrive->log_drive[(index+1)].start_cyl
	 -pDrive->log_drive[index].end_cyl) > 1 )
	  {
	  pDrive->ext_part_largest_free_space
	   =(pDrive->log_drive[(index+1)].start_cyl-1)
	   -(pDrive->log_drive[index].end_cyl + 1 );
	  pDrive->log_drive_free_space_start_cyl
	   =pDrive->log_drive[index].end_cyl + 1;
	  pDrive->log_drive_free_space_end_cyl
	   =pDrive->log_drive[(index+1)].start_cyl - 1;
	  pDrive->log_drive_largest_free_space_location=index+1;
	  }

#ifdef DEBUG
	if(debug.determine_free_space==TRUE)
	  {
	  if(index==12)
	    {
	    printf("\n");
	    Pause();
	    }

	  printf("\nLogical Drive #: %2d    ",index);
	  printf("SC: %4d    ",pDrive->log_drive[index].start_cyl);
	  printf("EC: %4d    ",pDrive->log_drive[index].end_cyl);
	  }
#endif

	index++;
	}while(index<22);

#ifdef DEBUG
      if(debug.determine_free_space==TRUE)
	{
	printf("\nLogical Drive #: %2d    ",index);
	printf("SC: %4d    ",pDrive->log_drive[22].start_cyl);
	printf("EC: %4d    \n",pDrive->log_drive[22].end_cyl);
	Pause();

	Clear_Screen(NULL);
	Print_Centered(0,"Determine_Free_Space(int drive) debugging screen 4",BOLD);
	printf("\n\nNote:  All values are in cylinders.\n\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -