📄 mini.c
字号:
}
short extractFromStr(char *str, char separateCh, char *strSet[])
{
char *p,*nextP;
short i;
if(!str)
{
printf("No string\n");
return -1;
}
p=str;
nextP=strchr(str,separateCh);
if(!nextP)
{
strSet[0]=(char *)malloc(strlen(str)+1);
memset(strSet[0],0,strlen(str)+1);
strcpy(strSet[0],str);
}
else
{
for(i=0;nextP;i++)
{
strSet[i]=(char *)malloc(nextP-p+1);
memset(strSet[i],0,nextP-p+1);
strncpy(strSet[i],p,nextP-p);
p=nextP+1;
nextP=strchr(p,separateCh);
}
strSet[i]=(char *)malloc(strlen(str)-(p-str)+1);
memset(strSet[i],0,strlen(str)-(p-str)+1);
strncpy(strSet[i],p,strlen(str)-(p-str));
}
return 0;
}
short isNumber(char *str)
{
char *p=str;
for(;*p;p++)
{
if(*p<48 || *p>57) return 0;
}
return 1;
}
short getCommonArgFromArg(char *str,
Dinfo *driveTable[],
Pinfo *partitionTable[],
commonArg **arg,
char win)
{
char *minus,*plus,*equal;
char *qmin[16];
char digit[16];
char *doPointer;
char *startToFind;
if(!str)
{
printf("Argument error\n");
return -1;
}
*arg=(commonArg *)malloc(sizeof(commonArg));
memset((*arg),0,sizeof(commonArg));
(*arg)->index=INVALID_INDEX;
startToFind=strchr(str,')');
startToFind=(startToFind ? startToFind : str);
minus=strchr(startToFind,'-');
plus=strchr(startToFind,'+');
equal=strchr(startToFind,'=');
if(equal)
{
short i;
for(i=0;i<16;i++)
qmin[i]=NULL;
extractFromStr(equal+1,'=',qmin);
for(i=0;qmin[i];i++)
switch(qmin[i][0])
{
case 'q' : (*arg)->quiet=1;
case 'Q' : (*arg)->quiet=1;
case 'm' : (*arg)->mute=1;
case 'M' : (*arg)->mute=1;
case 'i' : (*arg)->ignore=1;
case 'I' : (*arg)->ignore=1;
case 'n' : (*arg)->next=1;
case 'N' : (*arg)->next=1;
}
}
if(plus)
{
memset(digit,0,16);
doPointer=digit;
if(equal)
strncpy(digit,plus+1,equal-plus-1);
else doPointer=plus+1;
if(!isNumber(doPointer))
{
printf("Non-number found in %s\n",doPointer);
return -1;
}
(*arg)->sectorToDo=strtoul(doPointer,NULL,10);
}
if(minus)
{
memset(digit,0,16);
doPointer=digit;
if(plus)
strncpy(digit,minus+1,plus-minus-1);
else
{
if(equal)
strncpy(digit,minus+1,equal-minus-1);
else doPointer=minus+1;
}
if(!isNumber(doPointer))
{
printf("Non-number found in %s\n",doPointer);
return -1;
}
(*arg)->startToDo=strtoul(doPointer,NULL,10);
}
if((*arg)->startToDo)
{
(*arg)->startToDoTRUE=1;
(*arg)->startToDoGiven=(*arg)->startToDo;
}
if((*arg)->sectorToDo) (*arg)->sectorToDoTRUE=1;
if(str[0]=='(')
{
char killBracket[256];
char *fileArg[10];
short index=INVALID_INDEX;
short i;
char *plus;
char *minus;
char *equal;
char *end;
char *qmin[4];
(*arg)->argMode=ARGUMENT_IMAGE;
memset(killBracket,0,256);
for(i=0;i<10;i++)
fileArg[i]=NULL;
if((end=strchr(str,')'))==NULL)
{
printf("No \")\" found\n");
return -1;
}
strncpy(killBracket,str+1,strchr(str,')')-str-1);
extractFromStr(killBracket,'=',fileArg);
strcpy((*arg)->name,fileArg[0]);
if(strchr((*arg)->name,':'))
{
if((*arg)->name[0]) (*arg)->letter=(*arg)->name[0];
else
{
printf("No drive in %s\n",(*arg)->name);
return -1;
}
}
else
{
char buf[MAXPATH];
buf[0]='\0';
getcwd(buf,MAXPATH);
if(buf[0]) (*arg)->letter=buf[0];
else printf("Problem occured getting current working directory\n");
}
getPartitionItem(NULL,(*arg)->letter,partitionTable,&index,win);
(*arg)->drive=partitionTable[index]->hardDrive;
(*arg)->number=partitionTable[index]->number;
for(i=1;fileArg[i];i++)
{
if(toupper(fileArg[i][0])=='C')
{
(*arg)->compressDecompress=1;
if(strlen(fileArg[i])>1) (*arg)->compressLevel=atoi(&fileArg[i][1]);
else (*arg)->compressLevel=DEFAULT_COMPRESS_LEVEL;
}
else if(toupper(fileArg[i][0])=='K') (*arg)->keyTRUE=1;
else if(toupper(fileArg[i][0])=='V')
{
(*arg)->separatedVolume=1;
if(strlen(fileArg[i])>1) (*arg)->separatedMB=strtoul(&fileArg[i][1],NULL,10);
else (*arg)->separatedMB=DEFAULT_EVERY_VOLUME_MB;
}
else if(toupper(fileArg[i][0])=='E')
{
if(!((*arg)->compressDecompress) || !((*arg)->keyTRUE) || !((*arg)->separatedVolume))
{
printf("E needs C K V\n");
return -1;
}
(*arg)->keyEveryTRUE=1;
if(fileArg[i][1]=='?') (*arg)->keyEveryPromptTRUE=1;
}
}
}
else
{
typedef struct _deviceArg
{
char device[10];
unsigned long startToDo;
unsigned long sectorToDo;
}Darg;
/* char *pointerMinus=NULL,*pointerPlus=NULL; */
Darg deviceStr;
memset(&deviceStr,0,sizeof(Darg));
if(minus) doPointer=minus;
else if(plus) doPointer=plus;
else if(equal) doPointer=equal;
else doPointer=NULL;
strncpy(deviceStr.device,str,(doPointer ? doPointer-str : strlen(str)+1));
if(minus)
deviceStr.startToDo=(*arg)->startToDo;
if(plus)
deviceStr.sectorToDo=(*arg)->sectorToDo;
if(strchr(deviceStr.device,':')==NULL)
{
short index;
char drive;
if(!isNumber(deviceStr.device))
{
printf("Non-number found in %s\n",deviceStr.device);
return -1;
}
else drive=atoi(deviceStr.device);
if(getDriveItem(drive,driveTable,&index)==-1) return -1;
if(deviceStr.startToDo>driveTable[index]->totalSector ||
deviceStr.sectorToDo>driveTable[index]->totalSector ||
deviceStr.startToDo+deviceStr.sectorToDo>driveTable[index]->totalSector)
{
printf("%lu + %lu = %lu > %lu (drive %d total sector)\n",
deviceStr.startToDo,
deviceStr.sectorToDo,
deviceStr.startToDo+deviceStr.sectorToDo,
driveTable[index]->totalSector,
driveTable[index]->number);
return -1;
}
(*arg)->argMode=ARGUMENT_DISK;
(*arg)->drive=drive;
(*arg)->index=index;
(*arg)->totalSector=driveTable[index]->totalSector;
(*arg)->startToDo=deviceStr.startToDo;
(*arg)->sectorToDo=deviceStr.sectorToDo ?
deviceStr.sectorToDo : driveTable[index]->totalSector-deviceStr.startToDo;
}
else
{
if(deviceStr.device[0]>48 && deviceStr.device[0]<58)
{
char driveStr[10];
char drive;
char partitionStr[10];
char partition;
char *p=strchr(deviceStr.device,':');
short index;
memset(driveStr,0,10);
memset(partitionStr,0,10);
strncpy(driveStr,deviceStr.device,p-(deviceStr.device));
if(!isNumber(driveStr))
{
printf("Non-number found in %s\n",driveStr);
return -1;
}
else drive=atoi(driveStr);
strcpy(partitionStr,p+1);
if(!isNumber(partitionStr))
{
printf("Non-number found in %s\n",partitionStr);
return -1;
}
else partition=atoi(partitionStr);
if(getPartitionItem(drive,partition,partitionTable,&index,win)==-1) return -1;
if(deviceStr.startToDo>partitionTable[index]->total ||
deviceStr.sectorToDo>partitionTable[index]->total ||
deviceStr.startToDo+deviceStr.sectorToDo>partitionTable[index]->total)
{
printf("%lu + %lu = %lu > %lu (%d:%d total sector)\n",
deviceStr.startToDo,
deviceStr.sectorToDo,
deviceStr.startToDo+deviceStr.sectorToDo,
partitionTable[index]->total,
partitionTable[index]->hardDrive,
partitionTable[index]->number);
return -1;
}
(*arg)->argMode=ARGUMENT_PARTITION;
(*arg)->drive=drive;
(*arg)->number=partition;
(*arg)->index=index;
(*arg)->totalSector=partitionTable[index]->total;
(*arg)->startToDo=partitionTable[index]->start+deviceStr.startToDo;
(*arg)->sectorToDo=deviceStr.sectorToDo ?
deviceStr.sectorToDo : partitionTable[index]->total-deviceStr.startToDo;
}
else
{
char letter;
short index;
letter=toupper(deviceStr.device[0]);
if(letter<65 || letter>90)
{
printf("No such drive letter %c\n",deviceStr.device[0]);
return -1;
}
if(strlen(deviceStr.device)!=2)
{
printf("Invalid DOS letter parameter: %s\n",deviceStr.device);
return -1;
}
if(getPartitionItem(NULL,letter,partitionTable,&index,win)==-1)
{
return -1;
}
if(deviceStr.startToDo>partitionTable[index]->total ||
deviceStr.sectorToDo>partitionTable[index]->total ||
deviceStr.startToDo+deviceStr.sectorToDo>partitionTable[index]->total)
{
printf("%lu + %lu = %lu > %lu (%c: total sector)\n",
deviceStr.startToDo,
deviceStr.sectorToDo,
deviceStr.startToDo+deviceStr.sectorToDo,
partitionTable[index]->total,
partitionTable[index]->letter);
return -1;
}
(*arg)->argMode=ARGUMENT_DOSLETTER;
(*arg)->drive=partitionTable[index]->hardDrive;
(*arg)->number=partitionTable[index]->number;
/* if(win) (*arg)->WINletter=letter;
else*/ (*arg)->letter=letter;
(*arg)->index=index;
(*arg)->totalSector=partitionTable[index]->total;
(*arg)->startToDo=partitionTable[index]->start+deviceStr.startToDo;
(*arg)->sectorToDo=deviceStr.sectorToDo ?
deviceStr.sectorToDo : partitionTable[index]->total-deviceStr.startToDo;
}
}
}
return 0;
}
short YesNo(char *s, char *filename)
{
char str[8];
printf("%s%s ? (Y/N)",s,filename);
while(1)
{
fflush(stdin);
gets(str);
if(strcmpi(str,"Y")==0) return 1;
if(strcmpi(str,"N")==0) return 0;
}
}
short fopenFile(FILE **f, char rw, char *filename, char allVolumeEnd)
{
char *prompt1="Opening",*fail1="open";
char *prompt2="Creating",*fail2="create";
char *pFront,*pDot;
char bareName[16];
char userChangeFile=0;
*f=fopen(filename,(rw ? "rb" : "wb+"));
if(!*f) return -1;
/*
while(1)
{
memset(bareName,0,16);
if((pFront=strrchr(filename,'\\'))==NULL) pFront=strchr(filename,':');
if(pFront) pFront+=1;
else pFront=filename;
pDot=strchr(filename,'.');
if(!pDot) strcpy(bareName,pFront);
else strncpy(bareName,pFront,pDot-pFront);
if(strlen(bareName)>8)
{
printf("File bare name \"%s\" > 8\n",bareName);
goto changeFile;
}
if(pDot && strlen(pDot+1)>3)
{
printf("File suffix name \"%s\" > 3\n",pDot+1);
goto changeFile;
}
if(!rw)
{
*f=fopen(filename,"rb");
if((*f)!=NULL)
{
fclose(*f);
printf("\nFile \"%s\" exists, ",filename);
if(YesNo("overwrite it","")!=1) goto changeFile;
if(remove(filename)!=0)
{
printf("\nFailed to delete file \"%s\"\n",filename);
goto changeFile;
}
}
}
*f=fopen(filename,(rw ? "rb" : "wb+"));
if((*f)==NULL)
{
if(allVolumeEnd) return -2;
else printf("\nFailed to %s file \"%s\"\n",(rw ? fail1 : fail2),filename);
}
else if(rw)
{
if(filelength(fileno(*f))==0)
printf("\nFile \"%s\":\n0 bytes!\n",
filename);
else break;
}
else break;
while(1)
{
char choose[128];
memset(choose,0,128);
printf("Change to another file, press C\nExit, press E\nC,E ?");
fflush(stdin);
scanf("%s",choose);
if(strcmpi(choose,"C")==0) break;
else if(strcmpi(choose,"E")==0) return -1;
}
changeFile :
printf("Enter a file name:>>");
memset(filename,0,strlen(filename));
scanf("%s",filename);
userChangeFile=1;
}
if(userChangeFile) return -3;
*/
return 0;
}
char getDOSletterFromFileName(char *file)
{
char *p;
char fileBuf[256];
memset(fileBuf,0,256);
strcpy(fileBuf,file);
p=strchr(fileBuf,':');
if(p)
{
if(p-fileBuf!=1)
{
printf("\":\" invalid\n");
return 0;
}
return fileBuf[0];
}
else
{
memset(fileBuf,0,256);
getcwd(fileBuf,256);
return fileBuf[0];
}
/*return getdisk()+65;*/
}
/*void*/UINT savePrintPartitionTable(LPVOID p)/*Pinfo *pInfo[], short drive, char *file, char mode, char pause, char yes)
*/{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -