INT CommandActivate (LPTSTR param) { HWND hwnd; LPTSTR *arg; INT argc; if (_tcsncmp (param, _T("/?"), 2) == 0) { ConOutResPaging(TRUE,STRING_WINDOW_HELP2); return 0; } if(!(*param)) return 1; /*Split the user input into array*/ arg = split (param, &argc, FALSE); if(argc < 2) { if(arg != NULL) freep(arg); } hwnd = FindWindow(NULL, arg[0]); if (hwnd == NULL) { if(arg != NULL) freep(arg); ConErrResPuts(STRING_WINDOW_ERROR1); return 1; } if(arg != NULL) freep(arg); return ServiceActivate(param, hwnd); }
INT CommandEcho (LPTSTR param) { LPTSTR p1; TRACE ("CommandEcho: '%s'\n", debugstr_aw(param)); /* skip all spaces for the check of '/?', 'ON' and 'OFF' */ p1 = param; while(_istspace(*p1)) p1++; if (!_tcsncmp (p1, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_ECHO_HELP4); return 0; } if (!OnOffCommand(p1, &bEcho, STRING_ECHO_HELP5)) { /* skip the first character */ ConOutPuts(param + 1); ConOutChar(_T('\n')); } return 0; }
INT CommandFree (LPTSTR param) { LPTSTR szParam; TCHAR szDefPath[MAX_PATH]; INT argc, i; LPTSTR *arg; if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_FREE_HELP2); return 0; } if (!param || *param == _T('\0')) { GetCurrentDirectory (MAX_PATH, szDefPath); szDefPath[2] = _T('\0'); szParam = szDefPath; } else szParam = param; arg = split (szParam, &argc, FALSE, FALSE); for (i = 0; i < argc; i++) PrintDiskInfo (arg[i]); freep (arg); return 0; }
/* * set the exitflag to true */ INT CommandExit (LPTSTR param) { if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_EXIT_HELP); /* Just make sure */ bExit = FALSE; /* Dont exit */ return 0; } if (bc != NULL && _tcsnicmp(param,_T("/b"),2) == 0) { param += 2; while (_istspace (*param)) param++; if (_istdigit(*param)) nErrorLevel = _ttoi(param); ExitBatch(); } else { bExit = TRUE; } return 0; }
/* * does nothing */ INT CommandRem (LPTSTR param) { if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_REM_HELP); } return 0; }
/* * CD / CHDIR * */ INT cmd_chdir (LPTSTR param) { TCHAR szCurrent[MAX_PATH]; BOOL bChangeDrive = FALSE; /* Filter out special cases first */ /* Print Help */ if (!_tcsncmp(param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_CD_HELP); return 0; } /* Remove " */ StripQuotes(param); /* Set Error Level to Success */ nErrorLevel = 0; /* Print Current Directory on a disk */ if (_tcslen(param) == 2 && param[1] == _T(':')) { if (GetRootPath(param, szCurrent, MAX_PATH)) { error_invalid_drive(); return 1; } ConOutPuts(szCurrent); return 0; } /* Get Current Directory */ GetCurrentDirectory(MAX_PATH, szCurrent); if (param[0] == _T('\0')) { ConOutPuts(szCurrent); return 0; } /* Input String Contains /D Switch */ if (!_tcsncicmp(param, _T("/D"), 2)) { bChangeDrive = TRUE; param += 2; while (_istspace(*param)) param++; } if (!SetRootPath(bChangeDrive ? NULL : szCurrent, param)) return 1; return 0; }
INT CommandMemory (LPTSTR param) { MEMORYSTATUSEX msex; TCHAR szMemoryLoad[20]; TCHAR szTotalPhys[40]; TCHAR szAvailPhys[40]; TCHAR szTotalPageFile[40]; TCHAR szAvailPageFile[40]; TCHAR szTotalVirtual[40]; TCHAR szAvailVirtual[40]; BOOL (WINAPI *GlobalMemoryStatusEx)(LPMEMORYSTATUSEX); if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_MEMMORY_HELP1); return 0; } GlobalMemoryStatusEx = (BOOL (WINAPI *)(LPMEMORYSTATUSEX))GetProcAddress(GetModuleHandle(_T("KERNEL32")), "GlobalMemoryStatusEx"); if (GlobalMemoryStatusEx) { msex.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&msex); } else { MEMORYSTATUS ms; ms.dwLength = sizeof(MEMORYSTATUS); GlobalMemoryStatus(&ms); msex.dwMemoryLoad = ms.dwMemoryLoad; msex.ullTotalPhys = ms.dwTotalPhys; msex.ullAvailPhys = ms.dwAvailPhys; msex.ullTotalPageFile = ms.dwTotalPageFile; msex.ullAvailPageFile = ms.dwAvailPageFile; msex.ullTotalVirtual = ms.dwTotalVirtual; msex.ullAvailVirtual = ms.dwAvailVirtual; } ConvertULargeInteger(msex.dwMemoryLoad, szMemoryLoad, 20, FALSE); ConvertULargeInteger(msex.ullTotalPhys, szTotalPhys, 40, TRUE); ConvertULargeInteger(msex.ullAvailPhys, szAvailPhys, 40, TRUE); ConvertULargeInteger(msex.ullTotalPageFile, szTotalPageFile, 40, TRUE); ConvertULargeInteger(msex.ullAvailPageFile, szAvailPageFile, 40, TRUE); ConvertULargeInteger(msex.ullTotalVirtual, szTotalVirtual, 40, TRUE); ConvertULargeInteger(msex.ullAvailVirtual, szAvailVirtual, 40, TRUE); ConOutResPrintf(STRING_MEMMORY_HELP2, szMemoryLoad, szTotalPhys, szAvailPhys, szTotalPageFile, szAvailPageFile, szTotalVirtual, szAvailVirtual); return 0; }
INT cmd_prompt (LPTSTR param) { if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_PROMPT_HELP1); #ifdef FEATURE_DIRECTORY_STACK ConOutResPaging(FALSE,STRING_PROMPT_HELP2); #endif ConOutResPaging(FALSE,STRING_PROMPT_HELP3); return 0; } /* if it is null, then it needs to set to default, because that means the user entered "prompt" only. so even if param is null you _must_ still set prompt to the default. There seems to be some kinda difference between winxp and 2k in this matter and this way will cover both. Do not use fixed size of szParam for param the buffer are 8192bytes and will later change to dymatic buffer */ /* set PROMPT environment variable */ if (param[0] != _T('\0')) { if (!SetEnvironmentVariable (_T("PROMPT"), param)) return 1; } else { TCHAR szParam[5]; _tcscpy(szParam,_T("$P$G")); if (!SetEnvironmentVariable (_T("PROMPT"),szParam)) return 1; } return 0; }
INT cmd_if (LPTSTR param) { TRACE ("cmd_if: (\'%s\')\n", debugstr_aw(param)); if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_IF_HELP1); return 0; } error_syntax(param); return 1; }
INT CommandAssoc (LPTSTR param) { /* print help */ if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_ASSOC_HELP); return 0; } nErrorLevel = 0; if (_tcslen(param) == 0) PrintAllAssociations(); else { LPTSTR lpEqualSign = _tcschr(param, _T('=')); if (lpEqualSign != NULL) { LPTSTR fileType = lpEqualSign + 1; LPTSTR extension = cmd_alloc((lpEqualSign - param + 1) * sizeof(TCHAR)); _tcsncpy(extension, param, lpEqualSign - param); extension[lpEqualSign - param] = (TCHAR)0; if (_tcslen(fileType) == 0) /* if the equal sign is the last character in the string, then delete the key */ { RemoveAssociation(extension); } else /* otherwise, add the key and print out the association*/ { AddAssociation( extension, fileType); PrintAssociation(extension); } cmd_free(extension); } else { /* no equal sign, print all associations */ INT retval = PrintAssociation(param); if (retval == 0) /* if nothing printed out */ ConOutResPrintf(STRING_ASSOC_ERROR, param); } } return 0; }
INT cmd_path (LPTSTR param) { if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_PATH_HELP1); return 0; } nErrorLevel = 0; /* if param is empty, display the PATH environment variable */ if (!param || !*param) { DWORD dwBuffer; LPTSTR pszBuffer; pszBuffer = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR)); dwBuffer = GetEnvironmentVariable (_T("PATH"), pszBuffer, ENV_BUFFER_SIZE); if (dwBuffer == 0) { cmd_free(pszBuffer); ConOutResPrintf(STRING_VOL_HELP2, _T("PATH")); return 0; } else if (dwBuffer > ENV_BUFFER_SIZE) { pszBuffer = (LPTSTR)cmd_realloc (pszBuffer, dwBuffer * sizeof (TCHAR)); GetEnvironmentVariable (_T("PATH"), pszBuffer, dwBuffer); } ConOutPrintf (_T("PATH=%s\n"), pszBuffer); cmd_free (pszBuffer); return 0; } /* skip leading '=' */ if (*param == _T('=')) param++; /* set PATH environment variable */ if (!SetEnvironmentVariable (_T("PATH"), param)) { nErrorLevel = 1; return 1; } return 0; }
INT CommandWindow (LPTSTR param) { HWND hwnd; if (_tcsncmp (param, _T("/?"), 2) == 0) { ConOutResPaging(TRUE,STRING_WINDOW_HELP1); return 0; } hwnd = GetConsoleWindow(); Sleep(0); return ServiceActivate(param, hwnd); }
INT cmd_vol (LPTSTR param) { TCHAR szRootPath[] = _T("A:\\"); TCHAR szPath[MAX_PATH]; if (!_tcsncmp(param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_VOL_HELP4); return 0; } nErrorLevel = 0; if (param[0] == _T('\0')) { GetCurrentDirectory(MAX_PATH, szPath); szRootPath[0] = szPath[0]; } else { _tcsupr (param); if (param[1] == _T(':')) { szRootPath[0] = param[0]; } else { error_invalid_drive (); nErrorLevel = 1; return 1; } } if (!IsValidPathName (szRootPath)) { error_invalid_drive (); nErrorLevel = 1; return 1; } /* print the header */ if (!PrintVolumeHeader (szRootPath)) { nErrorLevel = 1; return 1; } return 0; }
INT cmd_title (LPTSTR param) { /* Do nothing if no args */ if (*param == _T('\0')) return 0; /* Asking help? */ if (!_tcsncmp(param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_TITLE_HELP); return 0; } return SetConsoleTitle (param); }
INT cmd_verify (LPTSTR param) { if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_VERIFY_HELP1); return 0; } if (!OnOffCommand(param, &bVerify, STRING_VERIFY_HELP2)) { ConErrResPuts(STRING_VERIFY_HELP3); return nErrorLevel = 1; } return nErrorLevel = 0; }
INT cmd_call (LPTSTR param) { TCHAR line[CMDLINE_LENGTH + 1]; TCHAR *first; BOOL bInQuote = FALSE; TRACE ("cmd_call: (\'%s\')\n", debugstr_aw(param)); if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_CALL_HELP); return 0; } /* Do a second round of %-variable substitutions */ if (!SubstituteVars(param, line, _T('%'))) return nErrorLevel = 1; /* Find start and end of first word */ first = line; while (_istspace(*first)) first++; for (param = first; *param; param++) { if (!bInQuote && (_istspace(*param) || _tcschr(_T(",;="), *param))) break; bInQuote ^= (*param == _T('"')); } /* Separate first word from rest of line */ memmove(param + 1, param, (_tcslen(param) + 1) * sizeof(TCHAR)); *param++ = _T('\0'); if (*first == _T(':') && (bc)) { /* CALL :label - call a subroutine of the current batch file */ while (*param == _T(' ')) param++; nErrorLevel = Batch(bc->BatchFilePath, first, param, NULL); return nErrorLevel; } nErrorLevel = DoCommand(first, param, NULL); return nErrorLevel; }
INT cmd_shift (LPTSTR param) { INT i = 0; TRACE ("cmd_shift: (\'%s\')\n", debugstr_aw(param)); if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_SHIFT_HELP); return 0; } nErrorLevel = 0; if (bc == NULL) { /* not in batch - error!! */ nErrorLevel = 1; return 1; } if (!_tcsicmp (param, _T("down"))) { if (bc->shiftlevel[0]) for (; i <= 9; i++) bc->shiftlevel[i]--; } else /* shift up */ { if (*param == _T('/')) { if (param[1] < '0' || param[1] > '9') { error_invalid_switch(param[1]); return 1; } i = param[1] - '0'; } for (; i < 9; i++) bc->shiftlevel[i] = bc->shiftlevel[i + 1]; bc->shiftlevel[i]++; } return 0; }
static INT CommandExit( PCONSOLE_STATE State, LPSTR param) { #if 0 if (!strncmp(param, "/?", 2)) { ConOutResPaging(TRUE,STRING_EXIT_HELP); /* Just make sure */ bExit = FALSE; /* Dont exit */ return 0; } #endif State->bExit = TRUE; return 0; }
INT CommandAlias (LPTSTR param) { LPTSTR ptr; if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_ALIAS_HELP); return 0; } nErrorLevel = 0; if (param[0] == _T('\0')) { PrintAlias (); return 0; } nErrorLevel = 0; /* error if no '=' found */ if ((ptr = _tcschr (param, _T('='))) == 0) { nErrorLevel = 1; return 1; } /* Split rest into name and substitute */ *ptr++ = _T('\0'); partstrlwr (param); if (ptr[0] == _T('\0')) AddConsoleAlias(param, NULL, _T("cmd.exe")); else AddConsoleAlias(param, ptr, _T("cmd.exe")); return 0; }
/* * MD / MKDIR */ INT cmd_mkdir (LPTSTR param) { LPTSTR *p; INT argc, i; if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_MKDIR_HELP); return 0; } p = split (param, &argc, FALSE, FALSE); if (argc == 0) { ConErrResPuts(STRING_ERROR_REQ_PARAM_MISSING); freep(p); nErrorLevel = 1; return 1; } nErrorLevel = 0; for (i = 0; i < argc; i++) { if (!MakeFullPath(p[i])) { if (GetLastError() == ERROR_PATH_NOT_FOUND) { ConErrResPuts(STRING_MD_ERROR2); } else { ErrorMessage (GetLastError(), _T("MD")); } nErrorLevel = 1; } } freep (p); return nErrorLevel; }
static INT CommandCls( PCONSOLE_STATE State, LPSTR param) { #if 0 HANDLE hOutput; COORD coPos; DWORD dwWritten; #if 0 if (!strncmp(param, "/?", 2)) { ConOutResPaging(TRUE,STRING_CLS_HELP); return 0; } #endif coPos.X = 0; coPos.Y = 0; hOutput = GetStdHandle(STD_OUTPUT_HANDLE); FillConsoleOutputAttribute(hOutput, csbi.wAttributes, State->maxx * State->maxy, coPos, &dwWritten); FillConsoleOutputCharacter(hOutput, ' ', State->maxx * State->maxy, coPos, &dwWritten); SetConsoleCursorPosition(hOutput, coPos); #endif CONSOLE_ClearScreen(); CONSOLE_SetCursorXY(0, 0); return 0; }
INT CommandMsgbox (LPTSTR param) { //used to parse command line LPTSTR tmp; //used to find window title (used as messagebox title) //and to find window handle to pass to MessageBox HWND hWnd; TCHAR buff[128]; //these are MessabeBox() parameters LPTSTR title, prompt = ""; UINT uType = U_TYPE_INIT; /* set default title to window title */ GetConsoleTitle(buff, 128); title = buff; if (_tcsncmp (param, _T("/?"), 2) == 0) { ConOutResPaging(TRUE,STRING_MSGBOX_HELP); return 0; } //yes here things are quite massed up :) //skip spaces while(_istspace(*param)) param++; //search for type of messagebox (ok, okcancel, ...) if (_tcsnicmp(param, _T("ok "), 3) == 0) { uType |= MB_ICONEXCLAMATION | MB_OK; param += 3; } else if (_tcsnicmp(param, _T("okcancel "), 9) == 0) { uType |= MB_ICONQUESTION | MB_OKCANCEL; param += 9; } else if (_tcsnicmp(param, _T("yesno "), 6) == 0) { uType |= MB_ICONQUESTION | MB_YESNO; param += 6; } else if (_tcsnicmp(param, _T("yesnocancel "), 12) == 0) { uType |= MB_ICONQUESTION | MB_YESNOCANCEL; param += 12; } else { #ifdef _SYNTAX_CHECK error_req_param_missing (); return 1; #else uType |= MB_ICONEXCLAMATION | MB_OK; #endif } //skip spaces while(_istspace(*param)) param++; #ifdef _SYNTAX_CHECK //if reached end of string //it is an error becuase we do not yet have prompt if (*param == 0) { error_req_param_missing (); return 1; } #endif //search for "title" tmp = param; if (*param == '"') { tmp = _tcschr(param + 1, '"'); if (tmp) { *tmp = 0; title = param + 1; tmp++; param = tmp; } } //skip spaces while(_istspace(*param)) param++; #ifdef _SYNTAX_CHECK //get prompt if (*param == 0) { error_req_param_missing (); return 1; } #endif prompt = param; hWnd=GetConsoleWindow (); switch (MessageBox(hWnd, prompt, title, uType)) { case IDYES: case IDOK: nErrorLevel = 10; break; case IDNO: nErrorLevel = 11; break; case IDCANCEL: nErrorLevel = 12; break; } return 0; }
INT CommandDelete (LPTSTR param) { /*cmd is the command that was given, in this case it will always be "del" or "delete" param is whatever is given after the command*/ LPTSTR *arg = NULL; INT args; INT i; INT nEvalArgs = 0; /* nunber of evaluated arguments */ DWORD dwFlags = 0; DWORD dwAttrFlags = 0; DWORD dwFiles = 0; LONG ch; TCHAR szOrginalArg[MAX_PATH]; /*checks the first two chars of param to see if it is /? this however allows the following command to not show help "del frog.txt /?" */ if (!StringsLoaded) { LoadStrings(); } if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_DEL_HELP1); return 0; } nErrorLevel = 0; arg = split (param, &args, FALSE); if (args == 0) { /* only command given */ error_req_param_missing (); freep (arg); return 1; } /* check for options anywhere in command line */ for (i = 0; i < args; i++) { if (*arg[i] == _T('/')) { /*found a command, but check to make sure it has something after it*/ if (_tcslen (arg[i]) >= 2) { ch = _totupper (arg[i][1]); if (ch == _T('N')) { dwFlags |= DEL_NOTHING; } else if (ch == _T('P')) { dwFlags |= DEL_PROMPT; } else if (ch == _T('Q')) { dwFlags |= DEL_QUIET; } else if (ch == _T('F')) { dwFlags |= DEL_FORCE; } else if (ch == _T('S')) { dwFlags |= DEL_SUBDIR; } else if (ch == _T('T')) { dwFlags |= DEL_TOTAL; } else if (ch == _T('W')) { dwFlags |= DEL_WIPE; } else if (ch == _T('Y')) { dwFlags |= DEL_YES; } else if (ch == _T('A')) { dwFlags |= DEL_ATTRIBUTES; /*the proper syntax for /A has a min of 4 chars i.e. /A:R or /A:-H */ if (_tcslen (arg[i]) < 4) { error_invalid_parameter_format(arg[i]); return 0; } ch = _totupper (arg[i][3]); if (_tcslen (arg[i]) == 4) { if(ch == _T('A')) { dwAttrFlags |= ATTR_ARCHIVE; } if(ch == _T('H')) { dwAttrFlags |= ATTR_HIDDEN; } if(ch == _T('S')) { dwAttrFlags |= ATTR_SYSTEM; } if(ch == _T('R')) { dwAttrFlags |= ATTR_READ_ONLY; } } if (_tcslen (arg[i]) == 5) { if(ch == _T('-')) { ch = _totupper (arg[i][4]); if(ch == _T('A')) { dwAttrFlags |= ATTR_N_ARCHIVE; } if(ch == _T('H')) { dwAttrFlags |= ATTR_N_HIDDEN; } if(ch == _T('S')) { dwAttrFlags |= ATTR_N_SYSTEM; } if(ch == _T('R')) { dwAttrFlags |= ATTR_N_READ_ONLY; } } } } } nEvalArgs++; } } /* there are only options on the command line --> error!!! there is the same number of args as there is flags, so none of the args were filenames*/ if (args == nEvalArgs) { error_req_param_missing (); freep (arg); return 1; } /* keep quiet within batch files */ if (bc != NULL) dwFlags |= DEL_QUIET; /* check for filenames anywhere in command line */ for (i = 0; i < args && !(dwFiles & 0x80000000); i++) { /*this checks to see if it isnt a flag, if it isnt, we assume it is a file name*/ if((*arg[i] == _T('/')) || (*arg[i] == _T('-'))) continue; /* We want to make a copies of the argument */ if(_tcslen(arg[i]) == 2 && arg[i][1] == _T(':')) { /* Check for C: D: ... */ GetRootPath(arg[i],szOrginalArg,MAX_PATH); } else { _tcscpy(szOrginalArg,arg[i]); } dwFiles += ProcessDirectory(szOrginalArg, &dwFlags, dwAttrFlags); } freep (arg); /*Based on MS cmd, we only tell what files are being deleted when /S is used */ if (dwFlags & DEL_TOTAL) { dwFiles &= 0x7fffffff; if (dwFiles < 2) { ConOutResPrintf(STRING_DEL_HELP3, dwFiles); } else { ConOutResPrintf(STRING_DEL_HELP4, dwFiles); } } return 0; }
/* * help * * displays help screen for dir * Rob Lake */ static VOID DirHelp(VOID) { ConOutResPaging(TRUE, STRING_DIR_HELP1); }
INT cmd_time (LPTSTR param) { LPTSTR *arg; INT argc; INT i; INT nTimeString = -1; if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_TIME_HELP1); return 0; } nErrorLevel = 0; /* build parameter array */ arg = split (param, &argc, FALSE, FALSE); /* check for options */ for (i = 0; i < argc; i++) { if (_tcsicmp (arg[i], _T("/t")) == 0) { /* Display current time in short format */ SYSTEMTIME st; TCHAR szTime[20]; GetLocalTime(&st); FormatTime(szTime, &st); ConOutPuts(szTime); freep(arg); return 0; } if ((*arg[i] != _T('/')) && (nTimeString == -1)) nTimeString = i; } if (nTimeString == -1) { ConOutResPrintf(STRING_LOCALE_HELP1); ConOutPrintf(_T(": %s\n"), GetTimeString()); } while (1) { if (nTimeString == -1) { TCHAR s[40]; ConOutResPuts(STRING_TIME_HELP2); ConInString (s, 40); TRACE ("\'%s\'\n", debugstr_aw(s)); while (*s && s[_tcslen (s) - 1] < _T(' ')) s[_tcslen(s) - 1] = _T('\0'); if (ParseTime (s)) { freep (arg); return 0; } } else { if (ParseTime (arg[nTimeString])) { freep (arg); return 0; } /* force input the next time around. */ nTimeString = -1; } ConErrResPuts(STRING_TIME_ERROR1); nErrorLevel = 1; } freep (arg); return 0; }
INT cmd_replace (LPTSTR param) { LPTSTR *arg; INT argc, i,filesReplaced = 0, nFiles, srcIndex = -1, destIndex = -1; DWORD dwFlags = 0; TCHAR szDestPath[MAX_PATH], szSrcPath[MAX_PATH], tmpSrcPath[MAX_PATH]; BOOL doMore = TRUE; /* Help wanted? */ if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_REPLACE_HELP1); return 0; } /* Divide the argument in to an array of c-strings */ arg = split (param, &argc, FALSE, FALSE); nFiles = argc; /* Read options */ for (i = 0; i < argc; i++) { if (arg[i][0] == _T('/')) { if (_tcslen(arg[i]) == 2) { switch (_totupper(arg[i][1])) { case _T('A'): dwFlags |= REPLACE_ADD; break; case _T('P'): dwFlags |= REPLACE_CONFIRM; break; case _T('R'): dwFlags |= REPLACE_READ_ONLY; break; case _T('S'): dwFlags |= REPLACE_SUBDIR; break; case _T('W'): dwFlags |= REPLACE_DISK; break; case _T('U'): dwFlags |= REPLACE_UPDATE; break; default: invalid_switch(arg[i]); return 0; } } else { invalid_switch(arg[i]); freep(arg); return 0; } nFiles--; } else { if (srcIndex == -1) { srcIndex = i; } else if (destIndex == -1) { destIndex = i; } else { invalid_switch(arg[i]); freep(arg); return 0; } } } /* See so that at least source is there */ if (nFiles < 1) { ConOutResPaging(TRUE,STRING_REPLACE_HELP2); ConOutResPaging(TRUE,STRING_REPLACE_HELP3); freep(arg); return 1; } /* Check so that not both update and add switch is added and subdir */ if ((dwFlags & REPLACE_UPDATE || dwFlags & REPLACE_SUBDIR) && (dwFlags & REPLACE_ADD)) { ConOutResPaging(TRUE,STRING_REPLACE_ERROR4); ConOutResPaging(TRUE,STRING_REPLACE_HELP7); freep(arg); return 1; } /* If we have a destination get the full path */ if (destIndex != -1) { if (_tcslen(arg[destIndex]) == 2 && arg[destIndex][1] == ':') GetRootPath(arg[destIndex],szDestPath,MAX_PATH); else { /* Check for wildcards in destination directory */ if (_tcschr (arg[destIndex], _T('*')) != NULL || _tcschr (arg[destIndex], _T('?')) != NULL) { ConOutResPrintf(STRING_REPLACE_ERROR2,arg[destIndex]); ConOutResPaging(TRUE,STRING_REPLACE_HELP3); freep(arg); return 1; } getPath(szDestPath, arg[destIndex]); /* Make sure that destination exists */ if (!IsExistingDirectory(szDestPath)) { ConOutResPrintf(STRING_REPLACE_ERROR2, szDestPath); ConOutResPaging(TRUE,STRING_REPLACE_HELP3); freep(arg); return 1; } } } else { /* Dest is current dir */ GetCurrentDirectory(MAX_PATH,szDestPath); } /* Get the full source path */ if (!(_tcslen(arg[srcIndex]) == 2 && arg[srcIndex][1] == ':')) getPath(szSrcPath, arg[srcIndex]); else _tcscpy(szSrcPath,arg[srcIndex]); /* Source does not have wildcards */ if (_tcschr (arg[srcIndex], _T('*')) == NULL && _tcschr (arg[srcIndex], _T('?')) == NULL) { /* Check so that source is not a directory, because that is not allowed */ if (IsExistingDirectory(szSrcPath)) { ConOutResPrintf(STRING_REPLACE_ERROR6, szSrcPath); ConOutResPaging(TRUE,STRING_REPLACE_HELP3); freep(arg); return 1; } /* Check if the file exists */ if (!IsExistingFile(szSrcPath)) { ConOutResPaging(TRUE,STRING_REPLACE_HELP3); freep(arg); return 1; } } /* /w switch is set so wait for any key to be pressed */ if (dwFlags & REPLACE_DISK) { msg_pause(); cgetchar(); } /* Add an extra \ to the destination path if needed */ if (szDestPath[_tcslen(szDestPath) - 1] != _T('\\')) _tcscat(szDestPath, _T("\\")); /* Save source path */ _tcscpy(tmpSrcPath,szSrcPath); /* Replace in dest dir */ filesReplaced += recReplace(dwFlags, tmpSrcPath, szDestPath, &doMore); /* If subdir switch is set replace in the subdirs to */ if (dwFlags & REPLACE_SUBDIR && doMore) { filesReplaced += recFindSubDirs(dwFlags, szSrcPath, szDestPath, &doMore); } /* If source == dest write no more */ if (filesReplaced != -1) { /* No files replaced */ if (filesReplaced==0) { /* Add switch dependent output */ if (dwFlags & REPLACE_ADD) ConOutResPaging(TRUE,STRING_REPLACE_HELP7); else ConOutResPaging(TRUE,STRING_REPLACE_HELP3); } /* Some files replaced */ else { /* Add switch dependent output */ if (dwFlags & REPLACE_ADD) ConOutResPrintf(STRING_REPLACE_HELP8, filesReplaced); else ConOutResPrintf(STRING_REPLACE_HELP4, filesReplaced); } } /* Return memory */ freep(arg); return 1; }
/* makes the replace */ INT replace(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], DWORD dwFlags, BOOL *doMore) { TCHAR d[MAX_PATH]; TCHAR s[MAX_PATH]; HANDLE hFileSrc, hFileDest; DWORD dwAttrib, dwRead, dwWritten; LPBYTE buffer; BOOL bEof = FALSE; FILETIME srcCreationTime, destCreationTime, srcLastAccessTime, destLastAccessTime; FILETIME srcLastWriteTime, destLastWriteTime; GetPathCase(source, s); GetPathCase(dest, d); s[0] = _totupper(s[0]); d[0] = _totupper(d[0]); // ConOutPrintf(_T("old-src: %s\n"), s); // ConOutPrintf(_T("old-dest: %s\n"), d); // ConOutPrintf(_T("src: %s\n"), source); // ConOutPrintf(_T("dest: %s\n"), dest); /* Open up the sourcefile */ hFileSrc = CreateFile (source, GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, 0, NULL); if (hFileSrc == INVALID_HANDLE_VALUE) { ConOutResPrintf(STRING_COPY_ERROR1, source); return 0; } /* * Get the time from source file to be used in the comparison * with dest time if update switch is set. */ GetFileTime (hFileSrc, &srcCreationTime, &srcLastAccessTime, &srcLastWriteTime); /* * Retrieve the source attributes so that they later on * can be inserted in to the destination. */ dwAttrib = GetFileAttributes (source); if (IsExistingFile (dest)) { /* * Resets the attributes to avoid probles with read only files, * checks for read only has been made earlier. */ SetFileAttributes(dest,FILE_ATTRIBUTE_NORMAL); /* * Is the update flas set? The time has to be controled so that * only older files are replaced. */ if (dwFlags & REPLACE_UPDATE) { /* Read destination time */ hFileDest = CreateFile(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (hFileSrc == INVALID_HANDLE_VALUE) { ConOutResPrintf(STRING_COPY_ERROR1, dest); return 0; } /* Compare time */ GetFileTime (hFileDest, &destCreationTime, &destLastAccessTime, &destLastWriteTime); if (!((srcLastWriteTime.dwHighDateTime > destLastWriteTime.dwHighDateTime) || (srcLastWriteTime.dwHighDateTime == destLastWriteTime.dwHighDateTime && srcLastWriteTime.dwLowDateTime > destLastWriteTime.dwLowDateTime))) { CloseHandle (hFileSrc); CloseHandle (hFileDest); return 0; } CloseHandle (hFileDest); } /* Delete the old file */ DeleteFile (dest); } /* Check confirm flag, and take appropriate action */ if (dwFlags & REPLACE_CONFIRM) { /* Output depending on add flag */ if (dwFlags & REPLACE_ADD) ConOutResPrintf(STRING_REPLACE_HELP9, dest); else ConOutResPrintf(STRING_REPLACE_HELP10, dest); if ( !FilePromptYNA (0)) return 0; } /* Output depending on add flag */ if (dwFlags & REPLACE_ADD) ConOutResPrintf(STRING_REPLACE_HELP11, dest); else ConOutResPrintf(STRING_REPLACE_HELP5, dest); /* Make sure source and destination is not the same */ if (!_tcscmp(s, d)) { ConOutResPaging(TRUE, STRING_REPLACE_ERROR7); CloseHandle (hFileSrc); *doMore = FALSE; return 0; } /* Open destination file to write to */ hFileDest = CreateFile (dest, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if (hFileDest == INVALID_HANDLE_VALUE) { CloseHandle (hFileSrc); ConOutResPaging(TRUE, STRING_REPLACE_ERROR7); *doMore = FALSE; return 0; } /* Get buffer for the copy process */ buffer = VirtualAlloc(NULL, BUFF_SIZE, MEM_COMMIT, PAGE_READWRITE); if (buffer == NULL) { CloseHandle (hFileDest); CloseHandle (hFileSrc); ConOutResPaging(TRUE, STRING_ERROR_OUT_OF_MEMORY); return 0; } /* Put attribute and time to the new destination file */ SetFileAttributes (dest, dwAttrib); SetFileTime (hFileDest, &srcCreationTime, &srcLastAccessTime, &srcLastWriteTime); do { /* Read data from source */ ReadFile (hFileSrc, buffer, BUFF_SIZE, &dwRead, NULL); /* Done? */ if (dwRead == 0) break; /* Write to destination file */ WriteFile (hFileDest, buffer, dwRead, &dwWritten, NULL); /* Done! or ctrl break! */ if (dwWritten != dwRead || CheckCtrlBreak(BREAK_INPUT)) { ConOutResPuts(STRING_COPY_ERROR3); VirtualFree (buffer, 0, MEM_RELEASE); CloseHandle (hFileDest); CloseHandle (hFileSrc); nErrorLevel = 1; return 0; } } while (!bEof); /* Return memory and close files */ VirtualFree (buffer, 0, MEM_RELEASE); CloseHandle (hFileDest); CloseHandle (hFileSrc); /* Return one file replaced */ return 1; }
INT cmd_set (LPTSTR param) { LPTSTR p; LPTSTR lpEnv; LPTSTR lpOutput; if ( !_tcsncmp (param, _T("/?"), 2) ) { ConOutResPaging(TRUE,STRING_SET_HELP); return 0; } param = (LPTSTR)skip_ws(param); /* if no parameters, show the environment */ if (param[0] == _T('\0')) { lpEnv = (LPTSTR)GetEnvironmentStrings (); if (lpEnv) { lpOutput = lpEnv; while (*lpOutput) { if (*lpOutput != _T('=')) ConOutPuts(lpOutput); lpOutput += _tcslen(lpOutput) + 1; } FreeEnvironmentStrings (lpEnv); } return 0; } /* the /A does *NOT* have to be followed by a whitespace */ if ( !_tcsnicmp (param, _T("/A"), 2) ) { BOOL Success; StripQuotes(param); Success = seta_eval ( skip_ws(param+2) ); if(!Success) { /*might seem random but this is what windows xp does */ nErrorLevel = 9165; } return !Success; } if (!_tcsnicmp(param, _T("/P"), 2)) { TCHAR value[1023]; param = GetQuotedString((LPTSTR)skip_ws(param + 2)); p = _tcschr(param, _T('=')); if (!p) { ConErrResPuts(STRING_SYNTAX_COMMAND_INCORRECT); nErrorLevel = 1; return 1; } *p++ = _T('\0'); ConOutPrintf(_T("%s"), GetQuotedString(p)); ConInString(value, 1023); if (!*value || !SetEnvironmentVariable(param, value)) { nErrorLevel = 1; return 1; } return 0; } param = GetQuotedString(param); p = _tcschr (param, _T('=')); if (p) { /* set or remove environment variable */ if (p == param) { /* handle set =val case */ ConErrResPuts(STRING_SYNTAX_COMMAND_INCORRECT); nErrorLevel = 1; return 1; } *p++ = _T('\0'); if (!SetEnvironmentVariable(param, *p ? p : NULL)) { nErrorLevel = 1; return 1; } } else { /* display all environment variable with the given prefix */ BOOL bFound = FALSE; while (_istspace(*param) || *param == _T(',') || *param == _T(';')) param++; p = _tcsrchr(param, _T(' ')); if (!p) p = param + _tcslen(param); *p = _T('\0'); lpEnv = GetEnvironmentStrings(); if (lpEnv) { lpOutput = lpEnv; while (*lpOutput) { if (!_tcsnicmp(lpOutput, param, p - param)) { ConOutPuts(lpOutput); bFound = TRUE; } lpOutput += _tcslen(lpOutput) + 1; } FreeEnvironmentStrings(lpEnv); } if (!bFound) { ConErrResPrintf (STRING_PATH_ERROR, param); nErrorLevel = 1; return 1; } } return 0; }
INT cmd_rmdir (LPTSTR param) { TCHAR ch; INT args; INT dirCount; LPTSTR *arg; INT i; BOOL RD_SUB = FALSE; BOOL RD_QUIET = FALSE; INT res; INT nError = 0; TCHAR szFullPath[MAX_PATH]; if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_RMDIR_HELP); return 0; } arg = split (param, &args, FALSE, FALSE); dirCount = 0; /* check for options anywhere in command line */ for (i = 0; i < args; i++) { if (*arg[i] == _T('/')) { /*found a command, but check to make sure it has something after it*/ if (_tcslen (arg[i]) == 2) { ch = _totupper (arg[i][1]); if (ch == _T('S')) { RD_SUB = TRUE; } else if (ch == _T('Q')) { RD_QUIET = TRUE; } } } else { dirCount++; } } if (dirCount == 0) { /* No folder to remove */ error_req_param_missing(); freep(arg); return 1; } for (i = 0; i < args; i++) { if (*arg[i] == _T('/')) continue; if (RD_SUB) { /* ask if they want to delete evrything in the folder */ if (!RD_QUIET) { res = FilePromptYNA (STRING_DEL_HELP2); if (res == PROMPT_NO || res == PROMPT_BREAK) { nError = 1; continue; } if (res == PROMPT_ALL) RD_QUIET = TRUE; } /* get the folder name */ GetFullPathName(arg[i],MAX_PATH,szFullPath,NULL); /* remove trailing \ if any, but ONLY if dir is not the root dir */ if (_tcslen (szFullPath) >= 2 && szFullPath[_tcslen (szFullPath) - 1] == _T('\\')) szFullPath[_tcslen(szFullPath) - 1] = _T('\0'); res = DeleteFolder(szFullPath); } else { res = RemoveDirectory(arg[i]); } if (!res) { /* Couldn't delete the folder, print out the error */ nError = GetLastError(); ErrorMessage(nError, _T("RD")); } } freep (arg); return nError; }
INT cmd_date (LPTSTR param) { LPTSTR *arg; INT argc; INT i; BOOL bPrompt = TRUE; INT nDateString = -1; if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_DATE_HELP4); return 0; } nErrorLevel = 0; /* build parameter array */ arg = split (param, &argc, FALSE, FALSE); /* check for options */ for (i = 0; i < argc; i++) { if (_tcsicmp (arg[i], _T("/t")) == 0) bPrompt = FALSE; if ((*arg[i] != _T('/')) && (nDateString == -1)) nDateString = i; } if (nDateString == -1) ConOutPrintf(_T("%s"), GetDateString()); if (!bPrompt) { freep (arg); return 0; } if (nDateString == -1) { while (TRUE) /* forever loop */ { TCHAR s[40]; PrintDateString (); ConInString (s, 40); TRACE ("\'%s\'\n", debugstr_aw(s)); while (*s && s[_tcslen (s) - 1] < _T(' ')) s[_tcslen (s) - 1] = _T('\0'); if (ParseDate (s)) { freep (arg); return 0; } ConErrResPuts(STRING_DATE_ERROR); } } else { if (!ParseDate (arg[nDateString])) { while (TRUE) /* forever loop */ { TCHAR s[40]; ConErrResPuts(STRING_DATE_ERROR); PrintDateString (); ConInString (s, 40); while (*s && s[_tcslen (s) - 1] < _T(' ')) s[_tcslen (s) - 1] = _T('\0'); if (ParseDate (s)) { freep (arg); return 0; } } } } freep (arg); return 0; }