metalsvm/newlib/examples/mshell.c

54 lines
1.2 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
void showlogo() {
printf("\n\n");
printf("================================================================================\n");
printf(" m(etalsvm)shell\n\n");
printf(" Copyright 2010 Stefan Lankes, Chair for Operating Systems,\n");
printf(" RWTH Aachen University\n\n");
printf("================================================================================\n");
}
void help() {
printf("possible commands: \n");
printf("exit > exit shell \n");
}
int main(int argc, char** argv)
{
char* command = malloc(1024*sizeof(char));
int size, status = 0;
pid_t pid;
system("clear");
showlogo();
while(1) {
2011-05-24 18:54:53 +02:00
printf("$ ");
size = scanf("%s", command);
if(!strcmp(command, "exit")) {
return 0;
}
if(!strcmp(command, "help")) {
help();
} else {
2011-05-24 18:54:53 +02:00
pid = fork();
if (pid == 0) { //child
char* newargv[] = {command, NULL};
char* newenv[] = {"USER=root", "PATH=/bin:/sbin:/usr/bin", "PWD=/", "TEMP=/tmp", NULL};
execve(command, newargv, newenv);
return errno;
2011-05-24 18:54:53 +02:00
} else {
wait(&status);
}
}
}
return errno;
}