ReactOS  0.4.12-dev-43-g63b00d8
chcp.c
Go to the documentation of this file.
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 */
37 
38  /* Print help */
39  if (argc > 1 && wcscmp(argv[1], L"/?") == 0)
40  {
42  return 0;
43  }
44 
45  if (argc == 1)
46  {
47  /* Display the active code page number */
49  return 0;
50  }
51 
52  if (argc > 2)
53  {
54  /* Too many parameters */
56  return 1;
57  }
58 
59  uNewCodePage = (UINT)_wtoi(argv[1]);
60 
61  if (uNewCodePage == 0)
62  {
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 */
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 */
94  return 1;
95 }
static int argc
Definition: ServiceArgs.c:12
#define STRING_CHCP_ERROR4
Definition: resource.h:8
BOOL WINAPI SetConsoleOutputCP(IN UINT wCodepage)
Definition: console.c:644
__wchar_t WCHAR
Definition: xmlstorage.h:180
_Check_return_ _CRTIMP int __cdecl _wtoi(_In_z_ const wchar_t *_Str)
#define STRING_CHCP_HELP
Definition: resource.h:10
static char ** argv
Definition: ServiceArgs.c:11
int wmain(int argc, WCHAR *argv[])
Definition: chcp.c:31
#define ConInitStdStreams()
Definition: stream.h:122
UINT WINAPI DECLSPEC_HOTPATCH GetConsoleOutputCP(VOID)
Definition: console.c:2453
UINT WINAPI DECLSPEC_HOTPATCH GetConsoleCP(VOID)
Definition: console.c:2393
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
unsigned int UINT
Definition: ndis.h:50
#define StdErr
Definition: stream.h:77
#define STRING_ERROR_INVALID_PARAM_FORMAT
Definition: resource.h:5
INT ConResPuts(IN PCON_STREAM Stream, IN UINT uID)
Definition: outstream.c:610
static const WCHAR L[]
Definition: oid.c:1087
#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)
#define StdOut
Definition: stream.h:76
#define UINT
Definition: msvc.h:27