- shell now supports multiple args in command line
- executes binaries in /bin/ now
This commit is contained in:
parent
06a2b17978
commit
265008c76d
1 changed files with 51 additions and 9 deletions
|
@ -21,6 +21,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <ctype.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#undef errno
|
#undef errno
|
||||||
|
@ -38,31 +39,72 @@ void help() {
|
||||||
printf("exit > exit shell \n");
|
printf("exit > exit shell \n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define MAXARGS 16
|
||||||
|
|
||||||
|
#define ST_WHITE 0
|
||||||
|
#define ST_ARG 1
|
||||||
|
|
||||||
|
char** splitargs(char* args) {
|
||||||
|
static char* ret[MAXARGS+1];
|
||||||
|
int i = 0;
|
||||||
|
int state = ST_WHITE;
|
||||||
|
while (*args != '\0') {
|
||||||
|
if (state == ST_WHITE) {
|
||||||
|
if (!isspace(*args)) {
|
||||||
|
ret[i++] = args;
|
||||||
|
if (i == MAXARGS)
|
||||||
|
break;
|
||||||
|
state = ST_ARG;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (isspace(*args)) {
|
||||||
|
*args = '\0';
|
||||||
|
state = ST_WHITE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
args++;
|
||||||
|
}
|
||||||
|
ret[i] = NULL;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
char* command = malloc(1024*sizeof(char));
|
char commandline[1024];
|
||||||
|
char* command;
|
||||||
|
|
||||||
int size = 0, status = 0;
|
int size = 0, status = 0;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
system("clear");
|
system("clear");
|
||||||
showlogo();
|
showlogo();
|
||||||
while(1) {
|
while(1) {
|
||||||
printf("$ ");
|
char** newargv;
|
||||||
do {
|
char* newenv[] = {"USER=root", "PATH=/bin:/sbin:/usr/bin", "PWD=/", "TEMP=/tmp", NULL};
|
||||||
size = scanf("%s", command);
|
|
||||||
} while(size <= 0);
|
printf("mshell> ");
|
||||||
|
gets(commandline);
|
||||||
|
|
||||||
|
newargv = splitargs(commandline);
|
||||||
|
command = newargv[0];
|
||||||
|
|
||||||
|
if(command == NULL) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if(!strcmp(command, "exit")) {
|
if(!strcmp(command, "exit")) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(!strcmp(command, "help")) {
|
if(!strcmp(command, "help")) {
|
||||||
help();
|
help();
|
||||||
} else {
|
} else {
|
||||||
|
char path[128];
|
||||||
|
sprintf(path, "/bin/%s", command);
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == 0) { //child
|
if (pid == 0) { //child
|
||||||
char* newargv[] = {command, NULL};
|
status = execve(path, newargv, newenv);
|
||||||
char* newenv[] = {"USER=root", "PATH=/bin:/sbin:/usr/bin", "PWD=/", "TEMP=/tmp", NULL};
|
if (status)
|
||||||
|
printf("mshell: %s: command not found\n", path);
|
||||||
execve(command, newargv, newenv);
|
|
||||||
return errno;
|
return errno;
|
||||||
} else {
|
} else {
|
||||||
wait(&status);
|
wait(&status);
|
||||||
|
|
Loading…
Add table
Reference in a new issue