6831e576906bfc2783ccfe4e20a7e5d93fa59ccc
[reactos.git] / reactos / apps / utils / cmd / chcp.c
1 /*
2 * CHCP.C - chcp internal command.
3 *
4 *
5 * History:
6 *
7 * 23-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
8 * Started.
9 *
10 */
11
12 #define WIN32_LEAN_AND_MEAN
13
14 #include "config.h"
15
16 #ifdef INCLUDE_CMD_CHCP
17
18 #include <windows.h>
19 #include <tchar.h>
20 #include <stdlib.h>
21
22 #include "cmd.h"
23 #include "chcp.h"
24
25
26 INT CommandChcp (LPTSTR cmd, LPTSTR param)
27 {
28 LPTSTR *arg;
29 INT args;
30
31 /* print help */
32 if (!_tcsncmp (param, _T("/?"), 2))
33 {
34 ConOutPuts (_T("Displays or sets the active code page number.\n\n"
35 "CHCP [nnn]\n\n"
36 " nnn Specifies the active code page number.\n\n"
37 "Type CHCP without a parameter to display the active code page number."));
38 return 0;
39 }
40
41 /* get parameters */
42 arg = split (param, &args);
43
44 if (args == 0)
45 {
46 /* display active code page number */
47 ConOutPrintf ("Active code page: %u\n", GetConsoleCP ());
48 }
49 else if (args >= 2)
50 {
51 /* too many parameters */
52 ConErrPrintf ("Invalid parameter format - %s\n", param);
53 }
54 else
55 {
56 /* set active code page number */
57
58 UINT uOldCodePage;
59 UINT uNewCodePage;
60
61 /* save old code page */
62 uOldCodePage = GetConsoleCP ();
63
64 uNewCodePage = (UINT)_ttoi (arg[0]);
65
66 if (uNewCodePage == 0)
67 {
68 ConErrPrintf ("Parameter format incorrect - %s\n", arg[0]);
69
70 }
71 else
72 {
73 if (!SetConsoleCP (uNewCodePage))
74 {
75 ConErrPrintf ("Invalid code page\n");
76 }
77 else
78 {
79 SetConsoleOutputCP (uNewCodePage);
80 InitLocale ();
81 }
82 }
83 }
84
85 freep (arg);
86
87 return 0;
88 }
89
90 #endif /* INCLUDE_CMD_CHCP */