This program and the accompanying materials are licensed and made available under\r
the terms and conditions of the BSD License that accompanies this distribution.\r
The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php.\r
+ http://opensource.org/licenses/bsd-license.\r
\r
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
**/\r
#include <Uefi.h>\r
#include <Library/UefiLib.h>\r
+#include <Library/DebugLib.h>\r
\r
#include <Library/ShellCEntryLib.h>\r
#include <Library/MemoryAllocationLib.h>\r
;\r
}\r
\r
-static\r
+/** Clean up data as required by the exit() function.\r
+\r
+**/\r
void\r
-FinalCleanup( void )\r
+exitCleanup(INTN ExitVal)\r
{\r
+ void (*CleanUp)(void); // Pointer to Cleanup Function\r
int i;\r
\r
- /* Close any open files */\r
- for(i = OPEN_MAX - 1; i >= 0; --i) {\r
- (void)close(i); // Close properly handles closing a closed file.\r
+ if(gMD != NULL) {\r
+ gMD->ExitValue = (int)ExitVal;\r
+ CleanUp = gMD->cleanup; // Preserve the pointer to the Cleanup Function\r
+\r
+ // Call all registered atexit functions in reverse order\r
+ i = gMD->num_atexit;\r
+ if( i > 0) {\r
+ do {\r
+ (gMD->atexit_handler[--i])();\r
+ } while( i > 0);\r
}\r
\r
- /* Free the global MainData structure */\r
- if(gMD != NULL) {\r
- if(gMD->NCmdLine != NULL) {\r
- FreePool( gMD->NCmdLine );\r
+ if (CleanUp != NULL) {\r
+ CleanUp();\r
}\r
- FreePool( gMD );\r
}\r
}\r
\r
char *string;\r
INTN nArgvSize; /* Cumulative size of narrow Argv[i] */\r
\r
+DEBUG_CODE_BEGIN();\r
+ Print(L"ArgvConvert called with %d arguments.\n", Argc);\r
+ for(count = 0; count < ((Argc > 5)? 5: Argc); ++count) {\r
+ Print(L"Argument[%d] = \"%s\".\n", count, Argv[count]);\r
+ }\r
+DEBUG_CODE_END();\r
+\r
nArgvSize = Argc;\r
/* Determine space needed for narrow Argv strings. */\r
for(count = 0; count < Argc; ++count) {\r
nArgv[count] = string;\r
AVsz = wcstombs(string, Argv[count], nArgvSize);\r
string[AVsz] = 0; /* NULL terminate the argument */\r
+ DEBUG((DEBUG_INFO, "Cvt[%d] %d \"%s\" --> \"%a\"\n", (INT32)count, (INT32)AVsz, Argv[count], nArgv[count]));\r
string += AVsz + 1;\r
nArgvSize -= AVsz + 1;\r
if(nArgvSize < 0) {\r
struct __filedes *mfd;\r
char **nArgv;\r
INTN ExitVal;\r
- INTN i;\r
+ int i;\r
\r
ExitVal = (INTN)RETURN_SUCCESS;\r
gMD = AllocateZeroPool(sizeof(struct __MainData));\r
_fltused = 1;\r
errno = 0;\r
EFIerrno = 0;\r
- gMD->FinalCleanup = &FinalCleanup;\r
\r
#ifdef NT32dvm\r
gMD->ClocksPerSecond = 1; // For NT32 only\r
ExitVal = (INTN)RETURN_INVALID_PARAMETER;\r
}\r
else {\r
- ExitVal = (INTN)main( (int)Argc, nArgv);\r
+ if( setjmp(gMD->MainExit) == 0) {\r
+ ExitVal = (INTN)main( (int)Argc, gMD->NArgV);\r
+ exitCleanup(ExitVal);\r
+ }\r
+ /* You reach here if:\r
+ * normal return from main()\r
+ * call to _Exit(), either directly or through exit().\r
+ */\r
+ ExitVal = (INTN)gMD->ExitValue;\r
+ }\r
+\r
+ if( ExitVal == EXIT_FAILURE) {\r
+ ExitVal = RETURN_ABORTED;\r
+ }\r
+\r
+ /* Close any open files */\r
+ for(i = OPEN_MAX - 1; i >= 0; --i) {\r
+ (void)close(i); // Close properly handles closing a closed file.\r
+ }\r
+\r
+ /* Free the global MainData structure */\r
+ if(gMD != NULL) {\r
+ if(gMD->NCmdLine != NULL) {\r
+ FreePool( gMD->NCmdLine );\r
+ }\r
+ FreePool( gMD );\r
}\r
}\r
- exit((int)ExitVal);\r
- /* Not Reached */\r
return ExitVal;\r
}\r