static BOOL RemoveFile (LPTSTR lpFileName, DWORD dwFlags, WIN32_FIND_DATA* f) { /*This function is called by CommandDelete and does the actual process of deleting the single file*/ if(CheckCtrlBreak(BREAK_INPUT)) return 1; /*check to see if it is read only and if this is done based on /A if it is done by file name, access is denied. However, if it is done using the /A switch you must un-read only the file and allow it to be deleted*/ if((dwFlags & DEL_ATTRIBUTES) || (dwFlags & DEL_FORCE)) { if(f->dwFileAttributes & FILE_ATTRIBUTE_READONLY) { /*setting file to normal, not saving old attrs first because the file is going to be deleted anyways so the only thing that matters is that it isnt read only.*/ SetFileAttributes(lpFileName,FILE_ATTRIBUTE_NORMAL); } } if (dwFlags & DEL_WIPE) { HANDLE file; DWORD temp; #define BufferSize 65536 BYTE buffer[BufferSize]; LONGLONG i; LARGE_INTEGER FileSize; FileSize.u.HighPart = f->nFileSizeHigh; FileSize.u.LowPart = f->nFileSizeLow; for(i = 0; i < BufferSize; i++) { buffer[i]=rand() % 256; } file = CreateFile (lpFileName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL); if (file != INVALID_HANDLE_VALUE) { for(i = 0; i < (FileSize.QuadPart - BufferSize); i += BufferSize) { WriteFile (file, buffer, BufferSize, &temp, NULL); ConOutPrintf (_T("%I64d%% %s\r"),(i * (LONGLONG)100)/FileSize.QuadPart,szDeleteWipe); } WriteFile (file, buffer, (DWORD)(FileSize.QuadPart - i), &temp, NULL); ConOutPrintf (_T("100%% %s\n"),szDeleteWipe); CloseHandle (file); } } return DeleteFile (lpFileName); }
VOID PrintCommandList(VOID) { LPCOMMAND cmdptr; INT y; y = 0; cmdptr = cmds; while (cmdptr->name) { if (!(cmdptr->flags & CMD_HIDE)) { if (++y == 8) { ConOutPuts(cmdptr->name); y = 0; } else { ConOutPrintf (_T("%-10s"), cmdptr->name); } } cmdptr++; } if (y != 0) ConOutChar(_T('\n')); }
static VOID PrintAlias (VOID) { LPTSTR Aliases; LPTSTR ptr; DWORD len; len = GetConsoleAliasesLength(_T("cmd.exe")); if (len == 0) return; /* allocate memory for an extra \0 char to make parsing easier */ ptr = cmd_alloc(len + sizeof(TCHAR)); if (!ptr) { WARN("Cannot allocate memory for ptr!\n"); return; } Aliases = ptr; ZeroMemory(Aliases, len + sizeof(TCHAR)); if (GetConsoleAliases(Aliases, len, _T("cmd.exe")) != 0) { while (*Aliases != '\0') { ConOutPrintf(_T("%s\n"), Aliases); Aliases = Aliases + lstrlen(Aliases); Aliases++; } } cmd_free(ptr); }
INT CommandEchos (LPTSTR param) { TRACE ("CommandEchos: '%s'\n", debugstr_aw(param)); if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPuts(STRING_ECHO_HELP1); return 0; } ConOutPrintf (_T("%s"), 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; }
static VOID PrintAttribute (LPTSTR pszPath, LPTSTR pszFile, BOOL bRecurse) { WIN32_FIND_DATA findData; HANDLE hFind; TCHAR szFullName[MAX_PATH]; LPTSTR pszFileName; /* prepare full file name buffer */ _tcscpy (szFullName, pszPath); pszFileName = szFullName + _tcslen (szFullName); /* display all subdirectories */ if (bRecurse) { /* append file name */ _tcscpy (pszFileName, pszFile); hFind = FindFirstFile (szFullName, &findData); if (hFind == INVALID_HANDLE_VALUE) { ErrorMessage (GetLastError (), pszFile); return; } do { if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue; if (!_tcscmp (findData.cFileName, _T(".")) || !_tcscmp (findData.cFileName, _T(".."))) continue; _tcscpy (pszFileName, findData.cFileName); _tcscat (pszFileName, _T("\\")); PrintAttribute (szFullName, pszFile, bRecurse); } while (FindNextFile (hFind, &findData)); FindClose (hFind); } /* append file name */ _tcscpy (pszFileName, pszFile); /* display current directory */ hFind = FindFirstFile (szFullName, &findData); if (hFind == INVALID_HANDLE_VALUE) { ErrorMessage (GetLastError (), pszFile); return; } do { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; _tcscpy (pszFileName, findData.cFileName); ConOutPrintf (_T("%c %c%c%c %s\n"), (findData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? _T('A') : _T(' '), (findData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? _T('S') : _T(' '), (findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? _T('H') : _T(' '), (findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? _T('R') : _T(' '), szFullName); } while (FindNextFile (hFind, &findData)); FindClose (hFind); }
/* * CD / CHDIR * */ INT cmd_chdir (LPTSTR param) { BOOL bChangeDrive = FALSE; LPTSTR tmp; TCHAR szCurrent[MAX_PATH]; /* Filter out special cases first */ /* Print Help */ if (!_tcsncmp(param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_CD_HELP); return 0; } /* Remove extra quotes and strip trailing whitespace */ StripQuotes(param); tmp = param + _tcslen(param) - 1; while (tmp > param && _istspace(*tmp)) tmp--; *(tmp + 1) = _T('\0'); /* 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; } ConOutPrintf(_T("%s\n"), szCurrent); return 0; } /* Get Current Directory */ GetCurrentDirectory(MAX_PATH, szCurrent); if (param[0] == _T('\0')) { ConOutPrintf(_T("%s\n"), 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)) { nErrorLevel = 1; return 1; } return 0; }
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_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; }
/* * returns 1 if at least one match, else returns 0 */ BOOL ShowCompletionMatches (LPTSTR str, INT charcount) { WIN32_FIND_DATA file; HANDLE hFile; BOOL found_dot = FALSE; INT curplace = 0; INT count; TCHAR path[MAX_PATH]; TCHAR fname[MAX_PATH]; TCHAR directory[MAX_PATH]; SHORT screenwidth; /* expand current file name */ count = charcount - 1; if (count < 0) count = 0; /* find front of word */ if (str[count] == _T('"')) { count--; while (count > 0 && str[count] != _T('"')) count--; } else { while (count > 0 && str[count] != _T(' ')) count--; } /* if not at beginning, go forward 1 */ if (str[count] == _T(' ')) count++; if (str[count] == _T('"')) count++; /* extract directory from word */ _tcscpy (directory, &str[count]); curplace = _tcslen (directory) - 1; if (curplace >= 0 && directory[curplace] == _T('"')) directory[curplace--] = _T('\0'); _tcscpy (path, directory); while (curplace >= 0 && directory[curplace] != _T('\\') && directory[curplace] != _T(':')) { directory[curplace] = 0; curplace--; } /* look for a . in the filename */ for (count = _tcslen (directory); path[count] != _T('\0'); count++) { if (path[count] == _T('.')) { found_dot = TRUE; break; } } if (found_dot) _tcscat (path, _T("*")); else _tcscat (path, _T("*.*")); /* current fname */ curplace = 0; hFile = FindFirstFile (path, &file); if (hFile != INVALID_HANDLE_VALUE) { UINT longestfname = 0; /* Get the size of longest filename first. */ do { if (_tcslen(file.cFileName) > longestfname) { longestfname = _tcslen(file.cFileName); /* Directories get extra brackets around them. */ if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) longestfname += 2; } } while (FindNextFile (hFile, &file)); FindClose (hFile); hFile = FindFirstFile (path, &file); /* Count the highest number of columns */ GetScreenSize(&screenwidth, NULL); /* For counting columns of output */ count = 0; /* Increase by the number of spaces behind file name */ longestfname += 3; /* find anything */ ConOutChar(_T('\n')); do { /* ignore . and .. */ if (!_tcscmp (file.cFileName, _T(".")) || !_tcscmp (file.cFileName, _T(".."))) continue; if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) _stprintf (fname, _T("[%s]"), file.cFileName); else _tcscpy (fname, file.cFileName); ConOutPrintf (_T("%*s"), - longestfname, fname); count++; /* output as much columns as fits on the screen */ if (count >= (screenwidth / longestfname)) { /* print the new line only if we aren't on the * last column, in this case it wraps anyway */ if (count * longestfname != (UINT)screenwidth) ConOutChar(_T('\n')); count = 0; } } while (FindNextFile (hFile, &file)); FindClose (hFile); if (count) ConOutChar(_T('\n')); } else { /* no match found */ MessageBeep (-1); return FALSE; } return TRUE; }
INT CommandChoice (LPTSTR param) { LPTSTR lpOptions; TCHAR Options[6]; LPTSTR lpText = NULL; BOOL bNoPrompt = FALSE; BOOL bCaseSensitive = FALSE; BOOL bTimeout = FALSE; INT nTimeout = 0; TCHAR cDefault = _T('\0'); INPUT_RECORD ir; LPTSTR p, np; LPTSTR *arg; INT argc; INT i; INT val; INT GCret; TCHAR Ch; DWORD amount,clk; LoadString(CMD_ModuleHandle, STRING_CHOICE_OPTION, Options, 4); lpOptions = Options; if (_tcsncmp (param, _T("/?"), 2) == 0) { ConOutResPaging(TRUE,STRING_CHOICE_HELP); return 0; } /* retrieve text */ p = param; while (TRUE) { if (*p == _T('\0')) break; if (*p != _T('/')) { lpText = p; break; } np = _tcschr (p, _T(' ')); if (!np) break; p = np + 1; } /* build parameter array */ arg = split (param, &argc, FALSE); /* evaluate arguments */ if (argc > 0) { for (i = 0; i < argc; i++) { if (_tcsnicmp (arg[i], _T("/c"), 2) == 0) { if (arg[i][2] == _T(':')) lpOptions = &arg[i][3]; else lpOptions = &arg[i][2]; if (_tcslen (lpOptions) == 0) { ConErrResPuts(STRING_CHOICE_ERROR); freep (arg); return 1; } } else if (_tcsnicmp (arg[i], _T("/n"), 2) == 0) { bNoPrompt = TRUE; } else if (_tcsnicmp (arg[i], _T("/s"), 2) == 0) { bCaseSensitive = TRUE; } else if (_tcsnicmp (arg[i], _T("/t"), 2) == 0) { LPTSTR s; if (arg[i][2] == _T(':')) { cDefault = arg[i][3]; s = &arg[i][4]; } else { cDefault = arg[i][2]; s = &arg[i][3]; } if (*s != _T(',')) { ConErrResPuts(STRING_CHOICE_ERROR_TXT); freep (arg); return 1; } s++; nTimeout = _ttoi(s); bTimeout = TRUE; } else if (arg[i][0] == _T('/')) { ConErrResPrintf(STRING_CHOICE_ERROR_OPTION, arg[i]); freep (arg); return 1; } } } /* print text */ if (lpText) ConOutPrintf (_T("%s"), lpText); /* print options */ if (bNoPrompt == FALSE) { ConOutPrintf (_T("[%c"), lpOptions[0]); for (i = 1; (unsigned)i < _tcslen (lpOptions); i++) ConOutPrintf (_T(",%c"), lpOptions[i]); ConOutPrintf (_T("]?")); } ConInFlush (); if(!bTimeout) { while (TRUE) { ConInKey (&ir); val = IsKeyInString (lpOptions, #ifdef _UNICODE ir.Event.KeyEvent.uChar.UnicodeChar, #else ir.Event.KeyEvent.uChar.AsciiChar, #endif bCaseSensitive); if (val >= 0) { ConOutPrintf (_T("%c\n"), lpOptions[val]); nErrorLevel = val + 1; break; } Beep (440, 50); } freep (arg); TRACE ("ErrorLevel: %d\n", nErrorLevel); return 0; } clk = GetTickCount (); amount = nTimeout*1000; loop: GCret = GetCharacterTimeout (&Ch, amount - (GetTickCount () - clk)); switch (GCret) { case GC_TIMEOUT: TRACE ("GC_TIMEOUT\n"); TRACE ("elapsed %d msecs\n", GetTickCount () - clk); break; case GC_NOKEY: TRACE ("GC_NOKEY\n"); TRACE ("elapsed %d msecs\n", GetTickCount () - clk); goto loop; case GC_KEYREAD: TRACE ("GC_KEYREAD\n"); TRACE ("elapsed %d msecs\n", GetTickCount () - clk); TRACE ("read %c", Ch); if ((val=IsKeyInString(lpOptions,Ch,bCaseSensitive))==-1) { Beep (440, 50); goto loop; } cDefault=Ch; break; } TRACE ("exiting wait loop after %d msecs\n", GetTickCount () - clk); val = IsKeyInString (lpOptions, cDefault, bCaseSensitive); ConOutPrintf (_T("%c\n"), lpOptions[val]); nErrorLevel = val + 1; freep (arg); TRACE ("ErrorLevel: %d\n", nErrorLevel); return 0; }
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; }
/* read in a command line */ BOOL ReadCommand(LPTSTR str, INT maxlen) { CONSOLE_SCREEN_BUFFER_INFO csbi; SHORT orgx; /* origin x/y */ SHORT orgy; SHORT curx; /*current x/y cursor position*/ SHORT cury; SHORT tempscreen; INT count; /*used in some for loops*/ INT current = 0; /*the position of the cursor in the string (str)*/ INT charcount = 0;/*chars in the string (str)*/ INPUT_RECORD ir; #ifdef FEATURE_UNIX_FILENAME_COMPLETION WORD wLastKey = 0; #endif TCHAR ch; BOOL bReturn = FALSE; BOOL bCharInput; #ifdef FEATURE_4NT_FILENAME_COMPLETION TCHAR szPath[MAX_PATH]; #endif #ifdef FEATURE_HISTORY //BOOL bContinue=FALSE;/*is TRUE the second case will not be executed*/ TCHAR PreviousChar; #endif if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { /* No console */ HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD dwRead; CHAR chr; do { if (!ReadFile(hStdin, &chr, 1, &dwRead, NULL) || !dwRead) return FALSE; #ifdef _UNICODE MultiByteToWideChar(InputCodePage, 0, &chr, 1, &str[charcount++], 1); #endif } while (chr != '\n' && charcount < maxlen); str[charcount] = _T('\0'); return TRUE; } /* get screen size */ maxx = csbi.dwSize.X; maxy = csbi.dwSize.Y; curx = orgx = csbi.dwCursorPosition.X; cury = orgy = csbi.dwCursorPosition.Y; memset (str, 0, maxlen * sizeof (TCHAR)); SetCursorType (bInsert, TRUE); do { bReturn = FALSE; ConInKey (&ir); if (ir.Event.KeyEvent.dwControlKeyState & (RIGHT_ALT_PRESSED |LEFT_ALT_PRESSED| RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED) ) { switch (ir.Event.KeyEvent.wVirtualKeyCode) { #ifdef FEATURE_HISTORY case 'K': /*add the current command line to the history*/ if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED)) { if (str[0]) History(0,str); ClearCommandLine (str, maxlen, orgx, orgy); current = charcount = 0; curx = orgx; cury = orgy; //bContinue=TRUE; break; } case 'D': /*delete current history entry*/ if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED)) { ClearCommandLine (str, maxlen, orgx, orgy); History_del_current_entry(str); current = charcount = _tcslen (str); ConOutPrintf (_T("%s"), str); GetCursorXY (&curx, &cury); //bContinue=TRUE; break; } #endif /*FEATURE_HISTORY*/ } } bCharInput = FALSE; switch (ir.Event.KeyEvent.wVirtualKeyCode) { case VK_BACK: /* <BACKSPACE> - delete character to left of cursor */ if (current > 0 && charcount > 0) { if (current == charcount) { /* if at end of line */ str[current - 1] = _T('\0'); if (GetCursorX () != 0) { ConOutPrintf (_T("\b \b")); curx--; } else { SetCursorXY ((SHORT)(maxx - 1), (SHORT)(GetCursorY () - 1)); ConOutChar (_T(' ')); SetCursorXY ((SHORT)(maxx - 1), (SHORT)(GetCursorY () - 1)); cury--; curx = maxx - 1; } } else { for (count = current - 1; count < charcount; count++) str[count] = str[count + 1]; if (GetCursorX () != 0) { SetCursorXY ((SHORT)(GetCursorX () - 1), GetCursorY ()); curx--; } else { SetCursorXY ((SHORT)(maxx - 1), (SHORT)(GetCursorY () - 1)); cury--; curx = maxx - 1; } GetCursorXY (&curx, &cury); ConOutPrintf (_T("%s "), &str[current - 1]); SetCursorXY (curx, cury); } charcount--; current--; } break; case VK_INSERT: /* toggle insert/overstrike mode */ bInsert ^= TRUE; SetCursorType (bInsert, TRUE); break; case VK_DELETE: /* delete character under cursor */ if (current != charcount && charcount > 0) { for (count = current; count < charcount; count++) str[count] = str[count + 1]; charcount--; GetCursorXY (&curx, &cury); ConOutPrintf (_T("%s "), &str[current]); SetCursorXY (curx, cury); } break; case VK_HOME: /* goto beginning of string */ if (current != 0) { SetCursorXY (orgx, orgy); curx = orgx; cury = orgy; current = 0; } break; case VK_END: /* goto end of string */ if (current != charcount) { SetCursorXY (orgx, orgy); ConOutPrintf (_T("%s"), str); GetCursorXY (&curx, &cury); current = charcount; } break; case VK_TAB: #ifdef FEATURE_UNIX_FILENAME_COMPLETION /* expand current file name */ if ((current == charcount) || (current == charcount - 1 && str[current] == _T('"'))) /* only works at end of line*/ { if (wLastKey != VK_TAB) { /* if first TAB, complete filename*/ tempscreen = charcount; CompleteFilename (str, charcount); charcount = _tcslen (str); current = charcount; SetCursorXY (orgx, orgy); ConOutPrintf (_T("%s"), str); if (tempscreen > charcount) { GetCursorXY (&curx, &cury); for (count = tempscreen - charcount; count--; ) ConOutChar (_T(' ')); SetCursorXY (curx, cury); } else { if (((charcount + orgx) / maxx) + orgy > maxy - 1) orgy += maxy - ((charcount + orgx) / maxx + orgy + 1); } /* set cursor position */ SetCursorXY ((orgx + current) % maxx, orgy + (orgx + current) / maxx); GetCursorXY (&curx, &cury); } else { /*if second TAB, list matches*/ if (ShowCompletionMatches (str, charcount)) { PrintPrompt(); GetCursorXY(&orgx, &orgy); ConOutPrintf(_T("%s"), str); /* set cursor position */ SetCursorXY((orgx + current) % maxx, orgy + (orgx + current) / maxx); GetCursorXY(&curx, &cury); } } } else { MessageBeep(-1); } #endif #ifdef FEATURE_4NT_FILENAME_COMPLETION /* used to later see if we went down to the next line */ tempscreen = charcount; szPath[0]=_T('\0'); /* str is the whole things that is on the current line that is and and out. arg 2 is weather it goes back one file or forward one file */ CompleteFilename(str, !(ir.Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED), szPath, current); /* Attempt to clear the line */ ClearCommandLine (str, maxlen, orgx, orgy); curx = orgx; cury = orgy; current = charcount = 0; /* Everything is deleted, lets add it back in */ _tcscpy(str,szPath); /* Figure out where cusor is going to be after we print it */ charcount = _tcslen(str); current = charcount; SetCursorXY(orgx, orgy); /* Print out what we have now */ ConOutPrintf(_T("%s"), str); /* Move cursor accordingly */ if (tempscreen > charcount) { GetCursorXY(&curx, &cury); for(count = tempscreen - charcount; count--; ) ConOutChar(_T(' ')); SetCursorXY(curx, cury); } else { if (((charcount + orgx) / maxx) + orgy > maxy - 1) orgy += maxy - ((charcount + orgx) / maxx + orgy + 1); } SetCursorXY((short)(((int)orgx + current) % maxx), (short)((int)orgy + ((int)orgx + current) / maxx)); GetCursorXY(&curx, &cury); #endif break; case _T('M'): case _T('C'): /* ^M does the same as return */ bCharInput = TRUE; if (!(ir.Event.KeyEvent.dwControlKeyState & (RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED))) { break; } case VK_RETURN: /* end input, return to main */ #ifdef FEATURE_HISTORY /* add to the history */ if (str[0]) History (0, str); #endif str[charcount++] = _T('\n'); str[charcount] = _T('\0'); ConOutChar(_T('\n')); bReturn = TRUE; break; case VK_ESCAPE: /* clear str Make this callable! */ ClearCommandLine (str, maxlen, orgx, orgy); curx = orgx; cury = orgy; current = charcount = 0; break; #ifdef FEATURE_HISTORY case VK_F3: History_move_to_bottom(); #endif case VK_UP: #ifdef FEATURE_HISTORY /* get previous command from buffer */ ClearCommandLine (str, maxlen, orgx, orgy); History (-1, str); current = charcount = _tcslen (str); if (((charcount + orgx) / maxx) + orgy > maxy - 1) orgy += maxy - ((charcount + orgx) / maxx + orgy + 1); ConOutPrintf (_T("%s"), str); GetCursorXY (&curx, &cury); #endif break; case VK_DOWN: #ifdef FEATURE_HISTORY /* get next command from buffer */ ClearCommandLine (str, maxlen, orgx, orgy); History (1, str); current = charcount = _tcslen (str); if (((charcount + orgx) / maxx) + orgy > maxy - 1) orgy += maxy - ((charcount + orgx) / maxx + orgy + 1); ConOutPrintf (_T("%s"), str); GetCursorXY (&curx, &cury); #endif break; case VK_LEFT: /* move cursor left */ if (current > 0) { current--; if (GetCursorX () == 0) { SetCursorXY ((SHORT)(maxx - 1), (SHORT)(GetCursorY () - 1)); curx = maxx - 1; cury--; } else { SetCursorXY ((SHORT)(GetCursorX () - 1), GetCursorY ()); curx--; } } else { MessageBeep (-1); } break; case VK_RIGHT: /* move cursor right */ if (current != charcount) { current++; if (GetCursorX () == maxx - 1) { SetCursorXY (0, (SHORT)(GetCursorY () + 1)); curx = 0; cury++; } else { SetCursorXY ((SHORT)(GetCursorX () + 1), GetCursorY ()); curx++; } } #ifdef FEATURE_HISTORY else { LPCTSTR last = PeekHistory(-1); if (last && charcount < (INT)_tcslen (last)) { PreviousChar = last[current]; ConOutChar(PreviousChar); GetCursorXY(&curx, &cury); str[current++] = PreviousChar; charcount++; } } #endif break; default: /* This input is just a normal char */ bCharInput = TRUE; } #ifdef _UNICODE ch = ir.Event.KeyEvent.uChar.UnicodeChar; if (ch >= 32 && (charcount != (maxlen - 2)) && bCharInput) #else ch = ir.Event.KeyEvent.uChar.AsciiChar; if ((UCHAR)ch >= 32 && (charcount != (maxlen - 2)) && bCharInput) #endif /* _UNICODE */ { /* insert character into string... */ if (bInsert && current != charcount) { /* If this character insertion will cause screen scrolling, * adjust the saved origin of the command prompt. */ tempscreen = _tcslen(str + current) + curx; if ((tempscreen % maxx) == (maxx - 1) && (tempscreen / maxx) + cury == (maxy - 1)) { orgy--; cury--; } for (count = charcount; count > current; count--) str[count] = str[count - 1]; str[current++] = ch; if (curx == maxx - 1) curx = 0, cury++; else curx++; ConOutPrintf (_T("%s"), &str[current - 1]); SetCursorXY (curx, cury); charcount++; } else { if (current == charcount) charcount++; str[current++] = ch; if (GetCursorX () == maxx - 1 && GetCursorY () == maxy - 1) orgy--, cury--; if (GetCursorX () == maxx - 1) curx = 0, cury++; else curx++; ConOutChar (ch); } } //wLastKey = ir.Event.KeyEvent.wVirtualKeyCode; } while (!bReturn); SetCursorType (bInsert, TRUE); #ifdef FEATURE_ALIASES /* expand all aliases */ ExpandAlias (str, maxlen); #endif /* FEATURE_ALIAS */ return TRUE; }
INT CommandTimer (LPTSTR param) { // all timers are kept static DWORD clksT[10]; // timers status // set all the clocks off by default static BOOL clksS[10]={FALSE,FALSE,FALSE,FALSE, FALSE,FALSE,FALSE,FALSE,FALSE,FALSE}; // TRUE if /S in command line BOOL bS = FALSE; // avoid to set clk_n more than once BOOL bCanNSet = TRUE; INT NewClkStatus = NCS_NOT_SPECIFIED; // the clock number specified on the command line // 1 by default INT clk_n=1; // output format INT iFormat=1; // command line parsing variables INT argc; LPTSTR *p; INT i; if (_tcsncmp (param, _T("/?"), 2) == 0) { ConOutResPrintf(STRING_TIMER_HELP3, cTimeSeparator, cTimeSeparator, cDecimalSeparator); return 0; } nErrorLevel = 0; p = split (param, &argc, FALSE, FALSE); //read options for (i = 0; i < argc; i++) { //set timer on if (!(_tcsicmp(&p[i][0],_T("on"))) && NewClkStatus == NCS_NOT_SPECIFIED) { NewClkStatus = NCS_ON; continue; } //set timer off if (!(_tcsicmp(&p[i][0],_T("off"))) && NewClkStatus == NCS_NOT_SPECIFIED) { NewClkStatus = NCS_OFF; continue; } // other options if (p[i][0] == _T('/')) { // set timer number if (_istdigit(p[i][1]) && bCanNSet) { clk_n = p[i][1] - _T('0'); bCanNSet = FALSE; continue; } // set s(plit) option if (_totupper(p[i][1]) == _T('S')) { bS = TRUE; continue; } // specify format if (_totupper(p[i][1]) == _T('F')) { iFormat = p[i][2] - _T('0'); continue; } } } // do stuff (start/stop/read timer) if (NewClkStatus == NCS_ON) { cT=GetTickCount(); cS=TRUE; ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF")); ConOutPuts(GetTimeString()); freep(p); return 0; } if (bS) { if (cS) { ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF")); ConOutPrintf(_T("%s\n"), GetTimeString()); PrintElapsedTime(GetTickCount()-cT, iFormat); freep(p); return 0; } cT=GetTickCount(); cS=TRUE; ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF")); ConOutPuts(GetTimeString()); freep(p); return 0; } if (NewClkStatus == NCS_NOT_SPECIFIED) { if (cS) { cS=FALSE; ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF")); ConOutPrintf(_T("%s\n"), GetTimeString()); PrintElapsedTime(GetTickCount()-cT, iFormat); freep(p); return 0; } cT=GetTickCount(); cS=TRUE; ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF")); ConOutPuts(GetTimeString()); freep(p); return 0; } if (NewClkStatus == NCS_OFF) { if (cS) { cS=FALSE; ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF")); ConOutPrintf(_T("%s\n"), GetTimeString()); PrintElapsedTime(GetTickCount()-cT, iFormat); freep(p); return 0; } ConOutResPrintf (STRING_TIMER_TIME,clk_n,cS?_T("ON"):_T("OFF")); ConOutPuts(GetTimeString()); freep(p); return 0; } freep(p); return 0; }
INT cmd_copy(LPTSTR param) { LPTSTR *arg; INT argc, i, nFiles, nOverwrite = 0, nSrc = -1, nDes = -1; /* this is the path up to the folder of the src and dest ie C:\windows\ */ TCHAR szDestPath[MAX_PATH]; TCHAR szSrcPath[MAX_PATH]; DWORD dwFlags = 0; /* If this is the type of copy where we are adding files */ BOOL bAppend = FALSE; WIN32_FIND_DATA findBuffer; HANDLE hFile = NULL; BOOL bTouch = FALSE; /* Used when something like "copy c*.exe d*.exe" during the process of figuring out the new name */ /* Pointer to keep track of how far through the append input(file1+file2+file3) we are */ TCHAR * appendPointer = _T("\0"); /* The full path to src and dest. This has drive letter, folders, and filename */ TCHAR tmpDestPath[MAX_PATH]; TCHAR tmpSrcPath[MAX_PATH]; /* A bool on weather or not the destination name will be taking from the input */ BOOL bSrcName = FALSE; /* Seems like a waste but it is a pointer used to copy from input to PreserveName */ TCHAR * UseThisName; /* for CMDCOPY env */ TCHAR *evar; int size; TCHAR * szTouch; BOOL bHasWildcard, bDone = FALSE, bMoreFiles = FALSE; BOOL bMultipleSource = FALSE, bMultipleDest = FALSE; /* Show help/usage info */ if (!_tcsncmp(param, _T("/?"), 2)) { ConOutResPaging(TRUE, STRING_COPY_HELP2); return 0; } nErrorLevel = 0; /* Get the envor value if it exists */ evar = cmd_alloc(512 * sizeof(TCHAR)); if (evar == NULL) size = 0; else size = GetEnvironmentVariable (_T("COPYCMD"), evar, 512); if (size > 512) { TCHAR *old_evar = evar; evar = cmd_realloc(evar,size * sizeof(TCHAR) ); if (evar!=NULL) size = GetEnvironmentVariable (_T("COPYCMD"), evar, size); else { size=0; evar = old_evar; } } /* check see if we did get any env variable */ if (size != 0) { int t = 0; /* scan and set the flags */ for (t = 0; t < size; t++) { if (_tcsncicmp(_T("/A"),&evar[t],2) == 0) { dwFlags |=COPY_ASCII; t++; } else if (_tcsncicmp(_T("/B"),&evar[t],2) == 0) { dwFlags |= COPY_BINARY; t++; } else if (_tcsncicmp(_T("/D"),&evar[t],2) == 0) { dwFlags |= COPY_DECRYPT; t++; } else if (_tcsncicmp(_T("/V"),&evar[t],2) == 0) { dwFlags |= COPY_VERIFY; t++; } else if (_tcsncicmp(_T("/N"),&evar[t],2) == 0) { dwFlags |= COPY_SHORTNAME; t++; } else if (_tcsncicmp(_T("/Y"),&evar[t],2) == 0) { dwFlags |= COPY_NO_PROMPT; t++; } else if (_tcsncicmp(_T("/-Y"),&evar[t],3) == 0) { dwFlags |= COPY_PROMPT; t+=2; } else if (_tcsncicmp(_T("/Z"),&evar[t],2) == 0) { dwFlags |= COPY_PROMPT; t++; } } } cmd_free(evar); /* Split the user input into array */ arg = split(param, &argc, FALSE, TRUE); nFiles = argc; /* Read switches and count files */ for (i = 0; i < argc; i++) { if (*arg[i] == _T('/')) { if (_tcslen(arg[i]) >= 2) { switch (_totupper(arg[i][1])) { case _T('A'): dwFlags |= COPY_ASCII; break; case _T('B'): dwFlags |= COPY_BINARY; break; case _T('D'): dwFlags |= COPY_DECRYPT; break; case _T('V'): dwFlags |= COPY_VERIFY; break; case _T('N'): dwFlags |= COPY_SHORTNAME; break; case _T('Y'): dwFlags |= COPY_NO_PROMPT; dwFlags &= ~COPY_PROMPT; break; case _T('-'): if (_tcslen(arg[i]) >= 3) if (_totupper(arg[i][2]) == _T('Y')) { dwFlags &= ~COPY_NO_PROMPT; dwFlags |= COPY_PROMPT; } break; case _T('Z'): dwFlags |= COPY_RESTART; break; default: /* Invalid switch */ ConOutResPrintf(STRING_ERROR_INVALID_SWITCH, _totupper(arg[i][1])); nErrorLevel = 1; freep (arg); return 1; break; } } /* If it was a switch, subtract from total arguments */ nFiles--; } else { /* If it isn't a switch then it is the source or destination */ if (nSrc == -1) { nSrc = i; } else if (*arg[i] == _T('+')) { /* Next file should be appended */ bMoreFiles = TRUE; nFiles -= 1; } else if (bMoreFiles) { /* Add this file to the source string this way we can do all checks directly on source string later on */ TCHAR * ptr; int length = (_tcslen(arg[nSrc]) + _tcslen(arg[i]) + 2) * sizeof(TCHAR); ptr = cmd_alloc(length); if (ptr) { _tcscpy(ptr, arg[nSrc]); _tcscat(ptr, _T("|")); _tcscat(ptr, arg[i]); cmd_free(arg[nSrc]); arg[nSrc] = ptr; nFiles -= 1; } bMoreFiles = FALSE; } else if (nDes == -1) { nDes = i; } } } /* keep quiet within batch files */ if (bc != NULL) { dwFlags |= COPY_NO_PROMPT; dwFlags &= ~COPY_PROMPT; } if (nFiles < 1) { /* There are not enough files, there has to be at least 1 */ ConOutResPuts(STRING_ERROR_REQ_PARAM_MISSING); freep(arg); return 1; } if (nFiles > 2) { /* There are too many file names in command */ ConErrResPrintf(STRING_ERROR_TOO_MANY_PARAMETERS,_T("")); nErrorLevel = 1; freep(arg); return 1; } if ((_tcschr(arg[nSrc], _T('|')) != NULL) || (_tcschr(arg[nSrc], _T('*')) != NULL) || (_tcschr(arg[nSrc], _T('?')) != NULL) || IsExistingDirectory(arg[nSrc])) { bMultipleSource = TRUE; } /* Reusing the number of files variable */ nFiles = 0; /* Check if no destination argument is passed */ if (nDes == -1) { /* If no destination was entered then just use the current directory as the destination */ GetCurrentDirectory(MAX_PATH, szDestPath); } else { /* Check if the destination is 'x:' */ if ((arg[nDes][1] == _T(':')) && (arg[nDes][2] == _T('\0'))) { GetRootPath(arg[nDes], szDestPath, MAX_PATH); } else { /* If the user entered two file names then form the full string path */ GetFullPathName(arg[nDes], MAX_PATH, szDestPath, NULL); } /* Make sure there is an ending slash to the path if the dest is a folder */ if ((_tcschr(szDestPath, _T('*')) == NULL) && IsExistingDirectory(szDestPath)) { bMultipleDest = TRUE; if (szDestPath[_tcslen(szDestPath) - 1] != _T('\\')) _tcscat(szDestPath, _T("\\")); } /* Check if the destination uses wildcards */ if ((_tcschr(arg[nDes], _T('*')) != NULL) || (_tcschr(arg[nDes], _T('?')) != NULL)) { bMultipleDest = TRUE; } } if (nDes != -1) /* you can only append files when there is a destination */ { if (bMultipleSource && !bMultipleDest) { /* We have multiple source files, but not multiple destination files. This means we are appending the soruce files. */ bAppend = TRUE; if (_tcschr(arg[nSrc], _T('|')) != NULL) appendPointer = arg[nSrc]; } } /* Save the name the user entered */ UseThisName = _tcsrchr(szDestPath,_T('\\')); if (UseThisName) { /* Split the name from the path */ *UseThisName++ = _T('\0'); /* Check if the dest path ends with '\*' or '\' */ if (((UseThisName[0] == _T('*')) && (UseThisName[1] == _T('\0'))) || (UseThisName[0] == _T('\0'))) { /* In this case we will be using the same name as the source file for the destination file because destination is a folder */ bSrcName = TRUE; UseThisName = NULL; } } else { /* Something's seriously wrong! */ UseThisName = szDestPath; } do { /* Get the full string of the path to the source file */ if (_tcschr(arg[nSrc], _T('|')) != NULL) { /* Reset the source path */ szSrcPath[0] = _T('\0'); /* Loop through the source file name and copy all the chars one at a time until it gets too + */ while(TRUE) { if (appendPointer[0] == _T('|')) { /* Skip the | and go to the next file name */ appendPointer++; break; } else if (appendPointer[0] == _T('\0')) { bDone = TRUE; break; } _tcsncat(szSrcPath, appendPointer, 1); appendPointer++; } if (_tcschr(arg[nSrc], _T(',')) != NULL) { /* Only time there is a , in the source is when they are using touch Cant have a destination and can only have on ,, at the end of the string Cant have more then one file name */ szTouch = _tcsstr(arg[nSrc], _T("|")); if (_tcsncmp(szTouch,_T("|,,\0"), 4) || (nDes != -1)) { ConErrResPrintf(STRING_ERROR_INVALID_PARAM_FORMAT,arg[nSrc]); nErrorLevel = 1; freep (arg); return 1; } bTouch = TRUE; bDone = TRUE; } } else { bDone = TRUE; _tcscpy(szSrcPath, arg[nSrc]); } /* "x:" is not a valid source path format. */ if ((szSrcPath[1] == _T(':')) && (szSrcPath[2] == _T('\0'))) { ConOutPrintf(_T("%s\n"), szSrcPath); ConOutFormatMessage(ERROR_FILE_NOT_FOUND, szSrcPath); nErrorLevel = 1; break; } /* From this point on, we can assume that the shortest path is 3 letters long and that would be [DriveLetter]:\ */ /* Check if the path has a wildcard */ bHasWildcard = (_tcschr(szSrcPath, _T('*')) != NULL); /* If there is no * in the path name and it is a folder then we will need to add a wildcard to the pathname so FindFirstFile comes up with all the files in that folder */ if (!bHasWildcard && IsExistingDirectory(szSrcPath)) { /* If it doesnt have a \ at the end already then on needs to be added */ if (szSrcPath[_tcslen(szSrcPath) - 1] != _T('\\')) _tcscat(szSrcPath, _T("\\")); _tcscat(szSrcPath, _T("*")); bHasWildcard = TRUE; } /* If the path ends with '\' add a wildcard at the end */ if (szSrcPath[_tcslen(szSrcPath) - 1] == _T('\\')) { _tcscat(szSrcPath, _T("*")); bHasWildcard = TRUE; } /* Get a list of all the files */ hFile = FindFirstFile(szSrcPath, &findBuffer); /* If it couldnt open the file handle, print out the error */ if (hFile == INVALID_HANDLE_VALUE) { /* only print source name when more then one file */ if (bMultipleSource) ConOutPrintf(_T("%s\n"), szSrcPath); ConOutFormatMessage(GetLastError(), szSrcPath); freep(arg); nErrorLevel = 1; return 1; } /* Strip the paths back to the folder they are in */ for (i = (_tcslen(szSrcPath) - 1); i > -1; i--) if (szSrcPath[i] != _T('\\')) szSrcPath[i] = _T('\0'); else break; do { /* Check Breaker */ if (CheckCtrlBreak(BREAK_INPUT)) { FindClose(hFile); freep(arg); return 1; } /* Set the override to yes each new file */ nOverwrite = 1; /* Ignore the . and .. files */ if (!_tcscmp(findBuffer.cFileName, _T(".")) || !_tcscmp(findBuffer.cFileName, _T("..")) || findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { continue; } /* Copy the base folder over to a tmp string */ _tcscpy(tmpDestPath, szDestPath); _tcscat(tmpDestPath, _T("\\")); /* Can't put a file into a folder that isn't there */ if (_tcscmp(tmpDestPath, _T("\\\\.\\")) && !IsExistingDirectory(tmpDestPath)) { FindClose(hFile); ConOutFormatMessage(GetLastError(), szSrcPath); freep(arg); nErrorLevel = 1; return 1; } /* Copy over the destination path name */ if (bSrcName) _tcscat(tmpDestPath, findBuffer.cFileName); else { /* If there is no wildcard you can use the name the user entered */ if ((_tcschr(UseThisName, _T('*')) == NULL) && (_tcschr(UseThisName, _T('?')) == NULL)) { _tcscat(tmpDestPath, UseThisName); } else { TCHAR DoneFile[MAX_PATH]; BuildFileName(findBuffer.cFileName, UseThisName, DoneFile); /* Add the filename to the tmp string path */ _tcscat(tmpDestPath, DoneFile); } } /* Build the string path to the source file */ _tcscpy(tmpSrcPath,szSrcPath); _tcscat (tmpSrcPath, findBuffer.cFileName); /* Check to see if the file is the same file */ if (!bTouch && !_tcscmp(tmpSrcPath, tmpDestPath)) { ConOutResPrintf(STRING_COPY_ERROR2); nErrorLevel = 1; break; } /* only print source name when more then one file */ if (bMultipleSource) ConOutPrintf(_T("%s\n"), tmpSrcPath); /* Handle any overriding / prompting that needs to be done */ if (((!(dwFlags & COPY_NO_PROMPT) && IsExistingFile (tmpDestPath)) || dwFlags & COPY_PROMPT) && !bTouch) nOverwrite = CopyOverwrite(tmpDestPath); if (nOverwrite == PROMPT_NO || nOverwrite == PROMPT_BREAK) continue; if (nOverwrite == PROMPT_ALL || (nOverwrite == PROMPT_YES && bAppend)) dwFlags |= COPY_NO_PROMPT; /* Tell weather the copy was successful or not */ if (copy(tmpSrcPath,tmpDestPath, bAppend, dwFlags, bTouch)) { nFiles++; //LoadString(CMD_ModuleHandle, STRING_MOVE_ERROR1, szMsg, RC_STRING_MAX_SIZE); } else { /* print out the error message */ ConOutResPrintf(STRING_COPY_ERROR3); ConOutFormatMessage (GetLastError(), szSrcPath); nErrorLevel = 1; } /* Loop through all wildcard files */ } while (FindNextFile(hFile, &findBuffer)); /* Loop through all files in src string with a + */ } while(!bDone); /* print out the number of files copied */ ConOutResPrintf(STRING_COPY_FILE, bAppend ? 1 : nFiles); if (hFile) FindClose(hFile); if (arg != NULL) freep(arg); return 0; }
/* * print the command-line prompt */ VOID PrintPrompt(VOID) { static TCHAR default_pr[] = _T("$P$G"); TCHAR szPrompt[256]; LPTSTR pr; if (GetEnvironmentVariable (_T("PROMPT"), szPrompt, 256)) pr = szPrompt; else pr = default_pr; while (*pr) { if (*pr != _T('$')) { ConOutChar (*pr); } else { pr++; switch (_totupper (*pr)) { case _T('A'): ConOutChar (_T('&')); break; case _T('B'): ConOutChar (_T('|')); break; case _T('C'): ConOutChar (_T('(')); break; case _T('D'): ConOutPrintf(_T("%s"), GetDateString()); break; case _T('E'): ConOutChar (_T('\x1B')); break; case _T('F'): ConOutChar (_T(')')); break; case _T('G'): ConOutChar (_T('>')); break; case _T('H'): ConOutChar (_T('\x08')); ConOutChar (_T(' ')); ConOutChar (_T('\x08')); break; case _T('L'): ConOutChar (_T('<')); break; case _T('N'): { TCHAR szPath[MAX_PATH]; GetCurrentDirectory (MAX_PATH, szPath); ConOutChar (szPath[0]); } break; case _T('P'): { TCHAR szPath[MAX_PATH]; GetCurrentDirectory (MAX_PATH, szPath); ConOutPrintf (_T("%s"), szPath); } break; case _T('Q'): ConOutChar (_T('=')); break; case _T('S'): ConOutChar (_T(' ')); break; case _T('T'): ConOutPrintf(_T("%s"), GetTimeString()); break; case _T('V'): switch (osvi.dwPlatformId) { case VER_PLATFORM_WIN32_WINDOWS: if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 1) ConOutPrintf (_T("Windows 98")); else ConOutPrintf (_T("Windows 95")); break; case VER_PLATFORM_WIN32_NT: ConOutPrintf (_T("Windows NT Version %lu.%lu"), osvi.dwMajorVersion, osvi.dwMinorVersion); break; } break; case _T('_'): ConOutChar (_T('\n')); break; case '$': ConOutChar (_T('$')); break; #ifdef FEATURE_DIRECTORY_STACK case '+': { INT i; for (i = 0; i < GetDirectoryStackDepth (); i++) ConOutChar (_T('+')); } break; #endif } } pr++; } }
INT cmd_move (LPTSTR param) { LPTSTR *arg; INT argc, i, nFiles; LPTSTR pszDest; TCHAR szDestPath[MAX_PATH]; TCHAR szFullDestPath[MAX_PATH]; TCHAR szSrcDirPath[MAX_PATH]; TCHAR szSrcPath[MAX_PATH]; TCHAR szFullSrcPath[MAX_PATH]; DWORD dwFlags = 0; INT nOverwrite = 0; WIN32_FIND_DATA findBuffer; HANDLE hFile; /* used only when source and destination directories are on different volume*/ HANDLE hDestFile; WIN32_FIND_DATA findDestBuffer; TCHAR szMoveDest[MAX_PATH]; TCHAR szMoveSrc[MAX_PATH]; LPTSTR pszDestDirPointer; LPTSTR pszSrcDirPointer; INT nDirLevel = 0; LPTSTR pszFile; BOOL OnlyOneFile; BOOL FoundFile; BOOL MoveStatus; DWORD dwMoveFlags = 0; DWORD dwMoveStatusFlags = 0; if (!_tcsncmp (param, _T("/?"), 2)) { #if 0 ConOutPuts (_T("Moves files and renames files and directories.\n\n" "To move one or more files:\n" "MOVE [/N][/Y|/-Y][drive:][path]filename1[,...] destination\n" "\n" "To rename a directory:\n" "MOVE [/N][/Y|/-Y][drive:][path]dirname1 dirname2\n" "\n" " [drive:][path]filename1 Specifies the location and name of the file\n" " or files you want to move.\n" " /N Nothing. Don everthing but move files or direcories.\n" " /Y\n" " /-Y\n" "...")); #else ConOutResPaging(TRUE,STRING_MOVE_HELP2); #endif return 0; } nErrorLevel = 0; arg = splitspace(param, &argc); /* read options */ for (i = 0; i < argc; i++) { if (!_tcsicmp(arg[i], _T("/N"))) dwFlags |= MOVE_NOTHING; else if (!_tcsicmp(arg[i], _T("/Y"))) dwFlags |= MOVE_OVER_YES; else if (!_tcsicmp(arg[i], _T("/-Y"))) dwFlags |= MOVE_OVER_NO; else break; } nFiles = argc - i; if (nFiles < 1) { /* there must be at least one pathspec */ error_req_param_missing(); freep(arg); return 1; } if (nFiles > 2) { /* there are more than two pathspecs */ error_too_many_parameters(param); freep(arg); return 1; } /* If no destination is given, default to current directory */ pszDest = (nFiles == 1) ? _T(".") : arg[i + 1]; /* check for wildcards in source and destination */ if (_tcschr(pszDest, _T('*')) != NULL || _tcschr(pszDest, _T('?')) != NULL) { /* '*'/'?' in dest, this doesnt happen. give folder name instead*/ error_invalid_parameter_format(pszDest); freep(arg); return 1; } if (_tcschr(arg[i], _T('*')) != NULL || _tcschr(arg[i], _T('?')) != NULL) { dwMoveStatusFlags |= MOVE_SOURCE_HAS_WILD; } /* get destination */ GetFullPathName (pszDest, MAX_PATH, szDestPath, NULL); TRACE ("Destination: %s\n", debugstr_aw(szDestPath)); /* get source folder */ GetFullPathName(arg[i], MAX_PATH, szSrcDirPath, &pszFile); if (pszFile != NULL) *pszFile = _T('\0'); TRACE ("Source Folder: %s\n", debugstr_aw(szSrcDirPath)); hFile = FindFirstFile (arg[i], &findBuffer); if (hFile == INVALID_HANDLE_VALUE) { ErrorMessage (GetLastError (), arg[i]); freep (arg); return 1; } /* check for special cases "." and ".." and if found skip them */ FoundFile = TRUE; while(FoundFile && (_tcscmp(findBuffer.cFileName,_T(".")) == 0 || _tcscmp(findBuffer.cFileName,_T("..")) == 0)) FoundFile = FindNextFile (hFile, &findBuffer); if (!FoundFile) { /* what? we don't have anything to move? */ error_file_not_found(); FindClose(hFile); freep(arg); return 1; } OnlyOneFile = TRUE; /* check if there can be found files as files have first priority */ if (findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) dwMoveStatusFlags |= MOVE_SOURCE_IS_DIR; else dwMoveStatusFlags |= MOVE_SOURCE_IS_FILE; while(OnlyOneFile && FindNextFile(hFile,&findBuffer)) { if (!(findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { ConOutPrintf(_T("")); if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE) OnlyOneFile = FALSE; else { /* this has been done this way so that we don't disturb other settings if they have been set before this */ dwMoveStatusFlags |= MOVE_SOURCE_IS_FILE; dwMoveStatusFlags &= ~MOVE_SOURCE_IS_DIR; } } } FindClose(hFile); TRACE ("Do we have only one file: %s\n", OnlyOneFile ? "TRUE" : "FALSE"); /* we have to start again to be sure we don't miss any files or folders*/ hFile = FindFirstFile (arg[i], &findBuffer); if (hFile == INVALID_HANDLE_VALUE) { ErrorMessage (GetLastError (), arg[i]); freep (arg); return 1; } /* check for special cases "." and ".." and if found skip them */ FoundFile = TRUE; while(FoundFile && (_tcscmp(findBuffer.cFileName,_T(".")) == 0 || _tcscmp(findBuffer.cFileName,_T("..")) == 0)) FoundFile = FindNextFile (hFile, &findBuffer); if (!FoundFile) { /* huh? somebody removed files and/or folders which were there */ error_file_not_found(); FindClose(hFile); freep(arg); return 1; } /* check if source and destination paths are on different volumes */ if (szSrcDirPath[0] != szDestPath[0]) dwMoveStatusFlags |= MOVE_PATHS_ON_DIF_VOL; /* move it */ do { TRACE ("Found file/directory: %s\n", debugstr_aw(findBuffer.cFileName)); nOverwrite = 1; dwMoveFlags = 0; dwMoveStatusFlags &= ~MOVE_DEST_IS_FILE & ~MOVE_DEST_IS_DIR & ~MOVE_SRC_CURRENT_IS_DIR & ~MOVE_DEST_EXISTS; _tcscpy(szFullSrcPath,szSrcDirPath); if(szFullSrcPath[_tcslen(szFullSrcPath) - 1] != _T('\\')) _tcscat (szFullSrcPath, _T("\\")); _tcscat(szFullSrcPath,findBuffer.cFileName); _tcscpy(szSrcPath, szFullSrcPath); if (IsExistingDirectory(szSrcPath)) { /* source is directory */ if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE) { dwMoveStatusFlags |= MOVE_SRC_CURRENT_IS_DIR; /* source is file but at the current round we found a directory */ continue; } TRACE ("Source is dir: %s\n", debugstr_aw(szSrcPath)); dwMoveFlags = MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED; } /* if source is file we don't need to do anything special */ if (IsExistingDirectory(szDestPath)) { /* destination is existing directory */ TRACE ("Destination is directory: %s\n", debugstr_aw(szDestPath)); dwMoveStatusFlags |= MOVE_DEST_IS_DIR; /*build the dest string(accounts for *)*/ _tcscpy (szFullDestPath, szDestPath); /*check to see if there is an ending slash, if not add one*/ if(szFullDestPath[_tcslen(szFullDestPath) - 1] != _T('\\')) _tcscat (szFullDestPath, _T("\\")); _tcscat (szFullDestPath, findBuffer.cFileName); if (IsExistingFile(szFullDestPath) || IsExistingDirectory(szFullDestPath)) dwMoveStatusFlags |= MOVE_DEST_EXISTS; dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED; } if (IsExistingFile(szDestPath)) { /* destination is a file */ TRACE ("Destination is file: %s\n", debugstr_aw(szDestPath)); dwMoveStatusFlags |= MOVE_DEST_IS_FILE | MOVE_DEST_EXISTS; _tcscpy (szFullDestPath, szDestPath); dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED; } TRACE ("Move Status Flags: 0x%X\n",dwMoveStatusFlags); if (dwMoveStatusFlags & MOVE_SOURCE_IS_DIR && dwMoveStatusFlags & MOVE_DEST_IS_DIR && dwMoveStatusFlags & MOVE_SOURCE_HAS_WILD) { /* We are not allowed to have existing source and destination dir when there is wildcard in source */ error_syntax(NULL); FindClose(hFile); freep(arg); return 1; } if (!(dwMoveStatusFlags & (MOVE_DEST_IS_FILE | MOVE_DEST_IS_DIR))) { /* destination doesn't exist */ _tcscpy (szFullDestPath, szDestPath); if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE) dwMoveStatusFlags |= MOVE_DEST_IS_FILE; if (dwMoveStatusFlags & MOVE_SOURCE_IS_DIR) dwMoveStatusFlags |= MOVE_DEST_IS_DIR; dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED; } if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE && dwMoveStatusFlags & MOVE_DEST_IS_FILE && !OnlyOneFile) { /*source has many files but there is only one destination file*/ error_invalid_parameter_format(pszDest); FindClose(hFile); freep (arg); return 1; } /*checks to make sure user wanted/wants the override*/ if((dwFlags & MOVE_OVER_NO) && (dwMoveStatusFlags & MOVE_DEST_EXISTS)) continue; if(!(dwFlags & MOVE_OVER_YES) && (dwMoveStatusFlags & MOVE_DEST_EXISTS)) nOverwrite = MoveOverwrite (szFullDestPath); if (nOverwrite == PROMPT_NO || nOverwrite == PROMPT_BREAK) continue; if (nOverwrite == PROMPT_ALL) dwFlags |= MOVE_OVER_YES; ConOutPrintf (_T("%s => %s "), szSrcPath, szFullDestPath); /* are we really supposed to do something */ if (dwFlags & MOVE_NOTHING) continue; /*move the file*/ if (!(dwMoveStatusFlags & MOVE_SOURCE_IS_DIR && dwMoveStatusFlags & MOVE_PATHS_ON_DIF_VOL)) /* we aren't moving source folder to different drive */ MoveStatus = MoveFileEx (szSrcPath, szFullDestPath, dwMoveFlags); else { /* we are moving source folder to different drive */ _tcscpy(szMoveDest, szFullDestPath); _tcscpy(szMoveSrc, szSrcPath); DeleteFile(szMoveDest); MoveStatus = CreateDirectory(szMoveDest, NULL); /* we use default security settings */ if (MoveStatus) { _tcscat(szMoveDest,_T("\\")); _tcscat(szMoveSrc,_T("\\")); nDirLevel = 0; pszDestDirPointer = szMoveDest + _tcslen(szMoveDest); pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc); _tcscpy(pszSrcDirPointer,_T("*.*")); hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer); if (hDestFile == INVALID_HANDLE_VALUE) MoveStatus = FALSE; else { BOOL FirstTime = TRUE; FoundFile = TRUE; MoveStatus = FALSE; while(FoundFile) { if (FirstTime) FirstTime = FALSE; else FoundFile = FindNextFile (hDestFile, &findDestBuffer); if (!FoundFile) { /* Nothing to do in this folder so we stop working on it */ FindClose(hDestFile); (pszSrcDirPointer)--; (pszDestDirPointer)--; _tcscpy(pszSrcDirPointer,_T("")); _tcscpy(pszDestDirPointer,_T("")); if (nDirLevel > 0) { TCHAR szTempPath[MAX_PATH]; INT nDiff; FoundFile = TRUE; /* we need to continue our seek for files */ nDirLevel--; RemoveDirectory(szMoveSrc); GetDirectory(szMoveSrc,szTempPath,0); nDiff = _tcslen(szMoveSrc) - _tcslen(szTempPath); pszSrcDirPointer = pszSrcDirPointer - nDiff; _tcscpy(pszSrcDirPointer,_T("")); GetDirectory(szMoveDest,szTempPath,0); nDiff = _tcslen(szMoveDest) - _tcslen(szTempPath); pszDestDirPointer = pszDestDirPointer - nDiff; _tcscpy(pszDestDirPointer,_T("")); if(szMoveSrc[_tcslen(szMoveSrc) - 1] != _T('\\')) _tcscat (szMoveSrc, _T("\\")); if(szMoveDest[_tcslen(szMoveDest) - 1] != _T('\\')) _tcscat (szMoveDest, _T("\\")); pszDestDirPointer = szMoveDest + _tcslen(szMoveDest); pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc); _tcscpy(pszSrcDirPointer,_T("*.*")); hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer); if (hDestFile == INVALID_HANDLE_VALUE) continue; FirstTime = TRUE; } else { MoveStatus = TRUE; /* we moved everything so lets tell user about it */ RemoveDirectory(szMoveSrc); } continue; } /* if we find "." or ".." we'll skip them */ if (_tcscmp(findDestBuffer.cFileName,_T(".")) == 0 || _tcscmp(findDestBuffer.cFileName,_T("..")) == 0) continue; _tcscpy(pszSrcDirPointer, findDestBuffer.cFileName); _tcscpy(pszDestDirPointer, findDestBuffer.cFileName); if (IsExistingFile(szMoveSrc)) { FoundFile = CopyFile(szMoveSrc, szMoveDest, FALSE); if (!FoundFile) continue; DeleteFile(szMoveSrc); } else { FindClose(hDestFile); CreateDirectory(szMoveDest, NULL); _tcscat(szMoveDest,_T("\\")); _tcscat(szMoveSrc,_T("\\")); nDirLevel++; pszDestDirPointer = szMoveDest + _tcslen(szMoveDest); pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc); _tcscpy(pszSrcDirPointer,_T("*.*")); hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer); if (hDestFile == INVALID_HANDLE_VALUE) { FoundFile = FALSE; continue; } FirstTime = TRUE; } } } } } if (MoveStatus) ConOutResPrintf(STRING_MOVE_ERROR1); else ConOutResPrintf(STRING_MOVE_ERROR2); } while ((!OnlyOneFile || dwMoveStatusFlags & MOVE_SRC_CURRENT_IS_DIR ) && !(dwMoveStatusFlags & MOVE_SOURCE_IS_DIR) && FindNextFile (hFile, &findBuffer)); FindClose (hFile); freep (arg); return 0; }
static BOOL ReadCommand( PCONSOLE_STATE State, LPSTR str, INT maxlen) { SHORT orgx; /* origin x/y */ SHORT orgy; SHORT curx; /*current x/y cursor position*/ SHORT cury; SHORT tempscreen; INT count; /*used in some for loops*/ INT current = 0; /*the position of the cursor in the string (str)*/ INT charcount = 0;/*chars in the string (str)*/ INPUT_RECORD ir; CHAR ch; BOOL bReturn = FALSE; BOOL bCharInput; #ifdef FEATURE_HISTORY //BOOL bContinue=FALSE;/*is TRUE the second case will not be executed*/ CHAR PreviousChar; #endif CONSOLE_GetCursorXY(&orgx, &orgy); curx = orgx; cury = orgy; memset(str, 0, maxlen * sizeof(CHAR)); CONSOLE_SetCursorType(State->bInsert, TRUE); do { bReturn = FALSE; CONSOLE_ConInKey(&ir); if (ir.Event.KeyEvent.dwControlKeyState & (RIGHT_ALT_PRESSED |LEFT_ALT_PRESSED| RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED) ) { switch (ir.Event.KeyEvent.wVirtualKeyCode) { #ifdef FEATURE_HISTORY case 'K': /*add the current command line to the history*/ if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED)) { if (str[0]) History(0,str); ClearCommandLine (str, maxlen, orgx, orgy); current = charcount = 0; curx = orgx; cury = orgy; //bContinue=TRUE; break; } case 'D': /*delete current history entry*/ if (ir.Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED)) { ClearCommandLine (str, maxlen, orgx, orgy); History_del_current_entry(str); current = charcount = strlen (str); ConOutPrintf("%s", str); GetCursorXY(&curx, &cury); //bContinue=TRUE; break; } #endif /*FEATURE_HISTORY*/ } } bCharInput = FALSE; switch (ir.Event.KeyEvent.wVirtualKeyCode) { case VK_BACK: /* <BACKSPACE> - delete character to left of cursor */ if (current > 0 && charcount > 0) { if (current == charcount) { /* if at end of line */ str[current - 1] = L'\0'; if (CONSOLE_GetCursorX () != 0) { CONSOLE_ConOutPrintf("\b \b"); curx--; } else { CONSOLE_SetCursorXY((SHORT)(State->maxx - 1), (SHORT)(CONSOLE_GetCursorY () - 1)); CONSOLE_ConOutChar(' '); CONSOLE_SetCursorXY((SHORT)(State->maxx - 1), (SHORT)(CONSOLE_GetCursorY () - 1)); cury--; curx = State->maxx - 1; } } else { for (count = current - 1; count < charcount; count++) str[count] = str[count + 1]; if (CONSOLE_GetCursorX () != 0) { CONSOLE_SetCursorXY ((SHORT)(CONSOLE_GetCursorX () - 1), CONSOLE_GetCursorY ()); curx--; } else { CONSOLE_SetCursorXY ((SHORT)(State->maxx - 1), (SHORT)(CONSOLE_GetCursorY () - 1)); cury--; curx = State->maxx - 1; } CONSOLE_GetCursorXY(&curx, &cury); CONSOLE_ConOutPrintf("%s ", &str[current - 1]); CONSOLE_SetCursorXY(curx, cury); } charcount--; current--; } break; case VK_INSERT: /* toggle insert/overstrike mode */ State->bInsert ^= TRUE; CONSOLE_SetCursorType(State->bInsert, TRUE); break; case VK_DELETE: /* delete character under cursor */ if (current != charcount && charcount > 0) { for (count = current; count < charcount; count++) str[count] = str[count + 1]; charcount--; CONSOLE_GetCursorXY(&curx, &cury); CONSOLE_ConOutPrintf("%s ", &str[current]); CONSOLE_SetCursorXY(curx, cury); } break; case VK_HOME: /* goto beginning of string */ if (current != 0) { CONSOLE_SetCursorXY(orgx, orgy); curx = orgx; cury = orgy; current = 0; } break; case VK_END: /* goto end of string */ if (current != charcount) { CONSOLE_SetCursorXY(orgx, orgy); CONSOLE_ConOutPrintf("%s", str); CONSOLE_GetCursorXY(&curx, &cury); current = charcount; } break; case 'M': case 'C': /* ^M does the same as return */ bCharInput = TRUE; if (!(ir.Event.KeyEvent.dwControlKeyState & (RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED))) { break; } case VK_RETURN: /* end input, return to main */ #ifdef FEATURE_HISTORY /* add to the history */ if (str[0]) History (0, str); #endif str[charcount] = '\0'; CONSOLE_ConOutChar('\n'); bReturn = TRUE; break; case VK_ESCAPE: /* clear str Make this callable! */ ClearCommandLine (str, maxlen, orgx, orgy); curx = orgx; cury = orgy; current = charcount = 0; break; #ifdef FEATURE_HISTORY case VK_F3: History_move_to_bottom(); #endif case VK_UP: #ifdef FEATURE_HISTORY /* get previous command from buffer */ ClearCommandLine (str, maxlen, orgx, orgy); History (-1, str); current = charcount = strlen (str); if (((charcount + orgx) / maxx) + orgy > maxy - 1) orgy += maxy - ((charcount + orgx) / maxx + orgy + 1); CONSOLE_ConOutPrintf("%s", str); CONSOLE_GetCursorXY(&curx, &cury); #endif break; case VK_DOWN: #ifdef FEATURE_HISTORY /* get next command from buffer */ ClearCommandLine (str, maxlen, orgx, orgy); History (1, str); current = charcount = strlen (str); if (((charcount + orgx) / maxx) + orgy > maxy - 1) orgy += maxy - ((charcount + orgx) / maxx + orgy + 1); CONSOLE_ConOutPrintf("%s", str); CONSOLE_GetCursorXY(&curx, &cury); #endif break; case VK_LEFT: /* move cursor left */ if (current > 0) { current--; if (CONSOLE_GetCursorX() == 0) { CONSOLE_SetCursorXY((SHORT)(State->maxx - 1), (SHORT)(CONSOLE_GetCursorY () - 1)); curx = State->maxx - 1; cury--; } else { CONSOLE_SetCursorXY((SHORT)(CONSOLE_GetCursorX () - 1), CONSOLE_GetCursorY ()); curx--; } } break; case VK_RIGHT: /* move cursor right */ if (current != charcount) { current++; if (CONSOLE_GetCursorX() == State->maxx - 1) { CONSOLE_SetCursorXY(0, (SHORT)(CONSOLE_GetCursorY () + 1)); curx = 0; cury++; } else { CONSOLE_SetCursorXY((SHORT)(CONSOLE_GetCursorX () + 1), CONSOLE_GetCursorY ()); curx++; } } #ifdef FEATURE_HISTORY else { LPCSTR last = PeekHistory(-1); if (last && charcount < (INT)strlen (last)) { PreviousChar = last[current]; CONSOLE_ConOutChar(PreviousChar); CONSOLE_GetCursorXY(&curx, &cury); str[current++] = PreviousChar; charcount++; } } #endif break; default: /* This input is just a normal char */ bCharInput = TRUE; } ch = ir.Event.KeyEvent.uChar.UnicodeChar; if (ch >= 32 && (charcount != (maxlen - 2)) && bCharInput) { /* insert character into string... */ if (State->bInsert && current != charcount) { /* If this character insertion will cause screen scrolling, * adjust the saved origin of the command prompt. */ tempscreen = strlen(str + current) + curx; if ((tempscreen % State->maxx) == (State->maxx - 1) && (tempscreen / State->maxx) + cury == (State->maxy - 1)) { orgy--; cury--; } for (count = charcount; count > current; count--) str[count] = str[count - 1]; str[current++] = ch; if (curx == State->maxx - 1) curx = 0, cury++; else curx++; CONSOLE_ConOutPrintf("%s", &str[current - 1]); CONSOLE_SetCursorXY(curx, cury); charcount++; } else { if (current == charcount) charcount++; str[current++] = ch; if (CONSOLE_GetCursorX () == State->maxx - 1 && CONSOLE_GetCursorY () == State->maxy - 1) orgy--, cury--; if (CONSOLE_GetCursorX () == State->maxx - 1) curx = 0, cury++; else curx++; CONSOLE_ConOutChar(ch); } } } while (!bReturn); CONSOLE_SetCursorType(State->bInsert, TRUE); return TRUE; }
static INT PrintAssociation(LPTSTR extension) { DWORD return_val; HKEY hKey = NULL, hInsideKey = NULL; DWORD fileTypeLength = 0; LPTSTR fileType = NULL; return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_READ, &hKey); if (return_val != ERROR_SUCCESS) { RegCloseKey(hKey); return -1; } return_val = RegOpenKeyEx(hKey, extension, 0, KEY_READ, &hInsideKey); if (return_val != ERROR_SUCCESS) { RegCloseKey(hKey); RegCloseKey(hInsideKey); return 0; } /* obtain string length */ return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, NULL, &fileTypeLength); if (return_val == ERROR_FILE_NOT_FOUND) /* no default value, don't display */ { RegCloseKey(hInsideKey); RegCloseKey(hKey); return 0; } if (return_val != ERROR_SUCCESS) { RegCloseKey(hInsideKey); RegCloseKey(hKey); return -2; } fileType = cmd_alloc(fileTypeLength * sizeof(TCHAR)); /* obtain actual file type */ return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, (LPBYTE) fileType, &fileTypeLength); RegCloseKey(hInsideKey); RegCloseKey(hKey); if (return_val != ERROR_SUCCESS) { cmd_free(fileType); return -2; } if (fileTypeLength != 0) /* if there is a default key, display relevant information */ { ConOutPrintf(_T("%s=%s\n"), extension, fileType); } if (fileTypeLength) cmd_free(fileType); return 1; }