• 大小: 195KB
    文件类型: .gz
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: C/C++
  • 标签:

资源简介

自己学习C语言时写的一个小有规模的程序,请大家看看,提提意见。

资源截图

代码片段和文件信息

#define  STRING_CHAR
#define  STRING_TO_NUM
#include “dpt.h“
#include “partition.h“
#include “para.h“
#include “error.h“
#include “sector-rw.h“
extern char *argp[];
extern char *devname;
extern pts  *pt_save;
/*
 *change boot partition to the specified partition.
 *if it is successful return 0;
 *if there is no mathched partition return -ENOMATCHPAR.
 *if failedreturn a negative number for error .
 */
int do_change_boot(char *devnameunsigned char partition_num)
{
  int iretfound=0;
  char *diskbuf;
  unsigned long secrel_sec;
  pt pt1[PT_HOLES];
  ret=get_pt(devname);
  if(ret<0) return ret;
  /*检查分区号码是否有效,启动分区只能是1-4. */
  if(partition_num> PT_HOLES || partition_num < 1 ) {
printf(“ the partition number is invalid.\n“) ;
printf(“ the valid number is from 1 to 4 .\n“);
return 0;
  }
  /*如果要改变的分区已经是启动分区,返回。*/
  if((pt_save+partition_num)->partition.active==0x80) 
return 0;
  diskbuf=malloc(BLKSIZE);
  if(diskbuf==NULL) return -ENOMEM;
  /*保存在分区表里的rel_sec此为判断的依据。 */
  rel_sec=(pt_save+partition_num)->partition.rel_sec;
  sec=MBR_SECTOR;/* 所在扇区只能是MBR即第0扇区。*/
  ret=read_sec_to_buff(devnamesec1diskbuf);
  if(ret <= 0) {
free(diskbuf);
return ret;
  }
  for(i=0;i memcpy (&pt1[i] \
(pt *)&diskbuf[PARTITION_TABLE_START + i*sizeof(pt)] \
sizeof(pt));
/*两者相等,我们才认为是同一分区且此分区扇区数必须大于零而且分区id不为空,否则不修改。*/
if(pt1[i].rel_sec==rel_sec && pt1[i].nr_sec > 0 && pt1[i].id != 0x00){
  found=1;               
  diskbuf[PARTITION_TABLE_START + i*sizeof(pt)+offset(ptactive)]=0x80; 
}
/*其他的启动分区改为非启动分区,因为只能有一个启动分区。*/
else if(pt1[i].active==0x80) {
  diskbuf[PARTITION_TABLE_START + i*sizeof(pt)+offset(ptactive)]=0x00; 
}
  }
  if(found) {/*找到了,才真正进行修改,否则返回。*/
ret=write_buff_to_sec(diskbufBLKSIZEdevnamesec);
free(diskbuf);
return ret; 
  }
  free(diskbuf);
  return -ENOMATCHPAR;/*没有找到匹配的分区,返回。 */
}
/*
 *parse the parameters or get the partition numberand 
 *then call the function to do the real work.
 */
int change_boot( )
{
  int t1ret;
  char *pnum=argp[0];
  unsigned char par_num;
  t1=parse_para(pnum);
  if(t1==DEC) {
par_num=strtoul(pnum&pnum10);
  }
  else {
printf(“ Please input the partition number you want to boot from :\n“);
printf(“ The valid number is from 1 to 4.\n“);
par_num=get_dec_num( );
  }
  ret=do_change_boot(devnamepar_num);
  if(ret<0) {
printf(“Failed to change partition %d to boot partition ! \n“par_num);
return ret;
  }
  printf(“ The partition %d has changed to boot partition successfully.\n“par_num);
  return ret;
}


评论

共有 条评论

相关资源