Added the $? command to the shell.

This commit is contained in:
Jonas 'Sortie' Termansen 2011-11-10 00:03:53 +01:00
parent 29a1b68933
commit 8b7188e6a4
1 changed files with 25 additions and 14 deletions

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
@ -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);