ReactOS  0.4.15-dev-3457-gf6edd65
chcp.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Change CodePage Command
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Displays or changes the active console input and output code pages.
5  * COPYRIGHT: Copyright 1999 Eric Kohl
6  * Copyright 2017-2021 Hermes Belusca-Maito
7  */
8 /*
9  * CHCP.C - chcp internal command.
10  *
11  * 23-Dec-1998 (Eric Kohl)
12  * Started.
13  *
14  * 02-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
15  * Remove all hardcoded strings in En.rc
16  */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include <windef.h>
22 #include <winbase.h>
23 #include <wincon.h>
24 
25 #include <conutils.h>
26 
27 #include "resource.h"
28 
29 // INT CommandChcp(LPTSTR cmd, LPTSTR param)
30 int wmain(int argc, WCHAR* argv[])
31 {
32  UINT uOldCodePage, uNewCodePage;
33 
34  /* Initialize the Console Standard Streams */
36 
37  /* Print help */
38  if (argc > 1 && wcscmp(argv[1], L"/?") == 0)
39  {
41  return 0;
42  }
43 
44  if (argc == 1)
45  {
46  /* Display the active code page number */
48  return 0;
49  }
50 
51  if (argc > 2)
52  {
53  /* Too many parameters */
55  return 1;
56  }
57 
58  uNewCodePage = (UINT)_wtoi(argv[1]);
59 
60  if (uNewCodePage == 0)
61  {
63  return 1;
64  }
65 
70  /*
71  * Save the original console code page to be restored
72  * in case SetConsoleCP() or SetConsoleOutputCP() fails.
73  */
74  uOldCodePage = GetConsoleCP();
75 
76  /*
77  * Try changing the console input and output code pages.
78  * If it succeeds, refresh the local code page information.
79  */
80  if (SetConsoleCP(uNewCodePage))
81  {
82  if (SetConsoleOutputCP(uNewCodePage))
83  {
84  /* Success, reset the current thread UI language
85  * and update the streams cached code page. */
87  ConStdStreamsSetCacheCodePage(uNewCodePage, uNewCodePage);
88 
89  /* Display the active code page number */
91  return 0;
92  }
93  else
94  {
95  /* Failure, restore the original console code page */
96  SetConsoleCP(uOldCodePage);
97  }
98  }
99 
100  /* An error happened, display an error and bail out */
102  return 1;
103 }
static int argc
Definition: ServiceArgs.c:12
#define STRING_CHCP_ERROR4
Definition: resource.h:8
BOOL WINAPI SetConsoleOutputCP(IN UINT wCodepage)
Definition: console.c:692
_Check_return_ _CRTIMP int __cdecl _wtoi(_In_z_ const wchar_t *_Str)
#define STRING_CHCP_HELP
Definition: resource.h:10
#define argv
Definition: mplay32.c:18
int wmain(int argc, WCHAR *argv[])
Definition: chcp.c:30
#define L(x)
Definition: ntvdm.h:50
UINT WINAPI DECLSPEC_HOTPATCH GetConsoleOutputCP(VOID)
Definition: console.c:2453
#define STRING_ERROR_INVALID_PARAM_FORMAT
Definition: resource.h:5
UINT WINAPI DECLSPEC_HOTPATCH GetConsoleCP(VOID)
Definition: console.c:2393
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
void ConResPrintf(FILE *fp, UINT nID,...)
Definition: fc.c:33
__wchar_t WCHAR
Definition: xmlstorage.h:180
LANGID ConSetThreadUILanguage(IN LANGID LangId OPTIONAL)
Definition: utils.c:352
#define STRING_CHCP_ERROR1
Definition: resource.h:7
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCP(UINT wCodePageID)
Definition: console.c:2422
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
unsigned int UINT
Definition: ndis.h:50
#define ConStdStreamsSetCacheCodePage(InputCodePage, OutputCodePage)
Definition: stream.h:152
#define StdOut
Definition: fc.c:14
#define ConInitStdStreams()
Definition: fc.c:13
#define StdErr
Definition: fc.c:15