请你编程

Author: 杨峰 Date: 1997-01-01

#3  题目:设计一个小的软件安装程序。
  一般说来,安装应具有以下功能:
  一、改变安装路径;
  二、在安装过程中给出提示帮助信息;
  三、可随时中断退出安装;
  四、较强的容错能力,可自动测试目标盘容量是否足够安装,当安装路径已经存在时能够给出提示;
  五、可以判断是否多盘安装,确定安装盘序号;
  六、可以显示安装进度;
  七、能安装成多级子目录。
#2  子程序一:
  main(int argc,char *argv[])
  {int done,lenth,k,ch;
  struct ffblk f;
  char str[4],path[128],pathname[128],message[80];/*确定安装盘所在驱动器*/
  if (argv[0][0]=='a' || argv[0][0]=='A') setdisk(0);
  else if (argv[0][0]=='b' || argv[0][0]=='B') setdisk(1);
  else {printf("请从安装盘进行安装 !");exit(0);}
  win(1,1,80,25,YELLOW,BLUE);
  gotoxy(2,3);printf(" 9705“请你编程”安装程序");
  gotoxy(2,4);printf("==========");
  win(1,25,80,25,0,3);
  printf("Enter确认   Ese退出安装程序");
  window(1,1,80,25);
  gotoxy(10,10);printf("请输入安装目标路径:");
  strcpy(path,PATH);
  do {if (getstr(10,70,12,0,7,path)==283) quit();  
  lenth=strlen(path);  
  if(mkdir(path))   
  if(! findfirst(path,&f,55))    
  {if ((f.ff-attrib&0x10)==16)     
  {strcpy(message,"子目录已经存在,继续安装吗(Y/N)?");       
  ch=wind(14,16,66,22,0,7,"提示信息",message);       
  if (ch!=5497 && ch!=5465) quit (0);       
  else done=0;}    
  else {     
  strcpy(message,"存在同名文件,不能建立子目录,按任意键");       
  wind(10,16,70,22,0,7," 错误信息 ",message);       
  done=1;}}    
  else if ((lenth==3 && path[2]=='\\')||(lenth==2 && path[1]==':'))      
  done=0;/*输入的路径为根目录*/    
  else { strcpy(message,"路径输入错误,按任意键");      
  wind(20,16,60,22,0,7,"错误信息");      
  done=1;} else done=0;} while (done);  
  win(1,25,80,25,0,3);
  printf("正在进行安装,按Ese退出安装程序");
  win(1,10,80,24,YELLOW,BLUE);
  strcpy(pathname,path);/*在目标路径的下一级子目录lib中安装文件*/
  if(pathname[lenth-1]!="xlh\\"xlh) strcat(pathname,"\\");
  strcat(pathname,"lib");
  mkdir(pathname);
  install(pathname,"demo.hlp");
  install(pathname,"demo.dat");
  strcpy(pathname,path);/*在目标路径的下一级子目录cpp中安装文件*/
  if(pathname[lenth-1]!="xlh\\"xlh) strcat(pathname,"\\");
  strcat(pathname,"cpp");
  mkdir(pathname);
  done=findfirst("*.c",&f,55);
  while(!done) 
  { install(pathname,f.ff-name);   
  done=findnext(&f);}
  install(path,"setup.exe");/*在目标路径中安装文件*/
  install(path,"demo.exe");
  install(path,"readme");
  install(path,"egavga.bgi");
  win(1,5,80,25,1,1);
  strcpy(message,"系统安装完毕,按任意键返回");
  wind(14,10,66,16,0,7,"提示信息",message);
  win(1,1,80,25,7,0);
  return 0;}
#2  子程序二:可以显示程序安装进度,其中filenum为文件数,step等于370.0/filenum  
  for(i=1;i<=filenum;i++) 
  {  strcpy(&ffile[2],fl.ff-name);       
  strcpy(&dfile[8],fl.ff-name);   
  if((fp1=fopen(ffile,"rb"))==NULL) return 0;   
  if((fp2=fopen(dfile,"wb"))==NULL) return 0;   
  setfillstyle(SOLID-FILL,LIGHTGRAY);   
  bar(225,350,500,400);   
  biosputs(28,21,ffile,0x70);   
  biosputs(28,22,dfile,0x70);   
  length=0;   
  while(length<fl.ff-fsize)   
  /*拷贝一个文件*/ 
  { len=(length+1024)<fl.ff-fsize?1024:fl.ff-fsize-length;      
  fread(buf,len,1,fp1);      
  fwrite(buf,len,1,fp2);      
  length+=len;}      
  fclose(fp1);fclose(fp2);      
  setfillstyle(SOLID-FILL,YELLOW);      
  bar(128,414,128+step*i,431);/*用温度计演示拷贝过程*/      
  if(128+step*i<39*8) textattr(0x7f);      
  else textattr(0xe0);      
  gotoxy(39,24);cprintf("%2d%%",(i*100)/filenum);      
  findnext(&fl);}    
  chdir(mulu-name);    
  return 1;}
#2  子程序三:能比较当前磁盘剩余空间与当前拷贝文件大小,如果磁盘空间不足,显示错误   
  getdfree(getdisk()+1,&free);   
  if(info.ff-fsize>((long)free.df-avail*(long)free.df-bsec*(long)free.df-sclus))     
  { win(19,10,62,16,RED,YELLOW,2,YELLOW+BLINK);       
  gotoxy(19,2);       
  cputs("ERROR");       
  gotoxy(10,4);       
  cputs("The space of target disk isn`t enough");       
  gotoxy(10,5);       
  cputs("press any key to quit");       
  if(getkey())/*按任意键退出*/      
  exitinstall();}
#2  子程序四:可以实现多盘安装  
  strcat(file,"\\disk.id"); 
  if((fp=fopen(file,"r"))!==NULL)/*安装盘上有DISK.ID文件,为多盘安装;没有,只安装当前盘*/ 
  {i=1;  
  fscanf(fp,"%d%d",&number,&disk);/*读取磁盘标志*/  
  fclose(fp);  
  while(i<=number)  
  {win(28,20,53,24,GREEN,WHITE,2,WHITE);/*提示插入几号磁盘*/     
  gotoxy(2,3);   
  cprintf("insert %d# disk",i);   
  if(getkey()==ESC) exitinstall();      
  win(28,20,53,24,BLACK,WHITE,0,WHITE);      
  if(i!=1)      
  { if((fp=fopen(file,"r"))==NULL)       
  disk=-1;       
  else  fscanf(fp,"%d%d",&number,&disk);}   
  if(i!=disk)/*如果当前插入磁盘序列号不等于安装号,提示错误*/    
  { win(27,21,54,25,RED,YELLOW,2,YELLOW+BLINK);       
  gotoxy(2,2);      
  cprintf("The disk isn`t %d# disk",i);       
  gotoxy(2,3);       
  cprintf("insert %d# disk",i);       
  gotoxy(2,4);       
  cprintf("press any key to continue");       
  if(getkey()==ESC) exitinstall();       
  win(27,21,54,25,BLACK,WHITE,0,WHITE);}       
  else/*如果当前插入磁盘序列号等于安装号,安装*/      
  { win(13,13,68,18,GREEN,WHITE,0,WHITE);       
  process(sdriver,directory);       
  i++;} fclose(fp);} } 
  else /*单盘安装*/  
  { win(13,13,68,18,GREEN,WHITE,0,WHITE);      
  process(sdriver,directory);}
  chdir(s);