parent
70fc453159
commit
036d731bf2
@ -0,0 +1,103 @@
|
||||
#define __LIBRARY__
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdarg.h>
|
||||
#define PATH_MAX 1024
|
||||
#define ENOEXEC 8
|
||||
#define MAX_NUMBER_BYTES 1024
|
||||
char tmp_path[PATH_MAX+1];
|
||||
static char * envp[] = { "HOME=/usr/root", NULL, NULL };
|
||||
char ** environ;
|
||||
FILE __stdin;
|
||||
FILE __stdout;
|
||||
_syscall3(pid_t ,waitpid,pid_t ,pid,int * ,wait_stat,int, options)
|
||||
_syscall0(int,fork)
|
||||
_syscall0(int,pause)
|
||||
_syscall0(int,sync)
|
||||
_syscall1(int,chdir,const char *, filename)
|
||||
_syscall3(int,write,int ,fd,const char *, buf, off_t ,count)
|
||||
_syscall3(int, execve,const char *, filename, char **, argv, char ** ,envp)
|
||||
_syscall2(int, getcwd,char *, buf, size_t, size)
|
||||
_syscall2(int ,access,const char *, filename, mode_t ,mode)
|
||||
_syscall3(int,read,int ,fildes,char *, buf, off_t ,count)
|
||||
_syscall2(int, stat,const char * ,filename, struct stat *, stat_buf)
|
||||
|
||||
|
||||
|
||||
int printf(const char *fmt,...)
|
||||
{
|
||||
write(2,fmt,strlen(fmt));
|
||||
}
|
||||
|
||||
pid_t wait(int * wait_stat)
|
||||
{
|
||||
return waitpid(-1,wait_stat,0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void main(int argc, char *argv[])
|
||||
{
|
||||
int pid;
|
||||
int status;
|
||||
char dirname[80];
|
||||
char cmdstr[255];
|
||||
int argc_cmd;
|
||||
char *argv_cmd[10];
|
||||
char *p;
|
||||
char a[50];
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
||||
memset(cmdstr, 0, 255);
|
||||
read(0,cmdstr, strlen(cmdstr));
|
||||
argc_cmd = 0;
|
||||
p = strtok(cmdstr, " \n\t");
|
||||
while (p && argc_cmd < 9){
|
||||
argv_cmd[argc_cmd] = p;
|
||||
p = strtok(NULL, " \n\t");
|
||||
argc_cmd++;
|
||||
}
|
||||
argv_cmd[argc_cmd] = NULL;
|
||||
if (argc_cmd == 0)
|
||||
continue;
|
||||
if (strcmp(argv_cmd[0], "exit") == 0)
|
||||
break;
|
||||
if (strcmp(argv_cmd[0], "sync") == 0){
|
||||
sync();
|
||||
continue;
|
||||
}
|
||||
if (strcmp(argv_cmd[0], "cd") == 0){
|
||||
if ((argc_cmd == 1) || (strcmp(argv_cmd[1], "~") == 0))
|
||||
argv_cmd[1] = "/";
|
||||
if (chdir(argv_cmd[1]) < 0)
|
||||
printf("cd error\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(pid = fork()))
|
||||
{
|
||||
strcpy(a,"/usr/bin/");
|
||||
strcat(a,argv_cmd[0]);
|
||||
if (execve(a, argv_cmd,envp) == -1)
|
||||
printf("execve error\n");
|
||||
exit(1);
|
||||
}
|
||||
while (1)
|
||||
if (pid == wait(&status))
|
||||
break;
|
||||
|
||||
}
|
||||
exit(0);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in new issue