Fix failure of create/opening a temporary file for piping

This commit is contained in:
maron2000
2025-05-12 22:40:42 +09:00
parent 88517e3270
commit 41228d9fd3

View File

@@ -468,20 +468,45 @@ void DOS_Shell::ParseLine(char * line) {
bool fail=false; bool fail=false;
char pipetmp[270]; char pipetmp[270];
uint16_t fattr; uint16_t fattr;
if (toc) { if(toc) {
initRand(); // Initialize random number generator
std::string line; initRand();
if (!GetEnvStr("TEMP",line)&&!GetEnvStr("TMP",line))
sprintf(pipetmp, "pipe%d.tmp", rand()%10000); // Try to get TEMP or TMP environment variable
else { std::string tempPath;
std::string::size_type idx = line.find('='); if(!GetEnvStr("TEMP", tempPath) && !GetEnvStr("TMP", tempPath)) {
std::string temp=line.substr(idx +1 , std::string::npos); // Fallback: use current drive root as fallback directory (e.g., "C:\")
if (DOS_GetFileAttr(temp.c_str(), &fattr) && fattr&DOS_ATTR_DIRECTORY) char currentDrive = DOS_GetDefaultDrive() + 'A';
sprintf(pipetmp, "%s\\pipe%d.tmp", temp.c_str(), rand()%10000); tempPath = std::string(1, currentDrive) + ":\\";
else }
sprintf(pipetmp, "pipe%d.tmp", rand()%10000); else {
} // Extract directory from environmental variable
} std::string::size_type idx = tempPath.find('=');
if(idx != std::string::npos)
tempPath = tempPath.substr(idx + 1);
}
// Ensure the path ends with backslash
if(!tempPath.empty() && tempPath.back() != '\\') {
tempPath += '\\';
}
// Check if directory is valid
uint16_t fattr;
if(!(DOS_GetFileAttr(tempPath.c_str(), &fattr) && (fattr & DOS_ATTR_DIRECTORY))) {
// Fallback to current drive root again if directory is invalid
char currentDrive = DOS_GetDefaultDrive() + 'A';
tempPath = std::string(1, currentDrive) + ":\\";
}
// Assign to tempEnv after fixing the path
const char* tempEnv = tempPath.c_str();
// Generate unique pipe file path
int pipeid = rand() % 10000;
snprintf(pipetmp, sizeof(pipetmp), "%spipe%d.tmp", tempEnv, pipeid);
}
DOS_Device *tmpdev = NULL; DOS_Device *tmpdev = NULL;
if (out||toc) { if (out||toc) {
if (out&&toc) if (out&&toc)