From 8b7188e6a4a13c1da3b3adfb5b2fd3c9cbbb6eda Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 10 Nov 2011 00:03:53 +0100 Subject: [PATCH] Added the $? command to the shell. --- utils/mxsh.cpp | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/utils/mxsh.cpp b/utils/mxsh.cpp index 1e8a8cff..dfbd474e 100644 --- a/utils/mxsh.cpp +++ b/utils/mxsh.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -9,6 +10,8 @@ using namespace Maxsi; +int status = 0; + void command() { printf("root@sortix / # "); @@ -44,24 +47,15 @@ void command() if ( command[0] == '\0' ) { return; } - if ( String::Compare(command, "$$") == 0 ) { printf("%u\n", Process::GetPID()); return; } - if ( String::Compare(command, "$PPID") == 0 ) { printf("%u\n", Process::GetParentPID()); return; } - if ( String::Compare(command, "exit") == 0 ) { exit(0); return; } - - pid_t child = fork(); - if ( child < 0 ) { printf("fork failed\n"); return; } - if ( child != 0 ) - { - int status; - pid_t childpid = wait(&status); - return; - } + if ( String::Compare(command, "$?") == 0 ) { printf("%u\n", status); status = 0; return; } + if ( String::Compare(command, "$$") == 0 ) { printf("%u\n", Process::GetPID()); status = 0; return; } + if ( String::Compare(command, "$PPID") == 0 ) { printf("%u\n", Process::GetParentPID()); status = 0; return; } int argc = 0; const char* argv[256]; - argv[argc++] = command; + argv[0] = NULL; - bool lastwasspace = false; + bool lastwasspace = true; for ( size_t i = 0; i <= commandused; i++ ) { switch ( command[i] ) @@ -78,6 +72,23 @@ void command() } } + if ( !argv[0] ) { return; } + + if ( strcmp(argv[0], "exit") == 0 ) + { + const char* status = "1"; + if ( 1 < argc ) { status = argv[1]; } + exit(atoi(status)); + } + + pid_t child = fork(); + if ( child < 0 ) { printf("fork failed\n"); return; } + if ( child != 0 ) + { + pid_t childpid = wait(&status); + return; + } + // Replace the current process with another process image. Process::Execute(argv[0], argc, argv);