New environment functions
Imported-from: https://svn.code.sf.net/p/dosbox/code-0/dosbox/trunk@331
This commit is contained in:
parent
b609fb89a4
commit
1f8fcff74e
1 changed files with 53 additions and 48 deletions
|
@ -78,8 +78,6 @@ static Bitu PROGRAMS_Handler(void) {
|
|||
|
||||
Program::Program() {
|
||||
/* Find the command line and setup the PSP */
|
||||
|
||||
|
||||
psp = new DOS_PSP(dos.psp);
|
||||
/* Scan environment for filename */
|
||||
char * envscan=(char *)HostMake(psp->GetEnvironment(),0);
|
||||
|
@ -105,64 +103,71 @@ void Program::WriteOut(const char * format,...) {
|
|||
}
|
||||
|
||||
|
||||
char * Program::GetEnvStr(char * env_str) {
|
||||
bool Program::GetEnvStr(const char * entry,std::string & result) {
|
||||
/* Walk through the internal environment and see for a match */
|
||||
/* Taking some short cuts here to not fuck around with memory structure */
|
||||
|
||||
char * envstart=(char *)HostMake(psp->GetEnvironment(),0);
|
||||
size_t len=strlen(env_str);
|
||||
while (*envstart) {
|
||||
if (strncasecmp(env_str,envstart,len)==0 && envstart[len]=='=') {
|
||||
return envstart;
|
||||
}
|
||||
envstart+=strlen(envstart)+1;
|
||||
}
|
||||
return 0;
|
||||
PhysPt env_read=PhysMake(psp->GetEnvironment(),0);
|
||||
char env_string[1024];
|
||||
result.erase();
|
||||
if (!entry[0]) return false;
|
||||
do {
|
||||
MEM_StrCopy(env_read,env_string,1024);
|
||||
if (!env_string[0]) return false;
|
||||
env_read+=strlen(env_string)+1;
|
||||
if (!strchr(env_string,'=')) continue;
|
||||
if (strncasecmp(entry,env_string,strlen(entry))!=0) continue;
|
||||
result=env_string;
|
||||
return true;
|
||||
} while (1);
|
||||
return false;
|
||||
};
|
||||
|
||||
char * Program::GetEnvNum(Bit32u num) {
|
||||
char * envstart=(char *)HostMake(psp->GetEnvironment(),0);
|
||||
while (*envstart) {
|
||||
if (!num) return envstart;
|
||||
envstart+=strlen(envstart)+1;
|
||||
bool Program::GetEnvNum(Bitu num,std::string & result) {
|
||||
char env_string[1024];
|
||||
PhysPt env_read=PhysMake(psp->GetEnvironment(),0);
|
||||
do {
|
||||
MEM_StrCopy(env_read,env_string,1024);
|
||||
if (!env_string[0]) break;
|
||||
if (!num) { result=env_string;return true;}
|
||||
env_read+=strlen(env_string)+1;
|
||||
num--;
|
||||
}
|
||||
return 0;
|
||||
} while (1);
|
||||
return false;
|
||||
}
|
||||
|
||||
Bit32u Program::GetEnvCount(void) {
|
||||
char * envstart=(char *)HostMake(psp->GetEnvironment(),0);
|
||||
Bit32u num=0;
|
||||
while (*envstart) {
|
||||
envstart+=strlen(envstart)+1;
|
||||
Bitu Program::GetEnvCount(void) {
|
||||
PhysPt env_read=PhysMake(psp->GetEnvironment(),0);
|
||||
Bitu num=0;
|
||||
while (mem_readb(env_read)!=0) {
|
||||
for (;mem_readb(env_read);env_read++) {};
|
||||
env_read++;
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
bool Program::SetEnv(char * env_entry,char * new_string) {
|
||||
MCB * env_mcb=(MCB *)HostMake(psp->GetEnvironment()-1,0);
|
||||
upcase(env_entry);
|
||||
Bit32u env_size=env_mcb->size*16;
|
||||
if (!env_size) E_Exit("SHELL:Illegal environment size");
|
||||
/* First try to find the old entry */
|
||||
size_t len=strlen(env_entry);
|
||||
char * envstart=(char *)HostMake(psp->GetEnvironment(),0);
|
||||
while (*envstart) {
|
||||
if (strncasecmp(env_entry,envstart,len)==0 && envstart[len]=='=') {
|
||||
/* Now remove this entry */
|
||||
memmove(envstart,envstart+strlen(envstart)+1,env_size);
|
||||
} else {
|
||||
envstart+=strlen(envstart)+1;
|
||||
env_size-=(strlen(envstart)+1);
|
||||
}
|
||||
bool Program::SetEnv(const char * entry,const char * new_string) {
|
||||
PhysPt env_read=PhysMake(psp->GetEnvironment(),0);
|
||||
PhysPt env_write=env_read;
|
||||
char env_string[1024];
|
||||
do {
|
||||
MEM_StrCopy(env_read,env_string,1024);
|
||||
if (!env_string[0]) break;
|
||||
env_read+=strlen(env_string)+1;
|
||||
if (!strchr(env_string,'=')) continue; /* Remove corrupt entry? */
|
||||
if ((strncasecmp(entry,env_string,strlen(entry))==0) &&
|
||||
env_string[strlen(entry)]=='=') continue;
|
||||
MEM_BlockWrite(env_write,env_string,strlen(env_string)+1);
|
||||
env_write+=strlen(env_string)+1;
|
||||
} while (1);
|
||||
/* TODO Maybe save the program name sometime. not really needed though */
|
||||
/* Save the new entry */
|
||||
if (new_string[0]) {
|
||||
sprintf(env_string,"%s=%s",entry,new_string);
|
||||
MEM_BlockWrite(env_write,env_string,strlen(env_string)+1);
|
||||
env_write+=strlen(env_string)+1;
|
||||
}
|
||||
/* Now add the string if there is space available */
|
||||
if (env_size<(strlen(env_entry)+strlen(new_string)+2)) return false;
|
||||
if (!*new_string) return true;
|
||||
sprintf(envstart,"%s=%s",env_entry,new_string);
|
||||
envstart+=strlen(envstart)+1;
|
||||
*envstart++=0;*envstart++=0;*envstart++=0;
|
||||
/* Clear out the final piece of the environment */
|
||||
mem_writed(env_write,0);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue