metalsvm/newlib/examples/mshell.c

73 lines
1.8 KiB
C
Raw Normal View History

2011-09-14 14:34:10 +02:00
/*
* Copyright 2011 Marian Ohligs, Chair for Operating Systems,
* RWTH Aachen University
2011-09-15 21:17:25 +02:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
2011-09-14 14:34:10 +02:00
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
2011-09-15 21:42:36 +02:00
#undef errno
extern int errno;
void showlogo() {
printf("\n\n");
printf("================================================================================\n");
printf(" m(etalsvm)shell\n\n");
printf("================================================================================\n");
}
void help() {
printf("possible commands: \n");
printf("exit > exit shell \n");
}
int main(int argc, char** argv)
{
2011-07-30 19:48:44 +02:00
char* command = malloc(1024*sizeof(char));
int size, status = 0;
pid_t pid;
system("clear");
showlogo();
while(1) {
2011-07-30 19:48:44 +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);
}
}
2011-07-30 19:48:44 +02:00
}
return errno;
}