BOOL PerformRedirection(REDIRECTION *RedirList) { REDIRECTION *Redir; LPTSTR Filename; HANDLE hNew; UINT DupNumber; static SECURITY_ATTRIBUTES SecAttr = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; /* Some parameters used for read, write, and append, respectively */ static const DWORD dwAccess[] = { GENERIC_READ, GENERIC_WRITE, GENERIC_WRITE }; static const DWORD dwShareMode[] = { FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SHARE_READ, FILE_SHARE_READ }; static const DWORD dwCreationDisposition[] = { OPEN_EXISTING, CREATE_ALWAYS, OPEN_ALWAYS }; for (Redir = RedirList; Redir; Redir = Redir->Next) { Filename = DoDelayedExpansion(Redir->Filename); if (!Filename) goto redir_error; StripQuotes(Filename); if (*Filename == _T('&')) { DupNumber = Filename[1] - _T('0'); if (DupNumber >= 10 || !DuplicateHandle(GetCurrentProcess(), GetHandle(DupNumber), GetCurrentProcess(), &hNew, 0, TRUE, DUPLICATE_SAME_ACCESS)) { hNew = INVALID_HANDLE_VALUE; } } else { hNew = CreateFile(Filename, dwAccess[Redir->Type], dwShareMode[Redir->Type], &SecAttr, dwCreationDisposition[Redir->Type], 0, NULL); } if (hNew == INVALID_HANDLE_VALUE) { ConErrResPrintf(Redir->Type == REDIR_READ ? STRING_CMD_ERROR1 : STRING_CMD_ERROR3, Filename); cmd_free(Filename); redir_error: /* Undo all the redirections before this one */ UndoRedirection(RedirList, Redir); return FALSE; } if (Redir->Type == REDIR_APPEND) SetFilePointer(hNew, 0, NULL, FILE_END); Redir->OldHandle = GetHandle(Redir->Number); SetHandle(Redir->Number, hNew); TRACE("%d redirected to: %s\n", Redir->Number, debugstr_aw(Filename)); cmd_free(Filename); } return TRUE; }
INT ExecuteIf(PARSED_COMMAND *Cmd) { INT result = FALSE; /* when set cause 'then' clause to be executed */ LPTSTR param; LPTSTR Left = NULL, Right; if (Cmd->If.LeftArg) { Left = DoDelayedExpansion(Cmd->If.LeftArg); if (!Left) return 1; } Right = DoDelayedExpansion(Cmd->If.RightArg); if (!Right) { cmd_free(Left); return 1; } if (Cmd->If.Operator == IF_CMDEXTVERSION) { /* IF CMDEXTVERSION n: check if Command Extensions version * is greater or equal to n */ DWORD n = _tcstoul(Right, ¶m, 10); if (*param != _T('\0')) { error_syntax(Right); cmd_free(Right); return 1; } result = (2 >= n); } else if (Cmd->If.Operator == IF_DEFINED) { /* IF DEFINED var: check if environment variable exists */ result = (GetEnvVarOrSpecial(Right) != NULL); } else if (Cmd->If.Operator == IF_ERRORLEVEL) { /* IF ERRORLEVEL n: check if last exit code is greater or equal to n */ INT n = _tcstol(Right, ¶m, 10); if (*param != _T('\0')) { error_syntax(Right); cmd_free(Right); return 1; } result = (nErrorLevel >= n); } else if (Cmd->If.Operator == IF_EXIST) { BOOL IsDir; INT Size; WIN32_FIND_DATA f; HANDLE hFind; /* IF EXIST filename: check if file exists (wildcards allowed) */ StripQuotes(Right); Size = _tcslen(Right); IsDir = (Right[Size - 1] == '\\'); if (IsDir) Right[Size - 1] = 0; hFind = FindFirstFile(Right, &f); if (hFind != INVALID_HANDLE_VALUE) { if (IsDir) { result = ((f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY); } else { result = TRUE; } FindClose(hFind); } if (IsDir) Right[Size - 1] = '\\'; } else { /* Do case-insensitive string comparisons if /I specified */ INT (*StringCmp)(LPCTSTR, LPCTSTR) = (Cmd->If.Flags & IFFLAG_IGNORECASE) ? _tcsicmp : _tcscmp; if (Cmd->If.Operator == IF_STRINGEQ) { /* IF str1 == str2 */ result = StringCmp(Left, Right) == 0; } else { result = GenericCmp(StringCmp, Left, Right); switch (Cmd->If.Operator) { case IF_EQU: result = (result == 0); break; case IF_NEQ: result = (result != 0); break; case IF_LSS: result = (result < 0); break; case IF_LEQ: result = (result <= 0); break; case IF_GTR: result = (result > 0); break; case IF_GEQ: result = (result >= 0); break; } } } cmd_free(Left); cmd_free(Right); if (result ^ ((Cmd->If.Flags & IFFLAG_NEGATE) != 0)) { /* full condition was true, do the command */ return ExecuteCommand(Cmd->Subcommands); } else { /* full condition was false, do the "else" command if there is one */ if (Cmd->Subcommands->Next) return ExecuteCommand(Cmd->Subcommands->Next); return 0; } }