ReactOS  0.4.13-dev-249-gcba1a2f
entry_point.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS
4  * FILE: lib/nt/entry_point.c
5  * PURPOSE: Native NT Runtime Library
6  * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 /* PSDK/NDK Headers */
12 #define WIN32_NO_STATUS
13 #include <stdio.h>
14 //#include <windows.h>
15 #define NTOS_MODE_USER
16 #include <ndk/psfuncs.h>
17 #include <ndk/rtlfuncs.h>
18 
20 __cdecl
21 _main(
22  int argc,
23  char *argv[],
24  char *envp[],
25  ULONG DebugFlag
26 );
27 
28 #define NDEBUG
29 #include <debug.h>
30 
31 /* FUNCTIONS ****************************************************************/
32 
33 static
35 {
36  if (wsIn)
37  {
38  PWCHAR CurrentChar = wsIn;
39 
40  while (*CurrentChar)
41  {
42  while(*CurrentChar++);
43  }
44  /* double nullterm at end */
45  CurrentChar++;
46 
47  usOut->Buffer = wsIn;
48  /* FIXME: the last (double) nullterm should perhaps not be included in Length
49  * but only in MaximumLength. -Gunnar */
50  usOut->MaximumLength = usOut->Length = (CurrentChar-wsIn) * sizeof(WCHAR);
51  }
52  else
53  {
54  usOut->Buffer = NULL;
55  usOut->Length = usOut->MaximumLength = 0;
56  }
57 }
58 
59 
60 
61 VOID
62 WINAPI
64 {
66  PRTL_USER_PROCESS_PARAMETERS ProcessParameters;
67  PUNICODE_STRING CmdLineString;
68  ANSI_STRING AnsiCmdLine;
69  UNICODE_STRING UnicodeEnvironment;
70  ANSI_STRING AnsiEnvironment;
71  PCHAR NullPointer = NULL;
72  INT argc = 0;
73  PCHAR *argv;
74  PCHAR *envp;
75  PCHAR *ArgumentList;
77  ULONG Length;
78  ASSERT(Peb);
79 
80 #ifdef _M_ARM // Huge achievement
81  DPRINT1("%s(%08lx) called\n", __FUNCTION__, Peb);
82  while (TRUE);
83 #endif
84 
85  /* Normalize and get the Process Parameters */
86  ProcessParameters = RtlNormalizeProcessParams(Peb->ProcessParameters);
87  ASSERT(ProcessParameters);
88 
89  /* Allocate memory for the argument list, enough for 512 tokens */
90  //FIXME: what if 512 is not enough????
91  ArgumentList = RtlAllocateHeap(RtlGetProcessHeap(), 0, 512 * sizeof(PCHAR));
92  if (!ArgumentList)
93  {
94  DPRINT1("ERR: no mem!");
96  goto fail;
97  }
98 
99  /* Use a null pointer as default */
100  argv = &NullPointer;
101  envp = &NullPointer;
102 
103  /* Set the first pointer to NULL, and set the argument array to the buffer */
104  *ArgumentList = NULL;
105  argv = ArgumentList;
106 
107  /* Get the pointer to the Command Line */
108  CmdLineString = &ProcessParameters->CommandLine;
109 
110  /* If we don't have a command line, use the image path instead */
111  if (!CmdLineString->Buffer || !CmdLineString->Length)
112  {
113  CmdLineString = &ProcessParameters->ImagePathName;
114  }
115 
116  /* Convert it to an ANSI string */
117  Status = RtlUnicodeStringToAnsiString(&AnsiCmdLine, CmdLineString, TRUE);
118  if (!NT_SUCCESS(Status))
119  {
120  DPRINT1("ERR: no mem(guess)\n");
121  goto fail;
122  }
123 
124  /* Save parameters for parsing */
125  Source = AnsiCmdLine.Buffer;
126  Length = AnsiCmdLine.Length;
127 
128  /* Ensure it's valid */
129  if (Source)
130  {
131  /* Allocate a buffer for the destination */
132  Destination = RtlAllocateHeap(RtlGetProcessHeap(), 0, Length + sizeof(WCHAR));
133  if (!Destination)
134  {
135  DPRINT1("ERR: no mem!");
137  goto fail;
138  }
139 
140  /* Start parsing */
141  while (*Source)
142  {
143  /* Skip the white space. */
144  while (*Source && *Source <= ' ') Source++;
145 
146  /* Copy until the next white space is reached */
147  if (*Source)
148  {
149  /* Save one token pointer */
150  *ArgumentList++ = Destination;
151 
152  /* Increase one token count */
153  argc++;
154 
155  /* Copy token until white space */
156  while (*Source > ' ') *Destination++ = *Source++;
157 
158  /* Null terminate it */
159  *Destination++ = '\0';
160  }
161  }
162  }
163 
164  /* Null terminate the token pointer list */
165  *ArgumentList++ = NULL;
166 
167  /* Now handle the environment, point the envp at our current list location. */
168  envp = ArgumentList;
169 
170  if (ProcessParameters->Environment)
171  {
172  EnvironmentStringToUnicodeString(ProcessParameters->Environment, &UnicodeEnvironment);
173  Status = RtlUnicodeStringToAnsiString (& AnsiEnvironment, & UnicodeEnvironment, TRUE);
174  if (!NT_SUCCESS(Status))
175  {
176  DPRINT1("ERR: no mem(guess)\n");
177  goto fail;
178  }
179 
180  ASSERT(AnsiEnvironment.Buffer);
181 
182  Source = AnsiEnvironment.Buffer;
183  while (*Source)
184  {
185  /* Save a pointer to this token */
186  *ArgumentList++ = Source;
187 
188  /* Keep looking for another variable */
189  while (*Source++);
190  }
191 
192  /* Null terminate the list again */
193  *ArgumentList++ = NULL;
194  }
195  /* Breakpoint if we were requested to do so */
196  if (ProcessParameters->DebugFlags) DbgBreakPoint();
197 
198  /* Call the Main Function */
199  Status = _main(argc, argv, envp, ProcessParameters->DebugFlags);
200 
201 fail:
202  /* We're done here */
204 }
205 
206 /* EOF */
signed char * PCHAR
Definition: retypes.h:7
static int argc
Definition: ServiceArgs.c:12
NTSTATUS __cdecl _main(int argc, char *argv[], char *envp[], ULONG DebugFlag)
#define TRUE
Definition: types.h:120
PPEB Peb
Definition: dllmain.c:27
#define __cdecl
Definition: accygwin.h:79
USHORT MaximumLength
Definition: env_spec_w32.h:370
LONG NTSTATUS
Definition: precomp.h:26
uint16_t * PWCHAR
Definition: typedefs.h:54
#define argv
Definition: mplay32.c:18
void DbgBreakPoint()
Definition: mach.c:558
#define FASTCALL
Definition: nt_native.h:50
int32_t INT
Definition: typedefs.h:56
static VOID FASTCALL EnvironmentStringToUnicodeString(PWCHAR wsIn, PUNICODE_STRING usOut)
Definition: entry_point.c:34
NTSYSAPI PRTL_USER_PROCESS_PARAMETERS NTAPI RtlNormalizeProcessParams(_In_ PRTL_USER_PROCESS_PARAMETERS ProcessParameters)
smooth NULL
Definition: ftsmooth.c:416
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToAnsiString(PANSI_STRING DestinationString, PUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
VOID WINAPI NtProcessStartup(PPEB Peb)
Definition: entry_point.c:63
#define NtCurrentProcess()
Definition: nt_native.h:1657
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:585
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define WINAPI
Definition: msvc.h:8
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:2875
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
UNICODE_STRING ImagePathName
Definition: btrfs_drv.h:1839
PRTL_USER_PROCESS_PARAMETERS ProcessParameters
Definition: btrfs_drv.h:1851
UNICODE_STRING CommandLine
Definition: btrfs_drv.h:1840
Status
Definition: gdiplustypes.h:24
#define STATUS_NO_MEMORY
Definition: ntstatus.h:246
#define DPRINT1
Definition: precomp.h:8
unsigned int ULONG
Definition: retypes.h:1
NTSTATUS NTAPI NtTerminateProcess(HANDLE ProcessHandle, LONG ExitStatus)
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3167
#define __FUNCTION__
Definition: types.h:112