ReactOS  0.4.13-dev-464-g6b95727
entry_point.c File Reference
#include <stdio.h>
#include <ndk/psfuncs.h>
#include <ndk/rtlfuncs.h>
#include <debug.h>
Include dependency graph for entry_point.c:

Go to the source code of this file.

Macros

#define WIN32_NO_STATUS
 
#define NTOS_MODE_USER
 
#define NDEBUG
 

Functions

NTSTATUS __cdecl _main (int argc, char *argv[], char *envp[], ULONG DebugFlag)
 
static VOID FASTCALL EnvironmentStringToUnicodeString (PWCHAR wsIn, PUNICODE_STRING usOut)
 
VOID WINAPI NtProcessStartup (PPEB Peb)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 28 of file entry_point.c.

◆ NTOS_MODE_USER

#define NTOS_MODE_USER

Definition at line 15 of file entry_point.c.

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 12 of file entry_point.c.

Function Documentation

◆ _main()

NTSTATUS __cdecl _main ( int  argc,
char argv[],
char envp[],
ULONG  DebugFlag 
)

Referenced by NtProcessStartup().

◆ EnvironmentStringToUnicodeString()

static VOID FASTCALL EnvironmentStringToUnicodeString ( PWCHAR  wsIn,
PUNICODE_STRING  usOut 
)
static

Definition at line 34 of file entry_point.c.

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 }
USHORT MaximumLength
Definition: env_spec_w32.h:370
uint16_t * PWCHAR
Definition: typedefs.h:54
smooth NULL
Definition: ftsmooth.c:416
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by NtProcessStartup().

◆ NtProcessStartup()

VOID WINAPI NtProcessStartup ( PPEB  Peb)

Definition at line 63 of file entry_point.c.

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 }
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
LONG NTSTATUS
Definition: precomp.h:26
#define argv
Definition: mplay32.c:18
void DbgBreakPoint()
Definition: mach.c:558
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)
#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
_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:1856
PRTL_USER_PROCESS_PARAMETERS ProcessParameters
Definition: btrfs_drv.h:1868
UNICODE_STRING CommandLine
Definition: btrfs_drv.h:1857
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