Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenentry_point.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: See COPYING in the top level directory 00003 * PROJECT: ReactOS 00004 * FILE: lib/nt/entry_point.c 00005 * PURPOSE: Native NT Runtime Library 00006 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net) 00007 */ 00008 00009 /* INCLUDES ******************************************************************/ 00010 00011 /* PSDK/NDK Headers */ 00012 #define WIN32_NO_STATUS 00013 #include <stdio.h> 00014 #include <windows.h> 00015 #define NTOS_MODE_USER 00016 #include <ndk/psfuncs.h> 00017 #include <ndk/rtlfuncs.h> 00018 00019 NTSTATUS 00020 __cdecl 00021 _main( 00022 int argc, 00023 char *argv[], 00024 char *envp[], 00025 ULONG DebugFlag 00026 ); 00027 00028 #define NDEBUG 00029 #include <debug.h> 00030 00031 /* FUNCTIONS ****************************************************************/ 00032 00033 static 00034 VOID FASTCALL EnvironmentStringToUnicodeString (PWCHAR wsIn, PUNICODE_STRING usOut) 00035 { 00036 if (wsIn) 00037 { 00038 PWCHAR CurrentChar = wsIn; 00039 00040 while (*CurrentChar) 00041 { 00042 while(*CurrentChar++); 00043 } 00044 /* double nullterm at end */ 00045 CurrentChar++; 00046 00047 usOut->Buffer = wsIn; 00048 /* FIXME: the last (double) nullterm should perhaps not be included in Length 00049 * but only in MaximumLength. -Gunnar */ 00050 usOut->MaximumLength = usOut->Length = (CurrentChar-wsIn) * sizeof(WCHAR); 00051 } 00052 else 00053 { 00054 usOut->Buffer = NULL; 00055 usOut->Length = usOut->MaximumLength = 0; 00056 } 00057 } 00058 00059 00060 00061 VOID 00062 WINAPI 00063 NtProcessStartup(PPEB Peb) 00064 { 00065 NTSTATUS Status; 00066 PRTL_USER_PROCESS_PARAMETERS ProcessParameters; 00067 PUNICODE_STRING CmdLineString; 00068 ANSI_STRING AnsiCmdLine; 00069 UNICODE_STRING UnicodeEnvironment; 00070 ANSI_STRING AnsiEnvironment; 00071 PCHAR NullPointer = NULL; 00072 INT argc = 0; 00073 PCHAR *argv; 00074 PCHAR *envp; 00075 PCHAR *ArgumentList; 00076 PCHAR Source, Destination; 00077 ULONG Length; 00078 ASSERT(Peb); 00079 00080 #ifdef _M_ARM // Huge achievement 00081 DPRINT1("%s(%08lx) called\n", __FUNCTION__, Peb); 00082 while (TRUE); 00083 #endif 00084 00085 /* Normalize and get the Process Parameters */ 00086 ProcessParameters = RtlNormalizeProcessParams(Peb->ProcessParameters); 00087 ASSERT(ProcessParameters); 00088 00089 /* Allocate memory for the argument list, enough for 512 tokens */ 00090 //FIXME: what if 512 is not enough???? 00091 ArgumentList = RtlAllocateHeap(RtlGetProcessHeap(), 0, 512 * sizeof(PCHAR)); 00092 if (!ArgumentList) 00093 { 00094 DPRINT1("ERR: no mem!"); 00095 Status = STATUS_NO_MEMORY; 00096 goto fail; 00097 } 00098 00099 /* Use a null pointer as default */ 00100 argv = &NullPointer; 00101 envp = &NullPointer; 00102 00103 /* Set the first pointer to NULL, and set the argument array to the buffer */ 00104 *ArgumentList = NULL; 00105 argv = ArgumentList; 00106 00107 /* Get the pointer to the Command Line */ 00108 CmdLineString = &ProcessParameters->CommandLine; 00109 00110 /* If we don't have a command line, use the image path instead */ 00111 if (!CmdLineString->Buffer || !CmdLineString->Length) 00112 { 00113 CmdLineString = &ProcessParameters->ImagePathName; 00114 } 00115 00116 /* Convert it to an ANSI string */ 00117 Status = RtlUnicodeStringToAnsiString(&AnsiCmdLine, CmdLineString, TRUE); 00118 if (!NT_SUCCESS(Status)) 00119 { 00120 DPRINT1("ERR: no mem(guess)\n"); 00121 goto fail; 00122 } 00123 00124 /* Save parameters for parsing */ 00125 Source = AnsiCmdLine.Buffer; 00126 Length = AnsiCmdLine.Length; 00127 00128 /* Ensure it's valid */ 00129 if (Source) 00130 { 00131 /* Allocate a buffer for the destination */ 00132 Destination = RtlAllocateHeap(RtlGetProcessHeap(), 0, Length + sizeof(WCHAR)); 00133 if (!Destination) 00134 { 00135 DPRINT1("ERR: no mem!"); 00136 Status = STATUS_NO_MEMORY; 00137 goto fail; 00138 } 00139 00140 /* Start parsing */ 00141 while (*Source) 00142 { 00143 /* Skip the white space. */ 00144 while (*Source && *Source <= ' ') Source++; 00145 00146 /* Copy until the next white space is reached */ 00147 if (*Source) 00148 { 00149 /* Save one token pointer */ 00150 *ArgumentList++ = Destination; 00151 00152 /* Increase one token count */ 00153 argc++; 00154 00155 /* Copy token until white space */ 00156 while (*Source > ' ') *Destination++ = *Source++; 00157 00158 /* Null terminate it */ 00159 *Destination++ = '\0'; 00160 } 00161 } 00162 } 00163 00164 /* Null terminate the token pointer list */ 00165 *ArgumentList++ = NULL; 00166 00167 /* Now handle the enviornment, point the envp at our current list location. */ 00168 envp = ArgumentList; 00169 00170 if (ProcessParameters->Environment) 00171 { 00172 EnvironmentStringToUnicodeString(ProcessParameters->Environment, &UnicodeEnvironment); 00173 Status = RtlUnicodeStringToAnsiString (& AnsiEnvironment, & UnicodeEnvironment, TRUE); 00174 if (!NT_SUCCESS(Status)) 00175 { 00176 DPRINT1("ERR: no mem(guess)\n"); 00177 goto fail; 00178 } 00179 00180 ASSERT(AnsiEnvironment.Buffer); 00181 00182 Source = AnsiEnvironment.Buffer; 00183 while (*Source) 00184 { 00185 /* Save a pointer to this token */ 00186 *ArgumentList++ = Source; 00187 00188 /* Keep looking for another variable */ 00189 while (*Source++); 00190 } 00191 00192 /* Null terminate the list again */ 00193 *ArgumentList++ = NULL; 00194 } 00195 /* Breakpoint if we were requested to do so */ 00196 if (ProcessParameters->DebugFlags) DbgBreakPoint(); 00197 00198 /* Call the Main Function */ 00199 Status = _main(argc, argv, envp, ProcessParameters->DebugFlags); 00200 00201 fail: 00202 /* We're done here */ 00203 NtTerminateProcess(NtCurrentProcess(), Status); 00204 } 00205 00206 /* EOF */ Generated on Sun May 27 2012 04:36:53 for ReactOS by
1.7.6.1
|