1 /* 2 * COPYRIGHT: See COPYING in the top level directory 3 * PROJECT: ReactOS Change CodePage Command 4 * FILE: base/applications/cmdutils/chcp/chcp.c 5 * PURPOSE: Displays or changes the active console input and output codepages. 6 * PROGRAMMERS: Eric Kohl 7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr) 8 */ 9 /* 10 * CHCP.C - chcp internal command. 11 * 12 * 23-Dec-1998 (Eric Kohl) 13 * Started. 14 * 15 * 02-Apr-2005 (Magnus Olsen <magnus@greatlord.com>) 16 * Remove all hardcoded strings in En.rc 17 */ 18 19 #include <stdio.h> 20 #include <stdlib.h> 21 22 #include <windef.h> 23 #include <winbase.h> 24 #include <wincon.h> 25 26 #include <conutils.h> 27 28 #include "resource.h" 29 30 // INT CommandChcp(LPTSTR cmd, LPTSTR param) 31 int wmain(int argc, WCHAR* argv[]) 32 { 33 UINT uOldCodePage, uNewCodePage; 34 35 /* Initialize the Console Standard Streams */ 36 ConInitStdStreams(); 37 38 /* Print help */ 39 if (argc > 1 && wcscmp(argv[1], L"/?") == 0) 40 { 41 ConResPuts(StdOut, STRING_CHCP_HELP); 42 return 0; 43 } 44 45 if (argc == 1) 46 { 47 /* Display the active code page number */ 48 ConResPrintf(StdOut, STRING_CHCP_ERROR1, GetConsoleOutputCP()); 49 return 0; 50 } 51 52 if (argc > 2) 53 { 54 /* Too many parameters */ 55 ConResPrintf(StdErr, STRING_ERROR_INVALID_PARAM_FORMAT, argv[2]); 56 return 1; 57 } 58 59 uNewCodePage = (UINT)_wtoi(argv[1]); 60 61 if (uNewCodePage == 0) 62 { 63 ConResPrintf(StdErr, STRING_ERROR_INVALID_PARAM_FORMAT, argv[1]); 64 return 1; 65 } 66 67 /* 68 * Save the original console codepage to be restored in case 69 * SetConsoleCP() or SetConsoleOutputCP() fails. 70 */ 71 uOldCodePage = GetConsoleCP(); 72 73 /* 74 * Try changing the console input codepage. If it works then also change 75 * the console output codepage, and refresh our local codepage cache. 76 */ 77 if (SetConsoleCP(uNewCodePage)) 78 { 79 if (SetConsoleOutputCP(uNewCodePage)) 80 { 81 /* Display the active code page number */ 82 ConResPrintf(StdOut, STRING_CHCP_ERROR1, GetConsoleOutputCP()); 83 return 0; 84 } 85 else 86 { 87 /* Failure, restore the original console codepage */ 88 SetConsoleCP(uOldCodePage); 89 } 90 } 91 92 /* An error happened, display an error and bail out */ 93 ConResPuts(StdErr, STRING_CHCP_ERROR4); 94 return 1; 95 } 96