d5fa3976a560af6e96d5be23004ab7855bdfadea
[reactos.git] / lib / kernel32 / process / cmdline.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS system libraries
5  * FILE:            lib/kernel32/proc/proc.c
6  * PURPOSE:         Process functions
7  * PROGRAMMER:      Ariadne ( ariadne@xs4all.nl)
8  * UPDATE HISTORY:
9  *                  Created 01/11/98
10  */
11
12 /* INCLUDES ****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <windows.h>
16 #include <kernel32/proc.h>
17 #include <kernel32/thread.h>
18 #include <wchar.h>
19 #include <string.h>
20 #include <napi/teb.h>
21 #include <ntdll/rtl.h>
22
23 #define NDEBUG
24 #include <kernel32/kernel32.h>
25
26
27 /* GLOBALS ******************************************************************/
28
29 static UNICODE_STRING CommandLineStringW;
30 static ANSI_STRING CommandLineStringA;
31
32 static WINBOOL bCommandLineInitialized = FALSE;
33
34
35 /* FUNCTIONS ****************************************************************/
36
37 static VOID
38 InitCommandLines (VOID)
39 {
40         PRTL_USER_PROCESS_PARAMETERS Params;
41
42         // get command line
43         Params = NtCurrentPeb()->ProcessParameters;
44         RtlNormalizeProcessParams (Params);
45
46         // initialize command line buffers
47         CommandLineStringW.Length = Params->CommandLine.Length;
48         CommandLineStringW.MaximumLength = CommandLineStringW.Length + sizeof(WCHAR);
49         CommandLineStringW.Buffer = RtlAllocateHeap(GetProcessHeap(),
50                                                     HEAP_GENERATE_EXCEPTIONS|HEAP_ZERO_MEMORY, 
51                                                     CommandLineStringW.MaximumLength);
52
53         RtlInitAnsiString(&CommandLineStringA, NULL);
54         
55         // copy command line
56         RtlCopyUnicodeString (&CommandLineStringW,
57                               &(Params->CommandLine));
58         CommandLineStringW.Buffer[CommandLineStringW.Length / sizeof(WCHAR)] = 0;
59
60         /* convert unicode string to ansi (or oem) */
61         if (bIsFileApiAnsi)
62             RtlUnicodeStringToAnsiString (&CommandLineStringA,
63                                           &CommandLineStringW, 
64                                           TRUE);
65         else
66             RtlUnicodeStringToOemString (&CommandLineStringA,
67                                          &CommandLineStringW,
68                                          TRUE);
69
70         CommandLineStringA.Buffer[CommandLineStringA.Length] = 0;
71
72         bCommandLineInitialized = TRUE;
73 }
74
75
76 LPSTR STDCALL GetCommandLineA(VOID)
77 {
78         if (bCommandLineInitialized == FALSE)
79         {
80                 InitCommandLines ();
81         }
82
83         DPRINT ("CommandLine \'%s\'\n", CommandLineStringA.Buffer);
84
85         return(CommandLineStringA.Buffer);
86 }
87
88 LPWSTR STDCALL GetCommandLineW (VOID)
89 {
90         if (bCommandLineInitialized == FALSE)
91         {
92                 InitCommandLines ();
93         }
94
95         DPRINT ("CommandLine \'%S\'\n", CommandLineStringW.Buffer);
96
97         return(CommandLineStringW.Buffer);
98 }
99
100 /* EOF */